UVALive 7146 Defeat the Enemy(模拟)

编程入门 行业动态 更新时间:2024-10-10 10:27:55

<a href=https://www.elefans.com/category/jswz/34/1758933.html style=UVALive 7146 Defeat the Enemy(模拟)"/>

UVALive 7146 Defeat the Enemy(模拟)

思路:按攻击方的攻击力排序,防御方的防御力排序,然后对于每一个防御方肯定是在攻击方找一个攻击力比它防御力高并且防御力比防御方的攻击力稍微大一点点的最优,所以用个multiset搞一下就OK了


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
const int maxn = 1e5+6;
struct en
{int gongji;int fangyu;
}a[maxn],b[maxn];
bool cmp1(en a,en b)
{return a.gongji>b.gongji;
}
bool cmp2(en a,en b)
{return a.fangyu > b.fangyu;
}
int main()
{int T,cas=1;scanf("%d",&T);while (T--){int n,m;scanf("%d%d",&n,&m);for (int i = 0;i<n;i++)scanf("%d%d",&a[i].gongji,&a[i].fangyu);for (int i = 0;i<m;i++)scanf("%d%d",&b[i].gongji,&b[i].fangyu);if (n<m){printf("Case #%d: -1\n",cas++);continue;}sort(a,a+n,cmp1);sort(b,b+m,cmp2);multiset<int>s;int cnt,pos,flag=1;pos=0;cnt=0;for (int i = 0;i<m;i++){for (int j = pos;j<n;j++){if (a[j].gongji>=b[i].fangyu){s.insert(a[j].fangyu);pos++;}elsebreak;}if (s.empty()){flag=0;break;}else{multiset<int>::iterator it;it = s.upper_bound(b[i].gongji);if (it != s.end()){s.erase(it);}else{s.erase(s.begin());cnt++;}}}if (flag)printf("Case #%d: %d\n",cas++,n-cnt);elseprintf("Case #%d: -1\n",cas++);}
}


Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others.
One day, there is another tribe become their target. The strong tribe has decide to terminate them!!!
There are m villages in the other tribe. Each village contains a troop with attack power EAttacki
,
and defense power EDefensei
. Our tribe has n troops to attack the enemy. Each troop also has the
attack power Attacki
, and defense power Defensei
. We can use at most one troop to attack one enemy
village and a troop can only be used to attack only one enemy village. Even if a troop survives an
attack, it can’t be used again in another attack.
The battle between 2 troops are really simple. The troops use their attack power to attack against
the other troop simultaneously. If a troop’s defense power is less than or equal to the other troop’s
attack power, it will be destroyed. It’s possible that both troops survive or destroy.
The main target of our tribe is to destroy all the enemy troops. Also, our tribe would like to have
most number of troops survive in this war.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case start
with 2 numbers n and m, the number of our troops and the number of enemy villages. n lines follow,
each with Attacki and Defensei
, the attack power and defense power of our troops. The next m
lines describe the enemy troops. Each line consist of EAttacki and EDefensei
, the attack power and
defense power of enemy troops
Output
For each test ease, output one line containing ‘Case #x: y’, where x is the test case number (starting
from 1) and y is the max number of survive troops of our tribe. If it‘s impossible to destroy all enemy
troops, output ‘-1’ instead.
Limits:
1 ≤ T ≤ 100,
1 ≤ n, m ≤ 105
,
1 ≤ Attacki
, Defensei
, EAttacki
, EDefensei ≤ 109

Sample Input23 25 77 31 24 42 22 13 41 105 6Sample OutputCase #1: 3Case #2: -1

更多推荐

UVALive 7146 Defeat the Enemy(模拟)

本文发布于:2024-02-07 11:20:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756550.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UVALive   Enemy   Defeat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!