题目链接:点击打开链接
思路:
我们按照时间从小到大排序, 因为时间最大2e6, 所以从小到大枚举时间, 当一个订单到了最后期限, 我们就看看它能否满足, 如果能就满足它, 如果不能, 我们看一下之前满足的订单中货物最多的一个是否比当前这个大, 如果大,就拿它换当前这个, 为后面节省空间。 用优先队列可以很方便的维护。
细节参见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1e6 + 10;
int T,n,m;
struct node {
int v, t;
node(int v=0, int t=0):v(v), t(t) {}
bool operator < (const node& rhs) const {
return t < rhs.t;
}
}a[maxn];
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d", &n);
int maxt = 0;
priority_queue<int> q;
for(int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].v, &a[i].t);
maxt = max(maxt, a[i].t);
}
int cnt = 1;
sort(a+1, a+n+1);
int cur = 0, ans = 0;
for(int i = 0; i <= maxt; i++) {
if(i) ++cur;
while(cnt <= n && a[cnt].t == i) {
if(cur >= a[cnt].v) {
q.push(a[cnt].v);
cur -= a[cnt].v;
++ans;
}
else if(!q.empty()) {
int hehe = q.top();
if(hehe > a[cnt].v) {
q.pop();
q.push(a[cnt].v);
cur += (hehe - a[cnt].v);
}
}
++cnt;
}
}
printf("%d\n", ans);
if(T) printf("\n");
}
return 0;
}