贪心,经典题的变形,只是变成了要消耗时间,其实还是跟经典的求最大价值相似。
View Code
1 #define REP(i, n) for (int i = 0; i < (n); i++) 2 #define PRIQ priority_queue 3 typedef pair<int, int> PII; 4 typedef vector<PII> VPII; 5 6 PRIQ<int, VI, less<int> > Q; 7 VPII rec; 8 9 int main() { 10 // freopen("in", "r", stdin); 11 int T, n, q, d; 12 scanf("%d", &T); 13 while (T--) { 14 scanf("%d", &n); 15 rec.clear(); 16 REP(i, n) { 17 scanf("%d%d", &q, &d); 18 rec.PB(MPR(d, q)); 19 } 20 sort(ALL(rec)); 21 while (!Q.empty()) Q.pop(); 22 int sum = 0; 23 REP(i, n) { 24 if (sum + rec[i].SE > rec[i].FI) { 25 if (!Q.empty()) { 26 if (Q.top() > rec[i].SE && (sum - Q.top() + rec[i].SE <= rec[i].FI)) { 27 sum -= Q.top(); 28 Q.pop(); 29 sum += rec[i].SE; 30 Q.push(rec[i].SE); 31 } 32 } 33 } else { 34 sum += rec[i].SE; 35 Q.push(rec[i].SE); 36 } 37 } 38 printf("%d\n", SZ(Q)); 39 if (T) puts(""); 40 } 41 return 0; 42 }
——written by Lyon