题意:
有n个工作,已知每个工作需要的时间q和截止时间d,最多能完成多少个工作?工作只能一个一个完成,起始时间从0开始。
分析:
以d排序,优先队列。
可以保证永远都是符合条件的时间小的在优先队列中。
代码:
#include<bits/stdc++.h>
#define LL long long
#define ms(s) memset(s, 0, sizeof(s))
using namespace std;
const int maxn = 1e6 + 10;
struct Node {
int p, d;
friend bool operator < (const Node& n1, const Node& n2) {
return n1.d < n2.d;
}
}node[maxn];
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
int kase = 0;
while(T--) {
if(kase++) std::cout << endl;
int n;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> node[i].p >> node[i].d;
}
sort(node, node + n);
priority_queue<int> pq;
int cur = 0;
for(int i = 0; i < n; i++) {
if(node[i].p + cur <= node[i].d) {
cur += node[i].p;
pq.push(node[i].p);
} else {
if(pq.empty()) continue;
int v = pq.top();
if(v >= node[i].p) {
pq.pop();
cur -= v - node[i].p;
pq.push(node[i].p);
}
}
}
cout << pq.size() << endl;
}
return 0;
}

该博客分析了如何使用优先队列解决UVa 1153问题,即在给定每个工作的时间需求和截止日期的情况下,确定最多能完成多少个工作。工作按截止日期排序,确保优先处理时间更紧迫的任务。文章提供了解决方案的代码实现。
&spm=1001.2101.3001.5002&articleId=103101295&d=1&t=3&u=9a8ceea6153841d5b99b14b4c4bc0dcf)
221

被折叠的 条评论
为什么被折叠?



