#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
using namespace std;
void solve(){
int n,k,r; cin >> n >> k >> r;
int count[k]; memset( count, 0, sizeof count );
bool needs[k]; memset( needs, false, sizeof needs );
int a[n]; for( int i = 0; i < n; i++ ){ cin >> a[i]; count[ a[i] ]++; }
map<int,int> required;
set<int> unsatisfied;
for( int i = 0; i < r; i++ ){
int b,k; cin >> b >> k;
required[b] = k;
needs[b] = true;
unsatisfied.insert(b);
}
for( auto i : required ){
if( count[ i.first ] < i.second ){ cout << "impossible" << endl; return; }
}
// reset count
memset( count, 0, sizeof count );
// fill
int ind = 0;
while( !unsatisfied.empty() ){
count[ a[ind] ]++; // add him to list
if( needs[ a[ind] ] ){
if( unsatisfied.find( a[ind] ) != unsatisfied.end() && count[ a[ind] ] >= required[ a[ind] ] ){ unsatisfied.erase( a[ind] ); }
}
ind++;
}
//cout << ind << endl;
int ans = ind;
bool flag = false;
for( int rS = 0; rS < n-1; rS++ ){
count[ a[rS] ]--;
if( needs[ a[rS] ] ){
if( count[ a[rS] ] < required[ a[rS] ] ){ unsatisfied.insert( a[rS] ); }
}
while( !unsatisfied.empty() ){
if( ind == n ){ flag = true; break; }
count[ a[ind] ]++; // add him to list
if( needs[ a[ind] ] ){
if( unsatisfied.find( a[ind] ) != unsatisfied.end() && count[ a[ind] ] >= required[ a[ind] ] ){ unsatisfied.erase( a[ind] ); }
}
ind++;
}
if( flag ){ break; }
ans = min( ans, ind-rS-1 );
}
cout << ans << endl;
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) { solve(); }
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
324 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
1484 KB |
Output is correct |
2 |
Correct |
17 ms |
1496 KB |
Output is correct |
3 |
Correct |
22 ms |
1488 KB |
Output is correct |
4 |
Correct |
19 ms |
1492 KB |
Output is correct |
5 |
Correct |
18 ms |
2620 KB |
Output is correct |
6 |
Correct |
17 ms |
1468 KB |
Output is correct |
7 |
Correct |
13 ms |
1640 KB |
Output is correct |
8 |
Correct |
21 ms |
3264 KB |
Output is correct |
9 |
Correct |
19 ms |
2124 KB |
Output is correct |
10 |
Correct |
18 ms |
1484 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
356 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
10744 KB |
Output is correct |
2 |
Correct |
137 ms |
8532 KB |
Output is correct |
3 |
Correct |
51 ms |
4440 KB |
Output is correct |
4 |
Correct |
18 ms |
1364 KB |
Output is correct |
5 |
Correct |
81 ms |
11164 KB |
Output is correct |
6 |
Correct |
233 ms |
13596 KB |
Output is correct |
7 |
Correct |
44 ms |
2764 KB |
Output is correct |
8 |
Correct |
58 ms |
3776 KB |
Output is correct |
9 |
Correct |
16 ms |
1492 KB |
Output is correct |
10 |
Correct |
20 ms |
1484 KB |
Output is correct |
11 |
Correct |
22 ms |
1512 KB |
Output is correct |
12 |
Correct |
19 ms |
1468 KB |
Output is correct |
13 |
Correct |
25 ms |
2516 KB |
Output is correct |
14 |
Correct |
18 ms |
1488 KB |
Output is correct |
15 |
Correct |
18 ms |
1592 KB |
Output is correct |
16 |
Correct |
22 ms |
3208 KB |
Output is correct |
17 |
Correct |
17 ms |
2068 KB |
Output is correct |
18 |
Correct |
19 ms |
1488 KB |
Output is correct |
19 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
332 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 |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
340 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 |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
232 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |