bzoj 3709: [PA2014]Bohater【贪心】

先打能回血的,按消耗从小到大打;然后按回血量降序打剩下的;中间体力小于0就输出无解

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100005;
int n,t1,t2;
long long z;
struct qwe
{
    int d,a,id;
}a[N],b[N];
bool cmp1(const qwe &a,const qwe &b)
{
    return a.d<b.d;
}
bool cmp2(const qwe &a,const qwe &b)
{
    return a.a>b.a;
}
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
int main()
{
    n=read(),z=read();
    for(int i=1;i<=n;i++)
    {
        int x=read(),y=read();
        if(x<=y)
            a[++t1].d=x,a[t1].a=y,a[t1].id=i;
        else 
            b[++t2].d=x,b[t2].a=y,b[t2].id=i;
    }
    sort(a+1,a+t1+1,cmp1);
    for(int i=1;i<=t1;i++)
    {
        if(z<=a[i].d)
        {
            puts("NIE");
            return 0;
        }
        else 
            z=z-a[i].d+a[i].a;
    }
    sort(b+1,b+t2+1,cmp2);
    for(int i=1;i<=t2;i++)
    {
        if(z<=b[i].d)
        {
            puts("NIE");
            return 0;
        }
        else 
            z=z-b[i].d+b[i].a;
    }
    puts("TAK");
    for(int i=1;i<=t1;i++)
        printf("%d ",a[i].id);
    for(int i=1;i<=t2;i++)
        printf("%d ",b[i].id);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lokiii/p/8951166.html