本文共 2601 字,大约阅读时间需要 8 分钟。
Problem Description
Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts of Kalimdor. Though he’s a guardian of Lich King now, he keeps the living habit of a scorpion like living underground and digging holes.
Someday Crixalis decides to move to another nice place and build a new house for himself (Actually it’s just a new hole). As he collected a lot of equipment, he needs to dig a hole beside his new house to store them. This hole has a volume of V units, and Crixalis has N equipment, each of them needs Ai units of space. When dragging his equipment into the hole, Crixalis finds that he needs more space to ensure everything is placed well. Actually, the ith equipment needs Bi units of space during the moving. More precisely Crixalis can not move equipment into the hole unless there are Bi units of space left. After it moved in, the volume of the hole will decrease by Ai. Crixalis wonders if he can move all his equipment into the new hole and he turns to you for help.
Input
The first line contains an integer T, indicating the number of test cases. Then follows T cases, each one contains N + 1 lines. The first line contains 2 integers: V, volume of a hole and N, number of equipment respectively. The next N lines contain N pairs of integers: Ai and Bi.
0<T<= 10, 0<V<10000, 0<N<1000, 0 <Ai< V, Ai <= Bi < 1000.Output
For each case output “Yes” if Crixalis can move all his equipment into the new hole or else output “No”.
Sample Input
2
20 3
10 20 3 10 1 710 2
1 10 2 11Sample Output
Yes
No突破口:要找那种占空间,即a尽量小的,同时要先装那些需要临时空间,即b大的,这样才能装的更多的equipment。当b-a越大时,说明这就是我们所要找的先装进去的equipment。
代码如下:
#include#include using namespace std;struct equip{ int a; int b; int v; //v=b-a,用于衡量哪个应该先装}e[1005];bool cmp(equip x,equip y) //按v从大到小排序,v同,按b从大到小排{ if(x.v!=y.v) return x.v>y.v; return x.b>y.b; }int main(){ int T,n,V; int i,j; int flag1,flag2,flag3,left,sum; scanf("%d",&T); while(T--) { flag1=flag2=flag3=1;sum=0; // printf("\n"); scanf("%d %d",&V,&n); left=V; for(i=0;i<=n-1;i++) { scanf("%d%d",&e[i].a,&e[i].b); e[i].v=e[i].b-e[i].a; sum+=e[i].a; if(e[i].b>V) flag1=0; } if(sum>V) flag2=0; if(flag1&&flag2) { sort(e,e+n,cmp); // for(i=0;i<=n-1;i++) printf("%d %d %d\n",e[i].a,e[i].b,e[i].v); for(i=0;i<=n-1;i++) { if(left>=e[i].b) { left-=e[i].a; } else { flag3=0;break; } } if(flag3) printf("Yes\n"); else printf("No\n"); } else printf("No\n"); } return 0;}
小结: AC思路: 1.要尽可能多装equipment; 2.首选那些a相对较小的; 3.有些a很小,但b很大,这种东西就应该先装,由局部扩展到整体,则应先选那些a相对b较小的东西。 4.不要单纯只看见一个物理量,如只考虑b的大小而不考虑a的大小。
转载地址:http://vfdci.baihongyu.com/