#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;
int n, m, in[1000001];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q;
int main() {
int s, num = -1;
int path, x, 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 }), check = false;
path = i;
}
if (in[path] < in[s] && check) Q.push({ len,x });
while ((!Q.empty()) && Q.top().first <= m) {
len = Q.top().first, x = Q.top().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);
if (num > n) break;
}
if (in[before(tx)] == in[tx]) break;
if (in[before(tx)] < in[tx]) continue;
x = tx, len = 1;
while (in[tx] == in[next(tx)]) len++, tx = next(tx);
if (in[tx] < in[next(tx)]) Q.push({ len,x });
}
printf("%d", tot * 2);
//getchar(), getchar();
}
Compilation message
route.cpp: In function 'int main()':
route.cpp:18:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
^
route.cpp:20:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &in[i]);
^
route.cpp:31:21: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (in[path] < in[s] && check) Q.push({ len,x });
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
5072 KB |
Output is correct |
2 |
Correct |
0 ms |
5072 KB |
Output is correct |
3 |
Correct |
0 ms |
5072 KB |
Output is correct |
4 |
Correct |
0 ms |
5072 KB |
Output is correct |
5 |
Correct |
0 ms |
5072 KB |
Output is correct |
6 |
Correct |
0 ms |
5072 KB |
Output is correct |
7 |
Correct |
26 ms |
5072 KB |
Output is correct |
8 |
Correct |
73 ms |
5072 KB |
Output is correct |
9 |
Correct |
496 ms |
5072 KB |
Output is correct |
10 |
Correct |
356 ms |
5072 KB |
Output is correct |
11 |
Execution timed out |
2000 ms |
5924 KB |
Execution timed out |
12 |
Halted |
0 ms |
0 KB |
- |