Description
在一款电脑游戏中,你需要打败n只怪物(从1到n编号)。为了打败第i只怪物,你需要消耗d[i]点生命值,但怪物死后会掉落血药,使你恢复a[i]点生命值。任何时候你的生命值都不能降到0(或0以下)。请问是否存在一种打怪顺序,使得你可以打完这n只怪物而不死掉
Input
第一行两个整数n,z(1<=n,z<=100000),分别表示怪物的数量和你的初始生命值。
接下来n行,每行两个整数d[i],ai
Output
第一行为TAK(是)或NIE(否),表示是否存在这样的顺序。
如果第一行为TAK,则第二行为空格隔开的1~n的排列,表示合法的顺序。如果答案有很多,你可以输出其中任意一个。
Sample Input
3 5
3 1
4 8
8 3
Sample Output
TAK
2 3 1
题解
首先当然把能加血的都打了啊!
加血的按d排序,从小到大打
扣血的话,设扣完后的血量为T,那么扣前的血量是T-a[i]+d[i]
可以发现是上面那个操作的逆序
于是我们按a从大到小排再搞就好了
更优秀的证明
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
struct node
{
LL u,v,upd;int op;
}a[110000];
bool cmp(node n1,node n2){return n1.u<n2.u;}
bool cmpx(node n1,node n2){return n1.v>n2.v;}
int n;LL z;
int ans[110000],tp;
int main()
{
scanf("%d%lld",&n,&z);
for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i].u,&a[i].v),a[i].upd=a[i].v-a[i].u,a[i].op=i;
sort(a+1,a+1+n,cmp);
bool bk=false;
LL now=z;
for(int i=1;i<=n;i++)
if(a[i].upd>0)
{
if(now>a[i].u)now+=a[i].upd,ans[++tp]=a[i].op;
else {bk=true;break;}
}
if(bk==true){printf("NIE\n");return 0;}
sort(a+1,a+1+n,cmpx);
for(int i=1;i<=n;i++)
if(a[i].upd<=0)
{
if(now>a[i].u)now+=a[i].upd,ans[++tp]=a[i].op;
else {bk=true;break;}
}
if(bk==true)printf("NIE\n");
else
{
printf("TAK\n");
for(int i=1;i<tp;i++)printf("%d ",ans[i]);
printf("%d\n",ans[tp]);
}
return 0;
}