It is greedy and priority queues, very smart to solve:
This greed is very clever ah
C, L; const int MAX_C = 2500 ; const int MAX_L = 2500 ; P A [MAX_L]; // store sunscreen P B [MAX_C]; // for storing bovine // qsort ordering function int CMP ( const void A *, const void * B) { P * AA = (P * ) A; P * BB = (P * ) B; return AA-> First> BB-> First? . 1 : - . 1 ; // the SPF is ordered Preferences } int main () { cin>>C>>L; P temp; priority_queue<int, vector<int>, greater<int> > pque; //注意是由大到小出现的 for(int i = 0; i < C; i++){ scanf("%d%d", &b[i].first, &b[i].second); } for(int i = 0; i < L; i++){ scanf("%d%d", &a[i].first, &a[i].second); } //升序排序, 完成对优先队列的存储 qsort(a, L, sizeof(a[0]), cmp); qsort(b, C, sizeof(b[0]), cmp); /* printf("SHOW THE BOTTLE!\n"); for(int i = 0; i < L; i++) printf("%d %d\n", a[i].first, a[i].second); puts(""); printf("SHOW THE COW : \n"); for(int i = 0; i < C; i++){ printf("%d %d\n", b[i].first, b[i].second); }*/ int res = 0; int j = 0; 0I =int(forwe must pay attention to the use of a cream i; b cattle, using J// ; I <L; I ++) { // array sunscreen traverse the while (J <&& A C [I] .first> = B [J] .first) { pque.push (B [J] .second) ; // maximum value of cattle, priority queue, because he's the minimum (section head) must be satisfied, we see here is the key to his maximum (tail section) J ++ ; } the while (pque!. empty () && A [I] .second) { // queue is not empty, sunscreen unspent int X = pque.top (); pque.pop (); // priority queue been taken out IF (X <A [ I] .first) Continue ; // head meet, but do not satisfy the tail section RES ++; a [I] .second--; // for answer, and update the number of sunscreen } } the printf ( "%d\n", res); return 0; }