bzoj 3709: [PA2014]Bohater【greedy】

First hit the ones that can restore blood, and hit them according to the consumption from small to large; then hit the rest in descending order according to the amount of blood returned; if the middle stamina is less than 0, there will be no solution.

#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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889903&siteId=291194637