#include <bits/stdc++.h>
using namespace std;
int sequence(int n, vector<int> a) {
int l_max[n], l_min[n], r_max[n], r_min[n];
for (int i = 0; i < n; i++) {
l_max[i] = l_min[i] = r_max[i] = r_min[i] = i;
int c1 = 0, c2 = 0;
int Lmax = 0, Lmin = 0, Rmax = 0, Rmin = 0;
for (int j = i - 1; j >= 0; j--) {
if (a[j] == a[i]) break;
c1 += (a[j] < a[i]), c2 += (a[j] > a[i]);
if (c1 - c2 < Lmax) Lmax = c1 - c2, l_max[i] = j;
if (c1 - c2 < Lmin) Lmin = c1 - c2, l_min[i] = j;
}
int c3 = 0, c4 = 0;
for (int j = i + 1; j < n; j++) {
if (a[j] == a[i]) break;
c3 += (a[j] < a[i]), c4 += (a[j] > a[i]);
if (c3 - c4 > Rmax) Rmax = c3 - c4, r_max[i] = j;
if (c3 - c4 < Rmin) Rmin = c3 - c4, r_min[i] = j;
}
}
vector<int> pos[n + 1];
for (int i = 0; i < n; i++) pos[a[i]].emplace_back(i);
int l = 0, r = n + 1;
while (r - l > 1) {
int m = (l + r) / 2, F = 0;
for (int val = 1; val <= n; val++) {
for (int ind = 0; ind + m - 1 < (int) pos[val].size(); ind++) {
int LL = pos[val][ind], RR = pos[val][ind + m - 1], L = LL, R = RR;
int c1 = 0, c2 = 0;
for (int i = L; i <= R; i++) c1 += (a[i] < val), c2 += (a[i] > val);
int f1 = (L + R) / 2, f2 = (L + R + 1) / 2;
if ((L + c1 <= f1 && f1 <= R - c2) || (L + c1 <= f2 && f2 <= R - c2)) {
F = 1;
break;
}
}
}
if (F == 1) l = m;
else r = m;
}
return l;
}
//int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr), cout.tie(nullptr);
// int N;
// cin >> N;
// vector<int> A(N);
// for (int i = 0; i < N; i++) cin >> A[i];
// cout << sequence(N, A);
// return 0;
//}
Compilation message
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:6:9: warning: variable 'l_max' set but not used [-Wunused-but-set-variable]
6 | int l_max[n], l_min[n], r_max[n], r_min[n];
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2100 ms |
12124 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
76 ms |
26880 KB |
Output is correct |
3 |
Execution timed out |
2021 ms |
27076 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2065 ms |
12112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |