#include <bits/stdc++.h>
#define pii pair<int, int>
#define mp make_pair
#define F first
#define S second
#define PB push_back
#define maxc 1000000007
#define N 500005
using namespace std;
deque<int> q;
int n, a[N], x2[N], res[N], delta[N];
void inline MAX(int &A, int B) {A = max(A, B);}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("INP.TXT", "r", stdin);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= sqrt(n) + 1; i++)
x2[i] = i*i;
for (int i = 1; i <= sqrt(n) + 1; i++)
delta[i] = x2[i] - x2[i-1];
for (int can = 1; can <= sqrt(n)+1; can++)
{
int len = delta[can];
while (!q.empty()) q.pop_back();
for (int i = 1; i <= n; i++)
{
int pos = i - x2[can-1] - 1;
while (!q.empty() && q.front() <= pos - len)
q.pop_front();
if (pos > 0)
{
while (!q.empty() && a[q.back()] <= a[pos])
q.pop_back();
q.push_back(pos);
}
if (!q.empty()) MAX(res[i], a[q.front()] + can);
}
while (!q.empty()) q.pop_back();
for (int i = n; i >= 1; i--)
{
int pos = i + x2[can-1] + 1;
while (!q.empty() && q.front() >= pos + len)
q.pop_front();
if (pos <= n)
{
while (!q.empty() && a[q.back()] <= a[pos])
q.pop_back();
q.push_back(pos);
}
if (!q.empty()) MAX(res[i], a[q.front()] + can);
}
}
for (int i = 1; i <= n; i++)
if (res[i] <= a[i]) cout <<0<<'\n';
else cout <<res[i] - a[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
1540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
2328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
434 ms |
3176 KB |
Output is correct |
2 |
Correct |
180 ms |
3288 KB |
Output is correct |
3 |
Correct |
439 ms |
3708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
843 ms |
4944 KB |
Output is correct |
2 |
Correct |
706 ms |
5900 KB |
Output is correct |
3 |
Correct |
913 ms |
6988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
9220 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
13620 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1074 ms |
19576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1071 ms |
24404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |