#include <bits/stdc++.h>
#include "sequence.h"
using namespace std;
const int N = 5e5 + 1;
typedef long long ll;
int mn[N * 4],mx[N * 4],add[N * 4],n;
void inc(int v,int val){
add[v] += val;
mn[v] += val;
mx[v] += val;
}
void push(int v){
if(add[v]){
inc(v + v,add[v]);
inc(v + v +1,add[v]);
add[v] = 0;
}
}
void upd(int l,int r,int val,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr){
return;
}
if(tl >= l && tr <= r){
inc(v,val);
}else{
push(v);
int tm = (tl + tr) >> 1;
upd(l,r,val,v+v,tl,tm);
upd(l,r,val,v+v+1,tm+1,tr);
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
}
}
int get_min(int l,int r,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return 1e9;
if(tl >= l && tr <= r) return mn[v];
push(v);
int tm = (tl + tr) >> 1;
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
return min(get_min(l,r,v+v,tl,tm),get_min(l,r,v+v+1,tm+1,tr));
}
int get_max(int l,int r,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return -1e9;
if(tl >= l && tr <= r) return mx[v];
push(v);
int tm = (tl + tr) >> 1;
mn[v] = min(mn[v + v],mn[v + v + 1]);
mx[v] = max(mx[v + v],mx[v + v + 1]);
return max(get_max(l,r,v+v,tl,tm),get_max(l,r,v+v+1,tm+1,tr));
}
vector<int> pos[N];
bool inter(int l,int r,int l1,int r1){
return (min(r,r1) - max(l,l1) >= 0);
}
int sequence(int nn, std::vector<int> a) {
n = nn;
int mn = 1e9;
for(int i = 0;i < nn;i++){
mn = min(mn,a[i]);
pos[a[i]].push_back(i + 1);
}
for(int i = 1;i <= n;i++){
upd(i,n,1);
}
// cout << get_max(2,n) << "x\n";
// return 0;
int res = 1;
for(int i = 1;i <= n;i++){
for(int j:pos[i - 1]){
upd(j,n,-1);
}
for(int j:pos[i]){
upd(j,n,-1);
}
int it = 0;
for(int j = 0;j < (int)pos[i].size();j++){
while(it < (int)pos[i].size()){
ll mn_sum = get_min(pos[i][it],n) - max(0,get_max(1,pos[i][j] - 1));
ll mx_sum = get_max(pos[i][it],n) - min(0,get_min(1,pos[i][j] - 1));
int len = (it - j + 1);
// cout << pos[i][j] << ' ' << pos[i][it] << ' ' << mx_sum << ' ' << mn_sum << ' ' << inter(mn_sum,mx_sum,-len,len) << '\n';
if(inter(mn_sum,mx_sum,-len,len)){
it++;
}else{
break;
}
}
res = max(res,it - j);
}
}
return res;
}
// int main() {
// int N;
// assert(1 == scanf("%d", &N));
// std::vector<int> A(N);
// for (int i = 0; i < N; ++i) {
// assert(1 == scanf("%d", &A[i]));
// }
// int result = sequence(N, A);
// printf("%d\n", result);
// return 0;
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17024 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17024 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
669 ms |
43344 KB |
Output is correct |
3 |
Correct |
693 ms |
43168 KB |
Output is correct |
4 |
Correct |
651 ms |
35532 KB |
Output is correct |
5 |
Correct |
698 ms |
42228 KB |
Output is correct |
6 |
Correct |
657 ms |
42236 KB |
Output is correct |
7 |
Correct |
624 ms |
35716 KB |
Output is correct |
8 |
Correct |
658 ms |
35864 KB |
Output is correct |
9 |
Correct |
619 ms |
35348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Incorrect |
741 ms |
36464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
844 ms |
48904 KB |
Output is correct |
2 |
Correct |
818 ms |
48724 KB |
Output is correct |
3 |
Correct |
855 ms |
48560 KB |
Output is correct |
4 |
Correct |
853 ms |
48324 KB |
Output is correct |
5 |
Correct |
817 ms |
45228 KB |
Output is correct |
6 |
Correct |
829 ms |
45096 KB |
Output is correct |
7 |
Correct |
729 ms |
43788 KB |
Output is correct |
8 |
Correct |
750 ms |
43348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17024 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Incorrect |
3 ms |
17024 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |