#include "sequence.h"
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,a[5000500];
vector<int> pos[5000500];
struct node{
int mn,mx,tag;
};
node st[2000200];
node mrg(node a,node b){
node res;
res.mn=min(a.mn,b.mn);
res.mx=max(a.mx,b.mx);
return res;
}
void pull(int id){
st[id].mn=min(st[id<<1].mn,st[id<<1|1].mn);
st[id].mx=max(st[id<<1].mx,st[id<<1|1].mx);
}
void push(int id) {
if(!st[id].tag) return;
st[id<<1].mn+=st[id].tag;
st[id<<1].mx+=st[id].tag;
st[id<<1].tag+=st[id].tag;
st[id<<1|1].mn+=st[id].tag;
st[id<<1|1].mx+=st[id].tag;
st[id<<1|1].tag+=st[id].tag;
st[id].tag=0;
}
void build(int id,int l,int r){
if(l==r){
st[id].mn=l;
st[id].mx=l;
st[id].tag=0;
return;
}
int mid=(l+r)/2;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
pull(id);
}
void modify(int id,int l,int r,int ql,int qr,int v){
if(ql<=l&&r<=qr){
st[id].mn+=v;
st[id].mx+=v;
st[id].tag+=v;
push(id);
return;
}
push(id);
int mid=(l+r)/2;
if(qr<=mid) modify(id<<1,l,mid,ql,qr,v);
else if(ql>mid) modify(id<<1|1,mid+1,r,ql,qr,v);
else modify(id<<1,l,mid,ql,qr,v),modify(id<<1|1,mid+1,r,ql,qr,v);
pull(id);
}
node query(int id,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return st[id];
push(id);
int mid=(l+r)/2;
node res;
if(qr<=mid) res=query(id<<1,l,mid,ql,qr);
else if(ql>mid) res=query(id<<1|1,mid+1,r,ql,qr);
else res=mrg(query(id<<1,l,mid,ql,qr),query(id<<1|1,mid+1,r,ql,qr));
pull(id);
return res;
}
int min_diff(int l1,int r1,int l2,int r2){
if(max(l1,l2)<=min(r1,r2)) return 0;
return min(abs(l1-r2),abs(r1-l2));
}
bool good(int l,int r,int c){
node a=query(1,1,n,1,l);
node b=query(1,1,n,r,n);
return min_diff(a.mn,a.mx,b.mn,b.mx)<=c;
}
int sequence(int N,vector<int> A) {
n=N; for(int i=1;i<=n;i++) a[i]=A[i-1];
for(int i=1;i<=n;i++) pos[a[i]].pb(i);
build(1,1,n);
int ans=0;
for(int v=1;v<=n;v++){
if(pos[v].empty()) continue;
for(int j:pos[v]) modify(1,1,n,j,n,-1);
int sz=(int)pos[v].size();
int ptr=0;
for(int l=0;l<sz;l++){
while(ptr<sz&&good(pos[v][l],pos[v][ptr],ptr-l+1)) ptr++;
ans=max(ans,ptr-l);
}
for(int j:pos[v]) modify(1,1,n,j,n,-1);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
119460 KB |
Output is correct |
2 |
Correct |
24 ms |
119356 KB |
Output is correct |
3 |
Correct |
24 ms |
119632 KB |
Output is correct |
4 |
Correct |
25 ms |
119340 KB |
Output is correct |
5 |
Correct |
25 ms |
119444 KB |
Output is correct |
6 |
Incorrect |
25 ms |
119384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
119460 KB |
Output is correct |
2 |
Correct |
24 ms |
119356 KB |
Output is correct |
3 |
Correct |
24 ms |
119632 KB |
Output is correct |
4 |
Correct |
25 ms |
119340 KB |
Output is correct |
5 |
Correct |
25 ms |
119444 KB |
Output is correct |
6 |
Incorrect |
25 ms |
119384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
119460 KB |
Output is correct |
2 |
Runtime error |
213 ms |
302692 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
119356 KB |
Output is correct |
2 |
Runtime error |
224 ms |
305368 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
789 ms |
327036 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
119460 KB |
Output is correct |
2 |
Correct |
24 ms |
119356 KB |
Output is correct |
3 |
Correct |
24 ms |
119632 KB |
Output is correct |
4 |
Correct |
25 ms |
119340 KB |
Output is correct |
5 |
Correct |
25 ms |
119444 KB |
Output is correct |
6 |
Incorrect |
25 ms |
119384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
119460 KB |
Output is correct |
2 |
Correct |
24 ms |
119356 KB |
Output is correct |
3 |
Correct |
24 ms |
119632 KB |
Output is correct |
4 |
Correct |
25 ms |
119340 KB |
Output is correct |
5 |
Correct |
25 ms |
119444 KB |
Output is correct |
6 |
Incorrect |
25 ms |
119384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |