HDU4415 Assassin’s Creed

贪心

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int Ni = 100010;
 7 struct node{
 8     int a,b;
 9     bool operator < (const node &aa) const
10     {
11         return a<aa.a;
12     }
13 }arr[Ni];
14 int main()
15 {
16     int i,n,m,t,k,cs=1;
17     scanf("%d",&t);
18     while(t--)
19     {
20         int ansm=0,ansn=0;k=-1;
21         scanf("%d%d",&n,&m);
22         for(i=0;i<n;i++)
23             scanf("%d%d",&arr[i].a,&arr[i].b);
24         sort(arr,arr+n);
25         for(i=0;i<n;i++) if(arr[i].b>=1)
26             break;
27         if(arr[i].a<=m)
28         {
29             ansm+=arr[i].a;k=i;ansn++;
30             for(i=0;i<n;i++)
31                 ansn+=arr[i].b;
32         }
33         if(ansn>=n) {printf("Case %d: %d %d\n",cs++,n,ansm);continue;}
34         for(i=0;i<n&&arr[i].a+ansm<=m&&ansn!=n;i++)
35             if(i!=k) {ansm+=arr[i].a;ansn++;}
36         printf("Case %d: %d %d\n",cs++,ansn,ansm);
37     }
38     return 0;
39 }

 看了评论后完善的代码

1

6 5
1 0
1 0
1 0
1 0
1 0
5 1

ans: 5 5

上面的程序输出是2 5(不过也AC了可能是数据不够强)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int Ni = 100010;
 7 struct node{
 8     int a,b;
 9     bool operator < (const node &aa) const
10     {
11         return a<aa.a;
12     }
13 }arr[Ni];
14 int main()
15 {
16     int i,n,m,t,k,cs=1;
17     scanf("%d",&t);
18     while(t--)
19     {
20         int ansm=0,ansn=0;k=-1;
21         scanf("%d%d",&n,&m);
22         for(i=0;i<n;i++)
23             scanf("%d%d",&arr[i].a,&arr[i].b);
24         sort(arr,arr+n);
25         for(i=0;i<n;i++) if(arr[i].b>=1)
26             break;
27         if(arr[i].a<=m)
28         {
29             ansm+=arr[i].a;k=i;ansn++;
30             for(i=0;i<n;i++)
31                 ansn+=arr[i].b;
32         }
33         if(ansn>=n) {printf("Case %d: %d %d\n",cs++,n,ansm);continue;}
34         for(i=0;i<n&&arr[i].a+ansm<=m&&ansn!=n;i++)
35             if(i!=k) {ansm+=arr[i].a;ansn++;}
36         int ans_n=ansn,ans_m=ansm;
37         //考虑不杀有刀的人
38         ansn=0;ansm=0;
39         for(i=0;i<n&&arr[i].a+ansm<=m&&ansn!=n;i++)
40             {ansm+=arr[i].a;ansn++;}
41         if(ans_n>ansn||(ans_n==ansn&&ans_m<ansm))
42             printf("Case %d: %d %d\n",cs++,ans_n,ans_m);
43         else
44             printf("Case %d: %d %d\n",cs++,ansn,ansm);
45     }
46     return 0;
47 }

 

posted @ 2012-09-24 13:18  qijinbiao1  阅读(650)  评论(1编辑  收藏  举报