Crixalis's Equipment
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.
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.
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 7
10 2
1 10
2 11
Sample Output
Yes
No
题意: 蝎子想搬家(洞穴) 然后因为有家具所以 所以挖洞的时候要考虑家具的 摆放 摆放家具的时候 家具会占用 a 个 空间 在将家具移动到 合适的位置的时候 需要b个空间 新家的空间v 和 家具的件数n 然后跟着n行 分别是 每件家具的 占用空间 和 移动空间 现在问你 能不能 将 这些 家具 移动到 新家
因为有 移动空间 所以 问题就变得复杂了 . 但是核心思想还是 让 空间尽量慢的减少 在下一个家具没有移动进去之前要尽量的节省剩余空间 而且如果想移进去 剩余空间必须大于等于 家具的 占地空间和移动空间 排序的话 就按照 上个家具的占地空间+这个家具的移动空间 最小就行 (这样每次移动的时候 都会出现相同的状况(程序设计 找 长状态节点的思想))
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 struct hole 5 { 6 int a,b; // 占地 为a 移动空间 为b 7 }; 8 bool cmp(hole a,hole b) 9 { 10 if(a.a+b.b<a.b+b.a) 11 return 1; 12 else 13 if(a.a+b.b==a.b+b.a) 14 return a.b>b.b; 15 else 16 return 0; // 让上次的 已经占地的物品 和 这次的物品的移动空间 尽量小 . 17 } 18 int main() 19 { 20 int t,i,n,v; 21 hole a[1050]; 22 scanf("%d",&t); 23 while(t--) 24 { 25 int flag=1; 26 scanf("%d%d",&v,&n); 27 for(i=0;i<n;i++) 28 scanf("%d%d",&a[i].a,&a[i].b); 29 sort(a,a+n,cmp); 30 for(i=0;i<n;i++) 31 { 32 if(v>=a[i].a&&v>=a[i].b) // 剩下的 空间 大于等于 这次物品的 占地空间和 移动空间 33 { 34 v-=a[i].a; 35 } 36 else 37 { 38 flag=0; 39 break; 40 } 41 } 42 if(flag) 43 printf("Yes "); 44 else 45 printf("No "); 46 } 47 return 0; 48 }