Title Description:
AC Source:
This study greedy algorithm, problem-solving ideas: the principle of greed is to make the largest space left, preference for BI and AI difference between the largest, as to why? Here with only 2 equipment for example, (A1,B1) and (A2,B2), assuming that the first transport A1, the moment of transport, the actual space to occupy should be a1+b2, then in order to ensure that the largest space left, there should be a1+b2<a2+b1 set up, to first transport A1, That is B1-A1>B2-B1. (N devices can be 22 to make this comparison, to achieve the optimal selection)
#include "iostream" #include "algorithm" using namespace std;struct equipment {int A; int B;}; BOOL CMP (equipment A, equipment B) {return (A.B-A.A) > (B.B-B.A);} int main () {int T, V, N; BOOL Flag; Equipment eq[1000]; scanf ("%d", &t); for (int i = 0; i < T; i++) {scanf ("%d%d", &v, &n); for (int j = 0; J < N; j + +) {scanf ("%d%d", &eq[j]. A, &eq[j]. B); } sort (eq, eq+n, CMP); Flag = true; for (int j = 0; J < N; j + +) {if (Eq[j]. B <= v) {v-= eq[j]. A } else {flag = false; Break }} if (flag) {printf ("yes\n"); } else {printf ("no\n"); }} return 0;}
HD-ACM Algorithm Specialization series (--crixalis) equipment