#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define endl "\n"
void printVector(vector<int> a){
for (auto x: a) cout << x << " ";
cout << endl;
}
void solve(){
int n, k, r; cin >> n >> k >> r;
vector<int> d(n);
FOR(i,0,n) cin >> d[i];
vector<vector<int>> o(k);
FOR(i,0,n) o[d[i]].pb(i);
vector<int> e(k+1);
FOR(i,0,r){
int x, y; cin >> x >> y;
e[x] = y;
}
vector<int> start(n);
vector<int> end(n);
vector<int> prev(n);
vector<int> next(n);
FOR(i,0,k){
int h = o[i].size();
if (e[i] == 0) continue;
int l = 0; int j = e[i]-1;
while (j < h){
start[o[i][l]] = 1;
end[o[i][j]] = 1;
prev[o[i][j]] = o[i][l];
next[o[i][l]] = o[i][j];
l++;
j++;
}
}
// printVector(start);
// printVector(end);
int first = 0; int second = n;
int count = 0;
int answer = n+1;
while (first <= second){
int mid = (first+second+1)/2;
int l = 0; int j = mid-1;
bool can = false;
// vector<int> s(k), l(k);
vector<int> current(k);
int active = 0;
FOR(i,l,j+1){
if (end[i]){
if (prev[i] >= l){
current[d[i]]++;
if (current[d[i]] == 1) active++;
}
}
if (active >= r){
l = mid;
can = true;
}
}
while (j < n){
if (start[l] && next[l] <= j){
current[d[l]]--;
if (current[d[l]] == 0) active--;
}
l++;
j++;
if (end[j] && prev[j] >= l){
current[d[j]]++;
if (current[d[j]] == 1) active++;
}
if (active >= r) can = true;
}
// cout << mid << can << endl;
if (can){
second = mid;
answer = min(answer, mid);
}else{
first = mid;
}
if (second-first <= 1){
count++;
if (count > 2){
first = second;
}
}
if (first == second) break;
// cout << first << second << endl;
// if ()break;
}
if (answer > n) cout << "impossible" << endl;
else cout << answer << endl;
}
int32_t main(){
ios::sync_with_stdio(false);cin.tie(0);
int t = 1;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
756 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
420 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
420 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5936 KB |
Output is correct |
2 |
Correct |
25 ms |
5828 KB |
Output is correct |
3 |
Correct |
25 ms |
5844 KB |
Output is correct |
4 |
Correct |
19 ms |
6060 KB |
Output is correct |
5 |
Correct |
25 ms |
10588 KB |
Output is correct |
6 |
Correct |
14 ms |
5980 KB |
Output is correct |
7 |
Correct |
17 ms |
5844 KB |
Output is correct |
8 |
Correct |
34 ms |
18004 KB |
Output is correct |
9 |
Correct |
21 ms |
6840 KB |
Output is correct |
10 |
Correct |
35 ms |
5872 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
856 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
11088 KB |
Output is correct |
2 |
Correct |
41 ms |
9940 KB |
Output is correct |
3 |
Correct |
44 ms |
9584 KB |
Output is correct |
4 |
Correct |
19 ms |
5620 KB |
Output is correct |
5 |
Correct |
37 ms |
12744 KB |
Output is correct |
6 |
Correct |
54 ms |
17556 KB |
Output is correct |
7 |
Correct |
34 ms |
6740 KB |
Output is correct |
8 |
Correct |
49 ms |
7828 KB |
Output is correct |
9 |
Correct |
16 ms |
5924 KB |
Output is correct |
10 |
Correct |
24 ms |
5652 KB |
Output is correct |
11 |
Correct |
26 ms |
5592 KB |
Output is correct |
12 |
Correct |
24 ms |
6184 KB |
Output is correct |
13 |
Correct |
25 ms |
10580 KB |
Output is correct |
14 |
Correct |
13 ms |
6244 KB |
Output is correct |
15 |
Correct |
18 ms |
5980 KB |
Output is correct |
16 |
Correct |
34 ms |
18128 KB |
Output is correct |
17 |
Correct |
22 ms |
6748 KB |
Output is correct |
18 |
Correct |
28 ms |
5880 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |