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 "sequence.h"
#include <bits/stdc++.h>
using namespace std;
struct node {
int s, e, m, mn, mx, lz;
node *l, *r;
node(int _s, int _e) {
s = _s, e = _e, m = (s+e)/2, mn = mx = lz = 0;
if(s != e) {
l = new node(s, m);
r = new node(m+1, e);
}
}
void prop() {
if(lz == 0) return;
mn += lz;
mx += lz;
if(s != e) {
l->lz += lz;
r->lz += lz;
}
lz = 0;
}
void radd(int x, int y, int val) { prop();
if(x <= s && e <= y) {lz += val; return;}
else if(x > m) r->radd(x, y, val);
else if(y <= m) l->radd(x, y, val);
else l->radd(x, y, val), r->radd(x, y, val);
l->prop(); r->prop();
mn = min(l->mn, r->mn);
mx = max(l->mx, r->mx);
}
int rmin(int x, int y) {
prop();
if(x <= s && e <= y) return mn;
else if(x > m) return r->rmin(x, y);
else if(y <= m) return l->rmin(x, y);
else return min(l->rmin(x, y), r->rmin(x, y));
}
int rmax(int x, int y) {
prop();
if(x <= s && e <= y) return mx;
else if(x > m) return r->rmax(x, y);
else if(y <= m) return l->rmax(x, y);
else return max(l->rmax(x, y), r->rmax(x, y));
}
} *root;
int sequence(int n, std::vector<int> a) {
reverse(a.begin(), a.end());
a.push_back(0);
reverse(a.begin(), a.end());
int ans = 0;
vector<int> pos[n+2];
for(int i = 1; i <= n; i++) pos[i].push_back(0);
for(int i = 1; i <= n; i++) pos[a[i]].push_back(i);
for(int i = 1; i <= n; i++) pos[i].push_back(n+1);
root = new node(0, n);
for(int i = 1; i <= n; i++) {
if(a[i] > 1) root->radd(i, n, 1);
}
for(int i = 1; i <= n; i++) { // value i
int l = 1, r = (int)pos[i].size()-1, m;
while(l < r) {
m = (l+r)/2;
bool ok = false;
for(int j = 1; j < pos[i].size()-m; j++) {
int mn1 = root->rmin(pos[i][j-1], pos[i][j]-1);
int mx1 = root->rmax(pos[i][j-1], pos[i][j]-1);
int mn2 = root->rmin(pos[i][j+m-1], pos[i][j+m]-1);
int mx2 = root->rmax(pos[i][j+m-1], pos[i][j+m]-1);
if(m >= mn2-mx1 && m <= mx2-mn1) {
ok = true;
break;
}
}
if(ok) l = m+1;
else r = m;
}
ans = max(ans, l-1);
for(int j : pos[i]) if(j != 0 && j != n+1) root->radd(j, n, -1);
for(int j : pos[i+1]) if(j != 0 && j != n+1) root->radd(j, n, -1);
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:68:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int j = 1; j < pos[i].size()-m; j++) {
| ~~^~~~~~~~~~~~~~~~~
# | 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... |