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<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 (stderr)
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++) {
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |