bzoj 3709: [PA2014]Bohater
3709: [PA2014]Bohater
Description
在一款电脑游戏中,你需要打败n只怪物(从1到n编号)。为了打败第i只怪物,你需要消耗d[i]点生命值,但怪物死后会掉落血药,使你恢复a[i]点生命值。任何时候你的生命值都不能降到0(或0以下)。请问是否存在一种打怪顺序,使得你可以打完这n只怪物而不死掉
Input
第一行两个整数n,z(1<=n,z<=100000),分别表示怪物的数量和你的初始生命值。
接下来n行,每行两个整数d[i],a[i](0<=d[i],a[i]<=100000)
Output
第一行为TAK(是)或NIE(否),表示是否存在这样的顺序。
如果第一行为TAK,则第二行为空格隔开的1~n的排列,表示合法的顺序。如果答案有很多,你可以输出其中任意一个。
Sample Input
3 5
3 1
4 8
8 3
3 1
4 8
8 3
Sample Output
TAK
2 3 1
2 3 1
题解:
对于输入的d[i],a[i],将d[i]<=a[i]的分为一组,那么这一组的顺序肯定是按d[i]升序,因为血量始终是在上升的。
另一组d[i]>a[i],可以把它转化为第一组的模型,那么就是反着a[i]递增,也就是正着a[i]递减。
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100005;
struct node
{
int a,b,id;
}p[N],q[N];
int n,i,x,y,cnt,tot;
long long m;
bool cmp(const node&x,const node&y)
{
return x.a<y.a;
}
bool Cmp(const node&x,const node&y)
{
return x.b>y.b;
}
int main()
{
scanf("%d%lld",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
if(x<=y) p[++cnt].a=x,p[cnt].b=y,p[cnt].id=i;else
q[++tot].a=x,q[tot].b=y,q[tot].id=i;
}
sort(p+1,p+cnt+1,cmp);
for(i=1;i<=cnt;i++)
if(m-p[i].a<=0)
{
printf("NIE");
return 0;
} else m+=p[i].b-p[i].a;
sort(q+1,q+tot+1,Cmp);
for(i=1;i<=tot;i++)
if(m-q[i].a<=0)
{
printf("NIE");
return 0;
} else m+=q[i].b-q[i].a;
printf("TAK\n");
for(i=1;i<=cnt;i++) printf("%d ",p[i].id);
for(i=1;i<=tot;i++) printf("%d ",q[i].id);
return 0;
}
一念起,天涯咫尺; 一念灭,咫尺天涯。

浙公网安备 33010602011771号