太担心于算法的正确性,一不留神被输出的空行要求坑了。凡是只给一组sample的,都应该首先留意输出格式的文字要求
另外,题目的"before its due date"也是坑啊,加一个"or on"会怀孕么
Run Time: 0.399s
#define UVa "8-12.1153.cpp" //Keep the Customer Satisfied
char fileIn[30] = UVa, fileOut[30] = UVa;
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct Job {
int q, d;
Job(int a, int b):q(a),d(b){}
bool operator < (const Job& j) const {
return d < j.d;
}
};
//Global Variables. Reset upon Each Case!
const int maxn = 800000 + 10;
int T, n;
/////
int dp(int i, int j);
int main() {
scanf("%d", &T);
while(T--) {
priority_queue<int> ans;
vector<Job> v;
scanf("%d", &n);
int a, b;
for(int i = 0; i < n; i ++) {
scanf("%d%d", &a,&b);
v.push_back(Job(a,b));
}
sort(v.begin(), v.end());
long long t = 0;
for(int i = 0; i < n; i ++) {
Job& u = v[i];
if (!ans.empty() && t + u.q > u.d && ans.top() > u.q) { //not enough time.
t -= ans.top();
ans.pop();
}
if(t + u.q <= u.d) {
t += u.q;
ans.push(u.q);
}
}
printf("%d\n", ans.size());
if(T) printf("\n");
}
return 0;
}

本文提供了一种解决UVa 8-12.1153问题的有效算法实现,该算法通过优先队列来管理任务并确保每个任务在其截止日期前完成。讨论了样例输出格式的重要性以及题目描述中容易忽视的细节。

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



