题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2831

思路:按剩余时间排序即可,简单贪心。

hdu 2831(贪心)_i++hdu 2831(贪心)_ios_02
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 #define MAXN 111
 7 struct Node{
 8    int v,d,index;
 9 }node[MAXN];
10 int n,t;
11 bool flag;
12 
13 int cmp(const Node &p,const Node &q){
14    return p.v-p.d-t<q.v-q.d-t;
15 }
16 
17 
18 int main(){
19  //  freopen("1.txt","r",stdin);
20    while(~scanf("%d%d",&n,&t)){
21       for(int i=1;i<=n;i++){
22          scanf("%d%d",&node[i].v,&node[i].d);
23          node[i].index=i;
24       }
25       sort(node+1,node+1+n,cmp);
26       flag=true;
27       int tt=t;
28       for(int i=1;i<=n;i++){
29          if(node[i].v-node[i].d-t<0){ flag=false;break; }
30          t+=tt;
31       }
32       if(!flag){ puts("The zombies eat your brains!");continue; }
33       for(int i=1;i<=n;i++){
34          i==1?printf("%d",node[i].index):printf(" %d",node[i].index);
35       }
36       puts("");
37    }
38    return 0;
39 }
View Code