#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 5e5 + 10;
int cnt[N], lmin[N], lmax[N], rmin[N], rmax[N], sum[N];
vector<int> pos[N];
bool check(int lx, int rx, int ly, int ry) {
return !(rx < ly || ry < lx);
}
int sequence(int n, vector<int> a) {
for(int i = 0; i < n; i++) pos[a[i]].push_back(i);
int res = 0;
for(int val = 1; val <= n; val++) {
sum[0] = 0;
for(int i = 0; i < n; i++) {
if (i > 0) {
lmin[i] = lmin[i - 1];
lmax[i] = lmax[i - 1];
}
if (i > 0) sum[i] = sum[i - 1];
if (a[i] <= val) sum[i]--;
else sum[i]++;
lmin[i] = min(lmin[i], sum[i]);
lmax[i] = max(lmax[i], sum[i]);
}
rmin[n] = 1e9; rmax[n] = 0;
for(int i = n - 1; i >= 0; i--) {
rmin[i] = min(rmin[i + 1], sum[i]);
rmax[i] = max(rmax[i + 1], sum[i]);
}
// for(int i = 0; i < n; i++) cout << sum[i] << " "; cout << endl;
// cout << lmin[0] << " " << lmax[0] << endl;
// cout << rmin[n - 1] << " " << rmax[n - 1] << endl;
int j = 0;
for(int i = 0; i < pos[val].size(); i++) {
while (!check(lmin[pos[val][j]] - 1, lmax[pos[val][j]], rmin[pos[val][i]], rmax[pos[val][i]])) j++;
res = max(res, i - j + 1);
// if (val == 1 && i == 1) {
// for(int i = 0; i < n; i++) cout << sum[i] << " "; cout << endl;
// cout << lmin[pos[val][j]] << " " << lmax[pos[val][j]] << endl;
// cout << rmin[pos[val][i]] << " " << rmax[pos[val][i]] << endl;
// }
}
}
// cout << endl;
return res;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
// int n; cin >> n;
// vector<int> a(n);
// for(int i = 0; i < n; i++) cin >> a[i];
// cout << sequence(n, a);
// cout << sequence(7, {1, 2, 3, 1, 2, 1, 3}) << endl;
// cout << sequence(9, {1, 1, 2, 3, 4, 3, 2, 1, 1}) << endl;
// cout << sequence(14, {2, 6, 2, 5, 3, 4, 2, 1, 4, 3, 5, 6, 3, 2}) << endl;
}
#endif // ngu
Compilation message
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < pos[val].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12088 KB |
Output is correct |
4 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12088 KB |
Output is correct |
4 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Execution timed out |
2075 ms |
35636 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Execution timed out |
2067 ms |
27880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2058 ms |
41380 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12088 KB |
Output is correct |
4 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12088 KB |
Output is correct |
4 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |