作者: 赵晓鹏时间限制: 1S章节: 动态规划与贪心
输入范例 :
2
1 4
3 1
3
1 6
3 1
7 1
3
1 6
3 2
7 1
输出范例 :
10
21
23
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXMUM 50000;
long long tsum = 0;
struct student {
long long start;
long long process;
};
bool comparable1(student a, student b) {
return a.start < b.start;
}
int main()
{
long long result = 0;
long long ptr = 0;
long long excuTime = 0; //执行时间
long long interval = 0; //当前开始时间和下一个任务的开始时间的间隔
long long length;
student arr[50000];
priority_queue<long long, vector<long long>, greater<long long>> q;
for (; cin >> length;) {
long long answer = 0;
for (long long i = 0; i < length; i++)
{
cin >> arr[i].start;
cin >> arr[i].process;
}
sort(arr, arr + length, comparable1);
long long temperate = arr[0].start;
for (long long i = 1; i < length; i++)
{
q.push(arr[i - 1].process);
if (temperate != arr[i].start)
{
interval = arr[i].start - temperate;
ptr = temperate;
while (!q.empty())
{
if (interval > 0) {
if (q.top() > interval) {
long long s = q.top();
q.pop();
s -= interval;
q.push(s);
break;
}
else {
interval -= q.top();
answer += q.top() + ptr;
ptr += q.top();
q.pop();
}
}
else
{
break;
}
}
temperate = arr[i].start;
}
}
q.push(arr[length - 1].process);
while (1)
{
if (!q.empty()) {
answer += q.top() + temperate;
temperate += q.top();
q.pop();
}
else {
break;
}
}
cout << answer << endl;
}
return 0;
}