7146 Defeat The Enemy
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 EAttack i, and defense power EDefense i. Our tribe has n troops to attack the enemy. Each troop also has the
attack power Attack i , and defense power Defense i . 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 Attack i and Defense i , the attack power and defense power of our troops. The next m lines describe the enemy troops. Each line consist of EAttack i and EDefense i , 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<10^5,
1<Attack i,Defense i,EAttack i,EDefense i<10^9.
Sample Input
2
3 2
5 7
7 3
1 2
4 4
2 2
2 1
3 4
1 10
5 6
Sample Output
Case #1: 3
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 EAttack i, and defense power EDefense i. Our tribe has n troops to attack the enemy. Each troop also has the
attack power Attack i , and defense power Defense i . 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 Attack i and Defense i , the attack power and defense power of our troops. The next m lines describe the enemy troops. Each line consist of EAttack i and EDefense i , 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<10^5,
1<Attack i,Defense i,EAttack i,EDefense i<10^9.
Sample Input
2
3 2
5 7
7 3
1 2
4 4
2 2
2 1
3 4
1 10
5 6
Sample Output
Case #1: 3
Case #2: -1
题意:
我方有n个士兵,对方有m个士兵,每个士兵有自己的攻击力和防御力,要派士兵过去把对方的士兵都干掉。当两个士兵相遇时,如果A的攻击力不低于B的防御力,则B就死掉了,同理如果B的攻击力不低于A的防御力,A就死掉了,可以发生两个士兵同时死亡的情况。一个士兵只能参加一次作战(即最多只能干掉对方一个敌人)。问我方能否干掉对方所有的敌人,如果能,我方最多能剩下多少士兵存活。
题解:
去年上海赛的题目,比赛的时候一A过了这次做竟然WA了好久。
贪心题。
对我方的士兵按照攻击从大到小的顺序排序,对敌方的士兵按照防御从大到小的顺序排序。然后扫描的对方的士兵,每次把我方所有能干掉这个敌人的士兵都加到集合里面去,然后看看有没有士兵的防御力高于敌方的攻击力,如果有则派防御力高于敌方的攻击力中防御力最低的士兵去,如果没有则派防御力最低的士兵去。
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <set>
using namespace std;
struct point
{
int a;
int b;
void re()
{
scanf("%d%d",&a,&b);
}
}p[100005],d[100005];
multiset<int>s;
multiset<int>::iterator it;
bool comp1(const point &a,const point &b){return a.a>b.a;}
bool comp2(const point &a,const point &b){return a.b>b.b;}
int main()
{
int t,n,m,num;
scanf("%d",&t);
int ans;
bool f;
for (int cas=1;cas<=t;cas++)
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) p[i].re();
for (int i=1;i<=m;i++) d[i].re();
if (n<m)
{
printf("Case #%d: -1\n",cas);
continue;
}
sort(p+1,p+n+1,comp1);
sort(d+1,d+m+1,comp2);
ans=n;
s.clear();
num=0;
f=true;
for (int i=1;i<=m;i++)
{
while (num<n&&p[num+1].a>=d[i].b)
{
s.insert(p[++num].b);
}
if (s.empty()) {f=false;break;}
it=s.upper_bound(d[i].a);
if (it==s.end()) {it=s.begin();ans--;}
s.erase(it);
}
if (!f) printf("Case #%d: -1\n",cas);
else printf("Case #%d: %d\n",cas,ans);
}
return 0;
}