Shirlies
宁静专注认真的程序媛~

这一题要用差值排序,用Bi排序是不可以的,譬如容量为20,有两个5,17和1,16,如果用bi排序就要先放5,17,然后还剩下15,不可以放1,16了,而如果先放1,16还剩下19就可以放5,17.。。。至于其中的真理我没有仔细研究,敲完代码提交A了后看了一下别人的思路,觉得这个解释的不错http://hi.baidu.com/niren_cn/blog/item/11c701915d393b1e7af4807a.html

代码如下:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 const int maxn = 1000 + 100;
 6 struct hole
 7 {
 8     int ai,bi;
 9 }h[maxn];
10 int V,N;
11 
12 bool cmp(const hole& a,const hole& b)
13 {
14     int aa = a.bi - a.ai;
15     int bb = b.bi - b.ai;
16     if(aa == bb)
17     {
18         return a.bi > b.bi;
19     }
20 
21     return aa > bb;
22 }
23 
24 int main()
25 {
26     int cas;
27     scanf("%d",&cas);
28     while(cas --)
29     {
30         scanf("%d%d",&V,&N);
31         for(int i = 0;i < N;i ++)
32         {
33             scanf("%d%d",&h[i].ai,&h[i].bi);
34         }
35         std::sort(h,h + N,cmp);
36 
37         int flag = 0;
38         int cnt = V;
39         for(int i = 0;i < N;i ++)
40         {
41             if(cnt < h[i].bi)
42             {
43                 flag = 1;
44                 break;
45             }
46             cnt = cnt - h[i].ai;
47         }
48 
49         if(flag)
50         {
51             printf("No\n");
52         }
53         else
54         {
55             printf("Yes\n");
56         }
57 
58     }
59 
60     return 0;
61 }

 

posted on 2012-06-10 15:59  Shirlies  阅读(600)  评论(0编辑  收藏  举报