#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
const int N = (int)5e5 + 10;
vector<int> pos[N];
int A[N];
int n;
struct Node{
int low;
int high;
int lazy;
};
Node T[N * 4 + 512];
void build(int node, int cl, int cr){
T[node].low = T[node].high = T[node].lazy = 0;
if(cl == cr){
T[node].low = T[node].high = cl;
T[node].lazy = 0;
return;
}
int mid = (cl + cr) / 2;
build(node * 2, cl, mid);
build(node * 2 + 1, mid + 1, cr);
T[node].low=min(T[node*2].low, T[node*2+1].low);
T[node].high=max(T[node*2].high,T[node*2+1].high);
}
void push(int node, int cl, int cr){
T[node].high += T[node].lazy;
T[node].low += T[node].lazy;
if(cl != cr){
T[node * 2].lazy += T[node].lazy;
T[node * 2 + 1].lazy += T[node].lazy;
}
T[node].lazy = 0;
}
void update_segm(int node, int cl, int cr, int tl, int tr, int d){
push(node, cl, cr);
if(cr < tl || cl > tr) return;
if(cl >= tl && cr <= tr){
T[node].lazy = d;
push(node, cl, cr);
return;
}
int mid = (cl + cr) / 2;
update_segm(node * 2, cl, mid, tl, tr, d);
update_segm(node * 2 + 1, mid + 1, cr, tl, tr, d);
T[node].low=min(T[node*2].low, T[node*2+1].low);
T[node].high=max(T[node*2].high,T[node*2+1].high);
}
pii get_val(int node, int cl, int cr, int tl, int tr){
push(node, cl, cr);
if(cr < tl || cl > tr) return mp((int)1e9, -(int)1e9);
if(cl >= tl && cr <= tr){
return mp(T[node].low, T[node].high);
}
int mid = (cl + cr) / 2;
pii L = get_val(node * 2, cl, mid, tl, tr);
pii R = get_val(node * 2 + 1, mid + 1, cr, tl, tr);
return mp(min(L.fi, R.fi), max(L.se, R.se));
}
bool check(int cnt){
build(1, 0, n);
int bal;
int l, r;
pii suf;
pii lE;
pii rE;
pii lef;
int take;
for(int cand = 1; cand <= n; cand ++ ){
for(auto x : pos[cand]){
update_segm(1, 0, n, x, n, -1);
}
for(int j = 0 ; j + cnt - 1 < pos[cand].size(); j ++ ){
l = pos[cand][j];
r = pos[cand][j + cnt - 1];
lE = get_val(1, 0, n, l - 1, l - 1);
rE = get_val(1, 0, n, r, r);
bal = rE.fi - lE.fi;
suf = get_val(1, 0, n, r, n);
lef = get_val(1, 0, n, 0, l - 1);
suf.fi -= rE.fi;
suf.se -= rE.se;
lef = mp(-lef.se, -lef.fi);
lef.fi += lE.fi;
lef.se += lE.fi;
bal += lef.fi + suf.fi;
take = max(0, (-cnt) - bal);
if(bal <= cnt && take <= (lef.se - lef.fi) + (suf.se - suf.fi)){
return true;
}
}
for(auto x : pos[cand]){
update_segm(1, 0, n, x, n, -1);
}
}
return false;
}
int sequence(int _n, vector<int> a) {
n = _n;
for(int i = 0 ; i < n; i ++ ){
A[i + 1] = a[i];
pos[A[i + 1]].push_back(i + 1);
}
int l = 1;
for(int ln = 19; ln >= 0 ;ln -- ){
if(l + (1 << ln) <= n && check(l + (1 << ln))){
l += (1 << ln);
}
}
return l;
}
Compilation message
sequence.cpp: In function 'bool check(int)':
sequence.cpp:91:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int j = 0 ; j + cnt - 1 < pos[cand].size(); j ++ ){
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11976 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11976 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11976 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11976 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
17 ms |
12168 KB |
Output is correct |
14 |
Correct |
15 ms |
12116 KB |
Output is correct |
15 |
Correct |
14 ms |
12148 KB |
Output is correct |
16 |
Correct |
15 ms |
12116 KB |
Output is correct |
17 |
Correct |
20 ms |
12144 KB |
Output is correct |
18 |
Correct |
15 ms |
12188 KB |
Output is correct |
19 |
Correct |
17 ms |
12164 KB |
Output is correct |
20 |
Correct |
15 ms |
12160 KB |
Output is correct |
21 |
Correct |
18 ms |
12112 KB |
Output is correct |
22 |
Correct |
19 ms |
12164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Execution timed out |
2066 ms |
40056 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11976 KB |
Output is correct |
2 |
Execution timed out |
2069 ms |
32380 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2066 ms |
45796 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11976 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11976 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
17 ms |
12168 KB |
Output is correct |
14 |
Correct |
15 ms |
12116 KB |
Output is correct |
15 |
Correct |
14 ms |
12148 KB |
Output is correct |
16 |
Correct |
15 ms |
12116 KB |
Output is correct |
17 |
Correct |
20 ms |
12144 KB |
Output is correct |
18 |
Correct |
15 ms |
12188 KB |
Output is correct |
19 |
Correct |
17 ms |
12164 KB |
Output is correct |
20 |
Correct |
15 ms |
12160 KB |
Output is correct |
21 |
Correct |
18 ms |
12112 KB |
Output is correct |
22 |
Correct |
19 ms |
12164 KB |
Output is correct |
23 |
Correct |
909 ms |
17660 KB |
Output is correct |
24 |
Correct |
918 ms |
17660 KB |
Output is correct |
25 |
Correct |
929 ms |
17660 KB |
Output is correct |
26 |
Correct |
783 ms |
16736 KB |
Output is correct |
27 |
Correct |
865 ms |
16748 KB |
Output is correct |
28 |
Correct |
856 ms |
16736 KB |
Output is correct |
29 |
Correct |
703 ms |
16472 KB |
Output is correct |
30 |
Correct |
652 ms |
16540 KB |
Output is correct |
31 |
Correct |
301 ms |
16516 KB |
Output is correct |
32 |
Correct |
787 ms |
18576 KB |
Output is correct |
33 |
Correct |
747 ms |
17492 KB |
Output is correct |
34 |
Incorrect |
889 ms |
17492 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11976 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
11988 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11976 KB |
Output is correct |
9 |
Correct |
6 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
7 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
17 ms |
12168 KB |
Output is correct |
14 |
Correct |
15 ms |
12116 KB |
Output is correct |
15 |
Correct |
14 ms |
12148 KB |
Output is correct |
16 |
Correct |
15 ms |
12116 KB |
Output is correct |
17 |
Correct |
20 ms |
12144 KB |
Output is correct |
18 |
Correct |
15 ms |
12188 KB |
Output is correct |
19 |
Correct |
17 ms |
12164 KB |
Output is correct |
20 |
Correct |
15 ms |
12160 KB |
Output is correct |
21 |
Correct |
18 ms |
12112 KB |
Output is correct |
22 |
Correct |
19 ms |
12164 KB |
Output is correct |
23 |
Execution timed out |
2066 ms |
40056 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |