#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define F first
#define S second
const int N = 200000 + 5;
const int LOG = 30;
int n, k, R, a[N];
vector <int> ids[N];
pair <int, int> p[N];
int pntl[N], pntr[N];
vector <int> all[N];
int cnt_ok;
inline int get(int id){
int ret = 0;
int x = p[id].F;
int sz = ids[x].size();
if (pntl[id] < sz){
ret = pntr[id] - pntl[id];
}
return ret;
}
inline void update_l(int z){
for (int it : all[z]){
int bef = get(it);
pntl[it] ++;
int aft = get(it);
if (p[it].S <= bef){
cnt_ok --;
}
if (p[it].S <= aft){
cnt_ok ++;
}
}
return;
}
inline void update_r(int z){
for (int it : all[z]){
int bef = get(it);
pntr[it] ++;
int aft = get(it);
if (p[it].S <= bef){
cnt_ok --;
}
if (p[it].S <= aft){
cnt_ok ++;
}
}
return;
}
inline void pre(){
for (int i = 0; i < R; i ++){
int x = p[i].F;
int sz = ids[x].size();
for (int j = 0; j < sz; j ++){
all[ids[x][j]].pb(i);
}
}
return;
}
bool ok(int len){
if (n < len) return 0;
int strt = 1;
for (int i = 0; i < R; i ++){
int x = p[i].F;
if (ids[x].empty()){
return 0;
}
strt = max(strt, ids[x][0] - len + 1);
pntl[i] = pntr[i] = 0;
}
cnt_ok = 0;
for (int i = 0; i < R; i ++){
int x = p[i].F;
int y = p[i].S;
int sz = ids[x].size();
while (pntl[i] < sz && ids[x][pntl[i]] < strt){
pntl[i] ++;
}
while (pntr[i] < sz && ids[x][pntr[i]] <= strt + len - 1){
pntr[i] ++;
}
if (y <= get(i)){
cnt_ok ++;
}
}
if (cnt_ok == R){
return 1;
}
for (int l = strt + 1; l + len - 1 <= n; l ++){
int r = l + len - 1;
update_l(l - 1);
update_r(r);
if (cnt_ok == R){
return 1;
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0); cout.tie(0);
cin >> n >> k >> R;
for (int i = 1; i <= n; i ++){
cin >> a[i];
ids[a[i]].pb(i);
}
for (int i = 0; i < R; i ++){
cin >> p[i].F >> p[i].S;
}
pre();
int l = 0, r = n + 1;
while (l + 1 < r){
int mid = (l + r) / 2;
if (ok(mid)){
r = mid;
}
else {
l = mid;
}
}
if (r == n + 1){
cout << "impossible\n";
}
else {
cout << r << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9836 KB |
Output is correct |
2 |
Correct |
8 ms |
9964 KB |
Output is correct |
3 |
Correct |
8 ms |
9836 KB |
Output is correct |
4 |
Correct |
8 ms |
9836 KB |
Output is correct |
5 |
Correct |
8 ms |
9836 KB |
Output is correct |
6 |
Correct |
8 ms |
9964 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
17132 KB |
Output is correct |
2 |
Correct |
63 ms |
16292 KB |
Output is correct |
3 |
Correct |
50 ms |
16360 KB |
Output is correct |
4 |
Correct |
77 ms |
17220 KB |
Output is correct |
5 |
Correct |
47 ms |
13164 KB |
Output is correct |
6 |
Correct |
46 ms |
17924 KB |
Output is correct |
7 |
Correct |
26 ms |
12012 KB |
Output is correct |
8 |
Correct |
68 ms |
16748 KB |
Output is correct |
9 |
Correct |
35 ms |
11884 KB |
Output is correct |
10 |
Correct |
90 ms |
17804 KB |
Output is correct |
11 |
Correct |
8 ms |
9836 KB |
Output is correct |
12 |
Correct |
9 ms |
9964 KB |
Output is correct |
13 |
Correct |
8 ms |
9836 KB |
Output is correct |
14 |
Correct |
8 ms |
9836 KB |
Output is correct |
15 |
Correct |
7 ms |
9836 KB |
Output is correct |
16 |
Correct |
8 ms |
9964 KB |
Output is correct |
17 |
Correct |
7 ms |
9708 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
7 ms |
9708 KB |
Output is correct |
22 |
Correct |
7 ms |
9708 KB |
Output is correct |
23 |
Correct |
7 ms |
9708 KB |
Output is correct |
24 |
Correct |
7 ms |
9708 KB |
Output is correct |
25 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
20716 KB |
Output is correct |
2 |
Correct |
347 ms |
19308 KB |
Output is correct |
3 |
Correct |
109 ms |
14700 KB |
Output is correct |
4 |
Correct |
35 ms |
16900 KB |
Output is correct |
5 |
Correct |
180 ms |
19948 KB |
Output is correct |
6 |
Correct |
256 ms |
21484 KB |
Output is correct |
7 |
Correct |
138 ms |
17900 KB |
Output is correct |
8 |
Correct |
141 ms |
17260 KB |
Output is correct |
9 |
Correct |
66 ms |
17132 KB |
Output is correct |
10 |
Correct |
63 ms |
16292 KB |
Output is correct |
11 |
Correct |
50 ms |
16360 KB |
Output is correct |
12 |
Correct |
67 ms |
17220 KB |
Output is correct |
13 |
Correct |
45 ms |
13164 KB |
Output is correct |
14 |
Correct |
48 ms |
17900 KB |
Output is correct |
15 |
Correct |
27 ms |
12012 KB |
Output is correct |
16 |
Correct |
68 ms |
16876 KB |
Output is correct |
17 |
Correct |
35 ms |
11884 KB |
Output is correct |
18 |
Correct |
100 ms |
17804 KB |
Output is correct |
19 |
Correct |
8 ms |
9836 KB |
Output is correct |
20 |
Correct |
8 ms |
9964 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
9 ms |
9836 KB |
Output is correct |
23 |
Correct |
7 ms |
9836 KB |
Output is correct |
24 |
Correct |
8 ms |
9964 KB |
Output is correct |
25 |
Correct |
8 ms |
9708 KB |
Output is correct |
26 |
Correct |
8 ms |
9708 KB |
Output is correct |
27 |
Correct |
7 ms |
9708 KB |
Output is correct |
28 |
Correct |
7 ms |
9708 KB |
Output is correct |
29 |
Correct |
7 ms |
9708 KB |
Output is correct |
30 |
Correct |
7 ms |
9708 KB |
Output is correct |
31 |
Correct |
7 ms |
9708 KB |
Output is correct |
32 |
Correct |
7 ms |
9708 KB |
Output is correct |
33 |
Correct |
7 ms |
9708 KB |
Output is correct |