#### CodeForces 589K – Task processing [暴力/数学/优先队列]

http://codeforces.com/problemset/problem/589/K

### Problem

Vasya wants to create a computing system to process arbitrary tasks. As a part of the system he needs an algorithm which will choose an order of task execution.

He came up with the following algorithm:

• There is a single execution queue. For task to be completed, it needs to be added to the queue.
• For each task two values are known: li and ti — the number of seconds that it takes to complete the task and the moment of time when this task is added to the execution queue.
• If at some moment of time T the algorithm has to select a task for execution, it picks the one with the minimum value of li - (T - ti)2. In case of a tie, algorithm picks a task with the lowest index. Then for the following li seconds the algorithm will wait for the task to be completed.

In order to test the algorithm Vasya wants you to simulate it.

You are given n tasks. For each task, you know the number of seconds li that it takes to complete the task and the moment of time ti when this task is added to the execution queue.

For each task find out the moment of time when it will be completed.

### Analysis

PS…这道题没出真的挺可惜的，以后碰到没人做的题，自己觉得能出，还是要大胆做一下改一下调一下bug…说不定能AC…

### Code

```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

struct Node {
int len, add, id;
Node(int len = 0, int add = 0, int id = 0):
bool operator < (const Node &rhs) const {
return len == rhs.len ? id > rhs.id : len > rhs.len;
}
};
const int maxn = 102400, inf = 0x3f3f3f3f;
int N;
Node v[maxn];
map<int, priority_queue<Node>> qu;
ll res[maxn];

inline ll sqr(ll x) {
return x * x;
}

int main() {
scanf("%d", &N);
for(int i = 1; i <= N; ++i) {
v[i].id = i;
}
sort(v + 1, v + N + 1, [](const Node &a, const Node &b) { return a.add < b.add; });
ll T = 0;
int pos = 1;
while(true) {
if(pos <= N && qu.empty() && T < v[pos].add) {
}
while(pos <= N && v[pos].add <= T) {
pos += 1;
}
if(qu.empty()) break;
int stadd = qu.begin()->first;
ll best = 0x3f3f3f3f3f3f3f3f;
int bestid = qu.begin()->second.top().id;
int ans = stadd;
for(auto &i : qu) {
if(sqr(i.first - stadd) > maxn)
break;
ll cur = i.second.top().len - sqr(T - i.first);
int curid = i.second.top().id;
if(cur < best || (cur == best && curid < bestid)) {
best = cur;
bestid = curid;
ans = i.first;
}
}
T += qu[ans].top().len;
res[bestid] = T;
qu[ans].pop();
if(qu[ans].empty())
qu.erase(ans);
}
for(int i = 1; i <= N; ++i) {
printf("%I64d%c", res[i], i==N ? '\n' : ' ');
}
return 0;
}
```