1. 程式人生 > >uva1153 Keep the Customer Satisfied

uva1153 Keep the Customer Satisfied

日期 const bool pan scan 如果 opera name can

貪心加優先隊列 (默認是小的在前,正好)

//這裏又很套路,設隊列裏的都是符合條件的考慮新加入的即可。再處理一下空隊列的情況。很完美//

截止時間短的在前面,幹的就多
先根據截止日期排序
優先隊列根據完成所需時間排序
首先隊列裏的都是能完成的
策略:新加入的,如果在前面的完成後仍能完成,就直接加進去;不能,因為截止日期在更後,不影響其他的,比較top元素用的時間,更短就換掉(此時因為截止日期靠後,用的時間還短,所以能成立)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include 
<algorithm> using namespace std; const int MAXN = 800005; const int N = 2000005; struct order{ int q, d; bool operator < (const order &a) const { return q<a.q; } }o[MAXN]; int cmp (order a, order b) { return a.d < b.d; } int n; int solve() { priority_queue<int
> done; int sum = 0, temp; for (int i = 0; i < n; i++) { if (o[i].q + sum <= o[i].d) { done.push(o[i].q); sum += o[i].q; } else if (!done.empty()){ temp = done.top(); if (temp > o[i].q) { sum = sum - temp + o[i].q; done.pop(); done.push(o[i].q); } } }
return done.size(); } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &o[i].q, &o[i].d); sort(o, o + n, cmp); printf("%d\n", solve()); if(T) printf("\n"); } return 0; }

uva1153 Keep the Customer Satisfied