#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5+10;
int a[N], b[N], cnt[N], cc=0, n, k, r;
void add(int x) {
++cnt[x];
if (cnt[x] == b[x]) ++cc;
}
void rem(int x) {
--cnt[x];
if (cnt[x] == b[x]-1) --cc;
}
void solve() {
cin>>n>>k>>r;
for (int i=1; i<=n; ++i) cin>>a[i], ++a[i];
for (int i=1; i<=r; ++i) {
int k, x; cin>>k>>x; ++k;
b[k]=x;
}
for (int i=1; i<=k; ++i) {
if (b[i] == 0) ++cc;
}
int ans=n+1, r=1;
while (r <= n && cc < k) add(a[r++]);
assert(cc <= k);
if (cc < k) {
cout<<"impossible\n";
return;
}
ans=r-1;
for (int l=1; l<=n; ++l) {
if (cc == k) ans=min(ans, r-l);
rem(a[l]);
while (r <= n && cc < k) add(a[r++]);
} cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2524 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2444 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2392 KB |
Output is correct |
2 |
Correct |
10 ms |
2396 KB |
Output is correct |
3 |
Correct |
10 ms |
2436 KB |
Output is correct |
4 |
Correct |
9 ms |
2504 KB |
Output is correct |
5 |
Correct |
12 ms |
2652 KB |
Output is correct |
6 |
Correct |
8 ms |
2396 KB |
Output is correct |
7 |
Correct |
9 ms |
2396 KB |
Output is correct |
8 |
Correct |
14 ms |
2776 KB |
Output is correct |
9 |
Correct |
11 ms |
2652 KB |
Output is correct |
10 |
Correct |
14 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2624 KB |
Output is correct |
2 |
Correct |
29 ms |
2772 KB |
Output is correct |
3 |
Correct |
16 ms |
2652 KB |
Output is correct |
4 |
Correct |
9 ms |
2396 KB |
Output is correct |
5 |
Correct |
21 ms |
2628 KB |
Output is correct |
6 |
Correct |
29 ms |
2640 KB |
Output is correct |
7 |
Correct |
18 ms |
2652 KB |
Output is correct |
8 |
Correct |
15 ms |
2652 KB |
Output is correct |
9 |
Correct |
8 ms |
2516 KB |
Output is correct |
10 |
Correct |
10 ms |
2396 KB |
Output is correct |
11 |
Correct |
13 ms |
2512 KB |
Output is correct |
12 |
Correct |
9 ms |
2396 KB |
Output is correct |
13 |
Correct |
13 ms |
2652 KB |
Output is correct |
14 |
Correct |
8 ms |
2512 KB |
Output is correct |
15 |
Correct |
9 ms |
2396 KB |
Output is correct |
16 |
Correct |
16 ms |
2592 KB |
Output is correct |
17 |
Correct |
12 ms |
2512 KB |
Output is correct |
18 |
Correct |
9 ms |
2520 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |