[HDU] 3177.Crixalis's Equipment (greedy)

Problem Description

 Subject to the effect:

 Space V and N of the article, each article placed there for Ai (reduce the size of the article into the remaining space), Bi required (minimum remaining before the article is placed in the space, if the remaining space is larger than the current size We can not put)

Input
 T sets of data, each set of data has N + 1 line, each data line of the first input V, N, N lines after a description of each line item Ai, 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
 
This is a topic greedy, greedy strategy and to demonstrate also read online dalao idea, here's mathematical proof:
 

For x (a1, b1) y ( a2, b2)
after the first x y maximum space needed for the max (b1, a1 + b2)
after the first x y maximum space was desired max (b2, a2 + b1)
obvious to put the two items, the priority is the minimum space required
greedy strategy compared
min (max (b1, a1 + b2), max (b2, a2 + b1)) ①
then it can be simplified to b1, b2 together with the other one of the smaller ai
b1 + a2 <b2 + a1 ②
was A1-B1 <b2-A2 ③
PS :( maybe somewhat difficult to understand, but the ① above formula can be written directly in the comparison function max (b1, a1 + b2) <max (b2, a2 + b1) , and can be the same AC)

pps: I In the following code, because I used in descending order, the following code is changed to max (b1, a1 + b2)> max (b2, a2 ​​+ b1), specifically see dalao the code should be noted qwq

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 struct pr
 9 {
10     int a,b;
11     bool operator <(const pr &x)
12     {
13         if(b-a==x.b-x.a)
14             return b<x.b;
15         return b-a<x.b-x.a;
16     }
17 }p[1010];
18 
19 int main()
20 {
21     int T,n,v;
22     cin>>T;
23     while(T--)
24     {
25         scanf("%d%d",&v,&n);
26         for(int i=0;i<n;++i)
27             scanf("%d%d",&p[i].a,&p[i].b);
28         sort(p,p+n);
29         bool suc=true;
30         for(int i=n-1;i>=0;--i)
31         {
32             if(v>=p[i].b)
33                 v-=p[i].a;
34             else
35             {
36                 suc=false;
37                 break;
38             }
39         }
40         if(suc)    printf("Yes\n");
41         else printf("No\n");
42     }
43     return 0;
44 }
View Code

Although it is relatively greedy algorithm based on greedy strategy but which in many cases are always different, usually need more focus on the accumulation and thinking qwq

Orz continue to go fishing in troubled waters

Guess you like

Origin www.cnblogs.com/JNzH/p/11004717.html