#include<bits/stdc++.h>
using namespace std;
#ifdef ONPC
#include"debug.h"
#else
#define debug(...) 42
#endif
#define endl '\n'
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 15;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, r;
cin >> n >> k >> r;
vector<int> a(n), frq(k), needs(k);
for (int i = 0; i < n; i++){
cin >> a[i];
}
set<int> active;
for (int i = 0; i < r; i++){
int u, v;
cin >> u >> v;
needs[u] = v;
active.insert(u);
}
int p1 = 0, p2 = 0;
int ans = inf;
while (p1 < n){
while (p2 < n && sz(active)){
frq[a[p2]]++;
if (frq[a[p2]] == needs[a[p2]]) active.erase(a[p2]);
p2++;
}
if (sz(active) == 0){
ckmin(ans, p2 - p1);
}
if (frq[a[p1]] == needs[a[p1]]){
active.insert(a[p1]);
}
frq[a[p1]]--;
p1++;
}
if (ans != inf)
cout << ans << endl;
else
cout << "impossible\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
412 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1116 KB |
Output is correct |
2 |
Correct |
11 ms |
1116 KB |
Output is correct |
3 |
Correct |
12 ms |
1116 KB |
Output is correct |
4 |
Correct |
11 ms |
1116 KB |
Output is correct |
5 |
Correct |
12 ms |
1880 KB |
Output is correct |
6 |
Correct |
8 ms |
1112 KB |
Output is correct |
7 |
Correct |
6 ms |
1112 KB |
Output is correct |
8 |
Correct |
11 ms |
2652 KB |
Output is correct |
9 |
Correct |
10 ms |
1116 KB |
Output is correct |
10 |
Correct |
8 ms |
1116 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 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 |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
5616 KB |
Output is correct |
2 |
Correct |
56 ms |
4380 KB |
Output is correct |
3 |
Correct |
24 ms |
2652 KB |
Output is correct |
4 |
Correct |
11 ms |
1112 KB |
Output is correct |
5 |
Correct |
37 ms |
5724 KB |
Output is correct |
6 |
Correct |
86 ms |
7200 KB |
Output is correct |
7 |
Correct |
14 ms |
1624 KB |
Output is correct |
8 |
Correct |
20 ms |
1924 KB |
Output is correct |
9 |
Correct |
9 ms |
1116 KB |
Output is correct |
10 |
Correct |
11 ms |
1116 KB |
Output is correct |
11 |
Correct |
12 ms |
1116 KB |
Output is correct |
12 |
Correct |
14 ms |
1116 KB |
Output is correct |
13 |
Correct |
14 ms |
1884 KB |
Output is correct |
14 |
Correct |
8 ms |
1112 KB |
Output is correct |
15 |
Correct |
8 ms |
1116 KB |
Output is correct |
16 |
Correct |
14 ms |
2824 KB |
Output is correct |
17 |
Correct |
10 ms |
1116 KB |
Output is correct |
18 |
Correct |
10 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |