UVA Live 7146 Defeat the Enemy(贪心+set)

6 篇文章 0 订阅
5 篇文章 0 订阅


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 theattack power Attacki, and defense power Defensei. We can use at most one troop to attack one enemyvillage and a troop can only be used to attack only one enemy village. Even if a troop survives anattack, 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 againstthe other troop simultaneously. If a troop’s defense power is less than or equal to the other troop’sattack 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 havemost 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 startwith 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 mlines describe the enemy troops. Each line consist of EAttacki and EDefensei, the attack power anddefense power of enemy troops

Output

For each test ease, output one line containing ‘Case #x: y’, where x is the test case number (startingfrom 1) and y is the max number of survive troops of our tribe. If it‘s impossible to destroy all enemytroops, output ‘-1’ instead.Limits:1 ≤ T ≤ 100,1 ≤ n, m ≤ 105,1 ≤ Attacki, Defensei, EAttacki, EDefensei ≤ 109,

Sample Input

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个大炮,两军交战。

交战规则是:两方同时选择一个大炮,然后互相攻击,如果我方的攻击力大于敌方的防御力,那么敌方的大炮被破坏。如果我方的防御力小于等于敌方的攻击力,那么我方的大炮也被破坏。一个大炮只能用一次,即只能攻击敌方的一个大炮。

现在我方要将敌方的所有大炮都打掉,而且我方要保留尽可能多的大炮。


思路:

贪心。首先我将敌方的大炮按照防御力高低排序,对于敌方的某一座大炮,我要选择我方的一些攻击力高于敌方防御力的大炮,然后在这些大炮里面,又要尽量选择防御力低但不会被敌方破坏的。如果对于敌方的一座大炮,我任何大炮都不能击溃他,那就是-1了。


代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
struct node{
  int a,d;
}x[100005],y[100005];
bool cmp(node a,node b)
{
    return a.a>b.a;
}
bool cmp2(node a,node b)
{
    return a.d>b.d;
}
int main()
{
    multiset<int>st;
    multiset<int>::iterator it;
    int T,n,m,i,j,k,icase=0;
    scanf("%d",&T);
    while(T--)
    {
        icase++;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
            scanf("%d%d",&x[i].a,&x[i].d);
        for(i=1;i<=m;i++)
            scanf("%d%d",&y[i].a,&y[i].d);
        sort(x+1,x+1+n,cmp);    //我方按照攻击力由高到低排。
        sort(y+1,y+1+m,cmp2);    //敌方按照防御力由高到低排。
        st.clear();
        k=1;
        int ans=n;
        for(i=1;i<=m;i++)
        {
            while(x[k].a>=y[i].d&&k<=n)   //将攻击力高于敌方一座大炮的防御力的大炮放入set。
            {
                st.insert(x[k].d);
                    k++;
            }
            if(st.size()==0){
                ans=-1;break;
            }
            it=st.upper_bound(y[i].a);   //找到防御力最低的那个。
            if(it==st.end()){            //如果没有找到,我方就损失了一座大炮。就把那个防御力最低的大炮给破坏
                ans--;
                st.erase(st.begin());
            }
            else
            st.erase(it);
        }
        printf("Case #%d: ",icase);
        printf("%d\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值