#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update> ordset;
#pragma GCC optimize("-O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-Os")
ll INF = 2e18;
ll mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll binpow(ll n, ll m){
if(m == 0) return 1ll;
if(m & 1ll) return n * binpow(n, m / 2ll);
ll b = binpow(n, m / 2ll);
return b*b;
}
void solve(){
int n, k, q;
cin >> n >> k >> q;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
vector<int> cnt(k, 0), need(k, 0);
for(int i = 0; i < q; i++){
int b, c;
cin >> b >> c;
need[b] = c;
}
int now = 0;
for(int i = 0; i < k; i++) now += (need[i] == cnt[i]);
int r = -1;
auto add = [&](int x){
if(cnt[x] == need[x] - 1) now++;
cnt[x]++;
};
auto del = [&](int x){
if(cnt[x] == need[x]) now--;
cnt[x]--;
};
int ans = n + 1;
for(int l = 0; l < n; l++){
if(r < l){
r = l;
add(a[r]);
}
while(now < k && r + 1 < n){
add(a[r + 1]);
r++;
}
if(now == k) ans = min(r - l + 1, ans);
del(a[l]);
}
if(ans == n + 1) cout << "impossible";
else cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
int tt;
//cin >> tt;
tt = 1;
while (tt--) {
solve();
//solve();
#ifdef LOCAL
cout << "__________________________________" << endl;
#endif
}
#ifdef LOCAL
cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << "sec" << '\n';
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1488 KB |
Output is correct |
2 |
Correct |
18 ms |
1492 KB |
Output is correct |
3 |
Correct |
14 ms |
1496 KB |
Output is correct |
4 |
Correct |
13 ms |
1492 KB |
Output is correct |
5 |
Correct |
19 ms |
2900 KB |
Output is correct |
6 |
Correct |
12 ms |
1492 KB |
Output is correct |
7 |
Correct |
13 ms |
1620 KB |
Output is correct |
8 |
Correct |
21 ms |
3916 KB |
Output is correct |
9 |
Correct |
16 ms |
2128 KB |
Output is correct |
10 |
Correct |
12 ms |
1488 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
3404 KB |
Output is correct |
2 |
Correct |
29 ms |
3156 KB |
Output is correct |
3 |
Correct |
21 ms |
2772 KB |
Output is correct |
4 |
Correct |
12 ms |
1492 KB |
Output is correct |
5 |
Correct |
32 ms |
4040 KB |
Output is correct |
6 |
Correct |
33 ms |
4588 KB |
Output is correct |
7 |
Correct |
18 ms |
2108 KB |
Output is correct |
8 |
Correct |
23 ms |
2380 KB |
Output is correct |
9 |
Correct |
13 ms |
1492 KB |
Output is correct |
10 |
Correct |
15 ms |
1484 KB |
Output is correct |
11 |
Correct |
16 ms |
1492 KB |
Output is correct |
12 |
Correct |
13 ms |
1492 KB |
Output is correct |
13 |
Correct |
17 ms |
2900 KB |
Output is correct |
14 |
Correct |
12 ms |
1492 KB |
Output is correct |
15 |
Correct |
14 ms |
1616 KB |
Output is correct |
16 |
Correct |
21 ms |
3916 KB |
Output is correct |
17 |
Correct |
16 ms |
2128 KB |
Output is correct |
18 |
Correct |
12 ms |
1492 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
316 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |