HDU 3177

Crixalis's Equipment

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1491 Accepted Submission(s): 611


Problem Description
HDU 3177Crixalis - 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 7 10 2 1 10 2 11
 

 

Sample Output
Yes No
 
 1 /*

 2 bool cmp(const S &s1, const S &s2){  

 3     if(s1.a + s2.b == s2.a + s1.b)  

 4         return s1.b < s2.b;  

 5     return s1.a + s2.b < s2.a + s1.b;  

 6     //return s1.b - s1.a > s2.b - s2.a;  也可   

 7 }  

 8 我们设第一件物品的占用空间和搬运所需空间分别为A1和B1,第二件为A2和B2,则:

 9 先搬第一件物品所需最大临时空间为A1 + B2,先搬第二件物品所需的最大临时空间为A2 + B1,那么很明显应该选择所需最大临时空间较小的方案。

10 */

11 #include <iostream>

12 #include <cstring>

13 #include <cstdlib>

14 using namespace std;

15 

16 typedef struct Node

17 {

18     int a,b;

19 }Node;

20 Node ch[1005];

21 

22 int cmp(const void *a,const void *b)

23 {

24     Node *c = (Node *)a;

25     Node *d = (Node *)b;

26     if(d->b != c->b)

27         return (d->b-d->a)- (c->b - c->a);

28     else

29         return c->a - d->a; 

30 }

31 

32 int main()

33 {

34     int i,j,k,t,T;

35     cin>>T;

36     int v,n;

37     while(T--)

38     {

39         memset(ch,0,sizeof(ch));

40         cin>>v>>n;

41         int x,y;

42         for(i=1;i<=n;i++)

43         {

44             cin>>x>>y;

45             ch[i].a = x,ch[i].b = y;

46         }

47         qsort(&ch[1],n,sizeof(Node),cmp);

48         for(i=1;i<=n;i++)

49         {

50             if(v>=ch[i].b&&v>=ch[i].a)//实际上第二个条件没必要,因为说了b>a,加上保险些 

51                 v -= ch[i].a;

52             else

53                 break;

54         }

55         if(i>n)

56             cout<<"Yes"<<endl;

57         else

58             cout<<"No"<<endl;

59     }

60     return 0;

61 }

62             

63         

64         

 

你可能感兴趣的:(HDU)