UVALive 7146 Defeat the Enemy贪心

18 篇文章 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 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 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[i].l和防御力a[i].r,我方的每一个军队只能攻击一个敌方的军队,且只能攻击一次,当攻击的军队的攻击力大于等于敌方的防御力,若防御力大于等于敌方的攻击力,那么敌方军队消灭,且我方攻击的军队还存在,否则我两个军队同归于尽,问要把敌方的m个军队都消灭,我方至少损失多少军队。

思路:我方军队按攻击力递减排序,敌方按防御力递减排序。

依次枚举敌人,把我方攻击力大于等于地方防御力的加入multiset,然后找set中有没有防御力大于敌方攻击力的,有的话直接删去我方的这个部队,否则删掉集合中最后一个元素。

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#include <stdlib.h>
#define lowbit(x) (x&-x)
#define e exp(1.0)
//ios::sync_with_stdio(false);
//    auto start = clock();
//    cout << (clock() - start) / (double)CLOCKS_PER_SEC;
typedef long long ll;
typedef long long LL;
using namespace std;

typedef struct troop
{
    int s,t;
    bool operator<(const troop & a)const
    {
        return s>a.s;
    }
}T;
int cmp(const troop &a,const troop &b)
{
    return a.s>b.s;
}
int cmp2(const troop &a,const troop &b)
{
    return a.t>b.t;
}
multiset<int>ms;
int main()
{
    int i,j,k,p,t;
    int n,m,flag;
    T f[100001],r[100001];
    cin>>t;
    for(p=1;p<=t;p++)
    {
        cin>>n>>m;
        for(i=0;i<n;i++)
            scanf("%d%d",&f[i].s,&f[i].t);
        for(i=0;i<m;i++)
            scanf("%d%d",&r[i].s,&r[i].t);
        if(m>n)
            cout<<"Case #"<<p<<": -1"<<endl;
        else
        {
            int ans=n;
            flag=1;
            sort(f,f+n,cmp);
            sort(r,r+m,cmp2);
            ms.clear();
            j=0;
            for(i=0;i<m;i++)
            {
                for(;j<n;j++)
                {
                    if(f[j].s>=r[i].t)
                        ms.insert(f[j].t);
                    else break;
                }
                if(ms.empty())
                {
                    flag=0;
                    break;
                }
                auto it=ms.upper_bound(r[i].s);
                if(it==ms.end())
                {
                    ans--;
                    it=ms.begin();
                    ms.erase(it);
                }
                else ms.erase(it);
            }
            if(!flag)cout<<"Case #"<<p<<": -1"<<endl;
            else cout<<"Case #"<<p<<": "<<ans<<endl;
        }
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值