Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3177
Problem descriptioncrix‑sand King used to be a giant scorpion (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 crixincludecides 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 crix?has n equipment, each of them needs AI units of space. when dragging his equipment into the hole, crix?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 crix?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. crix?wonders if he can move all his equipment into the new hole and he turns to you for help.
Inputthe 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.
Outputfor each case output "yes" if crix?can move all his equipment into the new hole or else output "no". sample input
220 310 203 101 710 21 102 11
Sample output
YesNo
Sourcehdu 2009-10 Programming Contest
Question:
Put an item in a hole with a capacity of V. Each item has a parking volume and a movable volume. Can you put all the items down?
Idea: -- greedy
Move parking volume
First item A1 B1
Item 2 B2
Assume that the moving volume of these two items is not greater than the volume of the hole V
When we compare two items separately, we will find that A1 + B2 puts the first item first, and then the maximum instantaneous volume of the second item.
A2 + B1 puts the second item first, and then the maximum instantaneous volume of the first item
Then we should select a relatively small first release in A1 + B2 and A2 + B1.
Then, from two items to N items, assume that the Movement volume of N items is no larger than the volume V of the holes,
Sort n items by a1 + b2 <A2 + B1 (in fact, according to the difference value) and then place them in the cave.
PS:
If it is simply sorted by Bi from large to small, it is wa;
Let's take a look at this case!
21 2
8 18
1 20
Yes
The Code is as follows:
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>using namespace std;struct num{ int a, b;} p[1017];bool cmp(num A, num B){ return A.b-A.a > B.b-B.a;}int main(){ int t; int v, n; scanf("%d",&t); while(t--) { scanf("%d %d",&v,&n); for(int i = 0; i < n; i++) { scanf("%d %d",&p[i].a,&p[i].b); } sort(p,p+n,cmp); int i; for(i = 0; i < n; i++) { if(v >= p[i].b) { v-=p[i].a; } else break; } if(i == n) printf("Yes\n"); else printf("No\n"); } return 0;}
HDU 3177 crixalis's equipment (Greedy)