標籤:
3709: [PA2014]BohaterTime Limit: 5 Sec Memory Limit: 128 MBSec Special Judge
Submit: 653 Solved: 220
[Submit][Status][Discuss]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 Input3 5
3 1
4 8
8 3Sample OutputTAK
2 3 1 很容易想到先把打完怪吃掉血以後加血的怪先幹掉 而且要按傷害從小到大慢慢打(等於你有一個初始血量,只要你現在血量大於這種怪的血量你就能獲得(給的血-掉的血)的血量) 然後剩下的一部分直接按藥從大到小排序。。 把傷害看做血瓶,把血瓶看做傷害,從最終的狀態往回倒流,那麼你就會發現,這和第一種是同一個情況了。。。這思路真是厲害,想了半天奇怪的貪心沒貪過去Orz
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 6 #define maxn 100001 7 8 using namespace std; 9 10 inline long long in()11 {12 int x=0;char ch=getchar();13 while(ch<‘0‘||ch>‘9‘)ch=getchar();14 while(ch<=‘9‘&&ch>=‘0‘)x=x*10+ch-‘0‘,ch=getchar();15 return x;16 }17 18 struct ed{19 int x,y,id;20 }a[maxn],b[maxn];21 22 int xb[maxn];23 24 bool cmp1(const ed A,const ed B)25 {26 return A.x<B.x;27 }28 29 bool cmp2(const ed A,const ed B)30 {31 return A.y>B.y;32 }33 34 int main()35 {36 int n,x,y,cnt1=0,cnt2=0,cnt=0;37 long long d;38 n=in(),d=in();39 for(int i=1;i<=n;i++)40 {41 x=in(),y=in();42 if(x<=y)a[++cnt1].x=x,a[cnt1].y=y,a[cnt1].id=i;43 else b[++cnt2].x=x,b[cnt2].y=y,b[cnt2].id=i;44 }45 sort(1+a,1+a+cnt1,cmp1);46 sort(1+b,1+b+cnt2,cmp2);47 for(int i=1;i<=cnt1;i++)48 {49 if(d<=a[i].x){printf("NIE");return 0;}50 d+=a[i].y-a[i].x;51 }52 for(int i=1;i<=cnt2;i++)53 {54 if(d<=b[i].x){printf("NIE");return 0;}55 d+=b[i].y-b[i].x;56 }57 printf("TAK\n");58 for(int i=1;i<=cnt1;i++)59 printf("%d ",a[i].id);60 for(int j=1;j<=cnt2;j++)61 printf("%d ",b[j].id);62 return 0;63 }View Code
【貪心】bzoj 3709:[PA2014]Bohater