# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37735 | 14kg | Mountain Trek Route (IZhO12_route) | C++11 | 2000 ms | 6312 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <queue>
#include <vector>
#include <functional>
#include <algorithm>
#define next(x) (x==n?1:x+1)
#define before(x) (x==1?n:x-1)
using namespace std;
typedef pair<int, pair<int, int> > pip;
int n, m, in[1000001];
priority_queue<pip, vector<pip>, greater<pip> > Q;
int main() {
int s, num = -1;
int path, x, y, len, tot = 0, tx;
bool check;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &in[i]);
if (num < in[i]) num = in[i], s = i;
}
path = s, len = 0, check = false;;
for (int i = next(s); i != s; i = next(i)) {
if (in[path] > in[i]) x = i, len = 1, check = true;
if (in[path] == in[i]) len++;
if (in[path] < in[i] && check) Q.push({ len,{x, path} }), check = false;
path = i;
}
if (in[path] < in[s] && check) Q.push({ len,{x, path} });
while ((!Q.empty()) && Q.top().first <= m) {
len = Q.top().first, x = Q.top().second.first, y = Q.top().second.second;
Q.pop(), m -= len ,tot++;
tx = x;
for (int i = 1; i <= len; i++) in[tx]++, tx = next(tx);
tx = x, num = 0;
while (in[before(tx)] == in[tx]) {
num++, tx = before(tx), len++;
if (num > n) break;
}
if (in[before(tx)] == in[tx]) break;
if (in[before(tx)] < in[tx]) continue;
while (in[y] == in[next(y)]) len++, y = next(y);
if (in[y] < in[next(y)]) Q.push({ len,{tx,y} });
}
printf("%d", tot * 2);
//getchar(), getchar();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |