其他分享
首页 > 其他分享> > 2021-TRN3-J

2021-TRN3-J

作者:互联网

2021-TRN3-J

https://vjudge.net/contest/424076#problem/J

#include <bits/stdc++.h>

using namespace std;
const int maxn=1e5+5;
struct node
{
    int attack,defense;
}a[maxn],b[maxn];
multiset<int>myset;
multiset<int>::iterator it;

int cmp1(struct node a,struct node b)
{
    return a.attack>b.attack;
}
int cmp2(struct node a,struct node b)
{
    return a.defense>b.defense;
}
int main()
{
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,i,j,ans;
        cas++;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].attack,&a[i].defense);
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&b[i].attack,&b[i].defense);
        }
        sort(a+1,a+1+n,cmp1);///把我方按攻击力递减排序
        sort(b+1,b+1+m,cmp2);///把敌方按防御力递减排序
        myset.clear();
        ans=n;///一开始认为全部存活
        for(i=1,j=1;i<=m;i++)
        {
            while(j<=n&&b[i].defense<=a[j].attack)///注意j<=n
            {
                myset.insert(a[j].defense);
        ///找出所有我方能够打败当前敌方军队的军队,把防御力丢到multiset里面
                j++;
            }
            if(myset.size()==0)
            {///myset为空,说明我方剩下的军队中已经没有可以打败敌方该军队的
                ans=-1;
                break;
            }
            else
            {
                it=myset.upper_bound(b[i].attack);
            ///在myset里面寻找防御力恰好可以抵挡该敌军进攻的我方军队
                if(it==myset.end())
                {
                    myset.erase(myset.begin());
                    ans--;///没找到,我方防御力最小的军队阵亡
                    continue;
                }
                else
                {
                    myset.erase(it);
                }
            }
        }
        printf("Case #%d: %d\n",cas,ans);
    }
    return 0;
}

标签:struct,int,myset,attack,defense,2021,ans,TRN3
来源: https://blog.csdn.net/booniebears/article/details/114091901