题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】
似乎没有看到跟我一样的做法?
首先这个题对于每一个物品有两个参数
Hint
设最大的
算法流程
得到这个事实后,我们可以维护一个大根堆,初始为空,我们按照时间
这么做,复杂度是跟
#include<cstdio>
#include<queue>
#include<algorithm>
#define rg register
#define MX (100000 + 5)
#define ll long long
#define int long long
std::priority_queue<int> q;
int read(){//快速读入}
struct WORK{
int pos ,val;
bool operator <(const WORK &B)const{return pos == B.pos ? val > B.val : pos > B.pos;}
}P[MX];
signed main(){
int n = read();
for(rg int i = 1 ,p ,v ; i <= n ; ++i){
p = read(); v = read();
P[i] = (WORK){p ,v};
}
std::sort(P + 1 ,P + 1 + n);
P[n + 1] = (WORK){0 ,0}; // 终止条件
ll Ans = 0;
for(rg int i = 1 ; i <= n ; ){ // 此处的 i 枚举的是 n 个插入操作
while(true){ // 同一秒的操作都一次性完成
q.push(P[i++].val);
if(P[i].pos != P[i - 1].pos) break;
// 下一个要插入的不是同一秒的就 break 掉
}
int cnt = P[i - 1].pos - P[i].pos;
// 找到两组插入之间的时间差,节约了枚举时间的复杂度
while(!q.empty() && cnt--){ // 注意堆要非空
Ans += q.top(); // 每秒都选最大值
q.pop();
}
}
printf("%lld\n" ,Ans);
return 0;
}