-
Notifications
You must be signed in to change notification settings - Fork 3
/
a.cpp
59 lines (52 loc) · 1.27 KB
/
a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @date 2022-06-24
* @tags binary search, moving windows
* @difficulty 1600
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL); // Fast IO
int N,K,R;
cin >> N >> K >> R;
vector<int> dna(N);
using Freq = unordered_map<int,int>;
Freq req;
for (int& i : dna) cin >> i;
for (int i = 0, b, q; i < R; ++i) {
cin >> b >> q;
req[b] = q;
}
auto incr = [&](Freq& freq, int k, int& cnt) {
++freq[k];
if (req.count(k) && freq[k] == req[k]) ++cnt;
};
auto decr = [&](Freq& freq, int k, int& cnt) {
if (req.count(k) && freq[k] == req[k]) --cnt;
--freq[k];
};
auto possible = [&](int len) {
Freq freq;
int cnt = 0;
for (int i = 0; i < len; ++i) incr(freq, dna[i], cnt);
if (cnt == req.size()) return true;
for (int i = len; i < N; ++i) {
decr(freq, dna[i-len], cnt);
incr(freq, dna[i], cnt);
if (cnt == req.size()) return true;
}
return false;
};
int lo = 0, hi = N;
while (lo <= hi) {
int mid = (lo+hi)/2;
if (possible(mid)) hi = mid - 1;
else lo = mid + 1;
}
#ifdef DEBUG
cerr << lo << ' ' << hi << endl;
#endif
if (lo-1 >= N) cout << "impossible" << endl;
else cout << (hi+1) << endl;
return 0;
}