您的位置:首页 > 其它

UVALive 7146 muliset<> 容器用法 防御塔

2015-10-05 22:06 405 查看
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.

题目大意:每个军队有一个攻击力和防御力,当一个军队的攻击力大于等于对方的防御力,就可以摧毁敌军(可以双方同时阵亡)。现给出我方和敌方所有军队的攻击力和防御力,问要击溃敌方所有军队,最多能保留多少的存活兵力(只能单挑)。

思路:贪心。

把我方按攻击力从大到小排序,把敌方按防御力从大到小排序。

遍历敌方的军队,设当前敌方军队为enemy。

要击溃enemy,首先我方派出军队的攻击力要大于等于enemy的防御力。如果我方能不损兵干掉对面,就贪心选择一个防御力最小的但又大于enemy的攻击力的军队。如果必须损兵,就贪心地选择一个防御力最小的军队。这个可以用multiset维护。

multiset<>可以共存多个存值相同的. set不行

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
using namespace std;
struct node
{
int att,def;
}p[100010],a[100010];
int cmp1(node p1,node p2)
{
return p1.att>p2.att;
}
int cmp2(node p1,node p2)
{
return p1.def>p2.def;
}
multiset<int>s;
multiset<int>::iterator it;
int n,m;
int solve()
{
int num=0,ans=n;
for(int i=0;i<m;i++)
{
while(num<n&&a[num].att>=p[i].def)
{
s.insert(a[num].def);
num++;
}
if(s.empty())
return -1;
it=s.lower_bound(p[i].att+1);
if(it==s.end())
{
s.erase(s.begin());//找不到满足的就用防御力最小的攻击
ans--;
}
else
s.erase(it);
}
return ans;
}
int main()
{
int T;
while(~scanf("%d",&T))
{
for(int Case=1;Case<=T;Case++)
{
s.clear();
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].att,&a[i].def);
for(int i=0;i<m;i++)
scanf("%d%d",&p[i].att,&p[i].def);
sort(a,a+n,cmp1);
sort(p,p+m,cmp2);
int ans=solve();
printf("Case #%d: %d\n",Case,ans);

}
}
}
/*
2
3 2
5 7
7 3
1 2
4 4
2 2
2 1
3 4
1 10
5 6

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: