题目大意
工程师需要安装n个服务,其中的第i个服务Ji需要Si个单位的时间,截止时间为Di,如果在截止时间之前完成任务不会收到惩罚,如果超时惩罚时间时Ci-di意思就是超时多少惩罚值就越大,从t=0时刻开始,找到两个惩罚值之和最小的任务。
分析
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 800000 + 10;
struct order {
int s, t;
bool operator < (const order & a) const {
return s < a.s;
}
}ord[maxn];
priority_queue<order>q;
bool cmp(const order &a, const order & b) {
return a.t < b.t || (a.t == b.t && a.s < b.s);
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin>>ord[i].s>>ord[i].t;
}
sort(ord, ord + n, cmp);
while (!q.empty())q.pop();
int cur = 0;
for (int i = 0; i < n; ++i) {
if (cur + ord[i].s <= ord[i].t) {
q.push(ord[i]);
cur += ord[i].s;
}
else {
if (q.empty())continue;
order u = q.top();
if (u.s > ord[i].s) {
cur = cur - u.s + ord[i].s;
q.pop();
q.push(ord[i]);
}
}
}
printf("%d\n",q.size());
if (t)puts("");
}
return 0;
}
知识兔View Code