UVALive - 3507 Keep the Customer Satisfied
题意:收到n个订单,每个订单有q,d分别代表做这个的时间,和最晚的完成时间,问你最多能接受几个订单
思路:贪心,我们显然要按最早的完成时间排序,那么接下来,我们用(6,8)和(4,9)做为例子,按照我们的贪心原则我们首先选择(6,8),然后再(4,9),但显然(4,9)作为首选才是最好的选择,试想一下不能两个都选的情况,就是我们总共做的时间4+6>9(第二个的最迟的时间),那么我们要删除做的时间最长的才是最优的
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int MAXN = 1000002; struct node{ int q,d; }arr[MAXN]; int n; bool cmp(node a,node b){ return a.d < b.d; } int main(){ int t; scanf("%d",&t); for (int cas = 0; cas < t; cas++){ if (cas) printf("\n"); scanf("%d",&n); for (int i = 0; i < n; i++) scanf("%d%d",&arr[i].q,&arr[i].d); sort(arr,arr+n,cmp); priority_queue<int> Q; int ans = 0; for (int i = 0; i < n; i++){ ans += arr[i].q; Q.push(arr[i].q); while (ans > arr[i].d){ ans -= Q.top(); Q.pop(); } } printf("%d\n",Q.size()); } return 0; }