#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, k, r;
const int N = 2e5 + 69;
int a[N], need[N];
deque <int> pos[N];
void Solve()
{
cin >> n >> k >> r;
for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]].push_back(i);
while(r--){
int x, y; cin >> x >> y;
need[x] = y;
if (y > pos[x].size()){
cout << "impossible\n";
return;
}
}
int ans = n;
int mx = -1;
for (int i = 0; i < k; i++){
if (need[i] != 0){
mx = max(mx, pos[i][need[i] - 1]);
}
}
for (int i = 1; i <= n; i++){
ans = min(ans, mx - i + 1);
pos[a[i]].pop_front();
if (pos[a[i]].size() < need[a[i]]) break;
if (need[a[i]] != 0) mx = max(mx, pos[a[i]][need[a[i]] - 1]);
}
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
dna.cpp: In function 'void Solve()':
dna.cpp:23:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if (y > pos[x].size()){
| ~~^~~~~~~~~~~~~~~
dna.cpp:40:30: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
40 | if (pos[a[i]].size() < need[a[i]]) break;
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
134996 KB |
Output is correct |
2 |
Correct |
64 ms |
134984 KB |
Output is correct |
3 |
Correct |
63 ms |
134920 KB |
Output is correct |
4 |
Correct |
62 ms |
134912 KB |
Output is correct |
5 |
Correct |
64 ms |
134972 KB |
Output is correct |
6 |
Correct |
64 ms |
134896 KB |
Output is correct |
7 |
Correct |
64 ms |
134996 KB |
Output is correct |
8 |
Correct |
63 ms |
134892 KB |
Output is correct |
9 |
Correct |
64 ms |
135004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
135064 KB |
Output is correct |
2 |
Correct |
74 ms |
135064 KB |
Output is correct |
3 |
Correct |
66 ms |
134988 KB |
Output is correct |
4 |
Correct |
66 ms |
135020 KB |
Output is correct |
5 |
Correct |
68 ms |
134980 KB |
Output is correct |
6 |
Correct |
80 ms |
135036 KB |
Output is correct |
7 |
Correct |
68 ms |
134964 KB |
Output is correct |
8 |
Correct |
65 ms |
134964 KB |
Output is correct |
9 |
Correct |
67 ms |
134992 KB |
Output is correct |
10 |
Correct |
65 ms |
134984 KB |
Output is correct |
11 |
Correct |
65 ms |
134968 KB |
Output is correct |
12 |
Correct |
65 ms |
134924 KB |
Output is correct |
13 |
Correct |
67 ms |
135024 KB |
Output is correct |
14 |
Correct |
66 ms |
134992 KB |
Output is correct |
15 |
Correct |
66 ms |
135008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
138640 KB |
Output is correct |
2 |
Correct |
111 ms |
138620 KB |
Output is correct |
3 |
Correct |
84 ms |
138616 KB |
Output is correct |
4 |
Correct |
81 ms |
138572 KB |
Output is correct |
5 |
Correct |
114 ms |
137928 KB |
Output is correct |
6 |
Correct |
82 ms |
138640 KB |
Output is correct |
7 |
Correct |
82 ms |
138796 KB |
Output is correct |
8 |
Correct |
132 ms |
137748 KB |
Output is correct |
9 |
Correct |
83 ms |
137460 KB |
Output is correct |
10 |
Correct |
84 ms |
138572 KB |
Output is correct |
11 |
Correct |
74 ms |
134992 KB |
Output is correct |
12 |
Correct |
84 ms |
134964 KB |
Output is correct |
13 |
Correct |
68 ms |
135016 KB |
Output is correct |
14 |
Correct |
68 ms |
135008 KB |
Output is correct |
15 |
Correct |
65 ms |
134952 KB |
Output is correct |
16 |
Correct |
76 ms |
135048 KB |
Output is correct |
17 |
Correct |
65 ms |
134920 KB |
Output is correct |
18 |
Correct |
65 ms |
134992 KB |
Output is correct |
19 |
Correct |
66 ms |
134908 KB |
Output is correct |
20 |
Correct |
66 ms |
134916 KB |
Output is correct |
21 |
Correct |
75 ms |
134988 KB |
Output is correct |
22 |
Correct |
65 ms |
134988 KB |
Output is correct |
23 |
Correct |
65 ms |
134988 KB |
Output is correct |
24 |
Correct |
64 ms |
134988 KB |
Output is correct |
25 |
Correct |
71 ms |
134936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
138856 KB |
Output is correct |
2 |
Correct |
120 ms |
138796 KB |
Output is correct |
3 |
Correct |
118 ms |
138444 KB |
Output is correct |
4 |
Correct |
83 ms |
138572 KB |
Output is correct |
5 |
Correct |
109 ms |
137856 KB |
Output is correct |
6 |
Correct |
151 ms |
140036 KB |
Output is correct |
7 |
Correct |
80 ms |
137548 KB |
Output is correct |
8 |
Correct |
96 ms |
137984 KB |
Output is correct |
9 |
Correct |
95 ms |
138708 KB |
Output is correct |
10 |
Correct |
84 ms |
138612 KB |
Output is correct |
11 |
Correct |
83 ms |
138660 KB |
Output is correct |
12 |
Correct |
83 ms |
138556 KB |
Output is correct |
13 |
Correct |
116 ms |
137880 KB |
Output is correct |
14 |
Correct |
88 ms |
138676 KB |
Output is correct |
15 |
Correct |
83 ms |
138760 KB |
Output is correct |
16 |
Correct |
134 ms |
137800 KB |
Output is correct |
17 |
Correct |
82 ms |
137536 KB |
Output is correct |
18 |
Correct |
96 ms |
138640 KB |
Output is correct |
19 |
Correct |
73 ms |
135000 KB |
Output is correct |
20 |
Correct |
72 ms |
135044 KB |
Output is correct |
21 |
Correct |
85 ms |
135060 KB |
Output is correct |
22 |
Correct |
67 ms |
135008 KB |
Output is correct |
23 |
Correct |
68 ms |
135012 KB |
Output is correct |
24 |
Correct |
74 ms |
134980 KB |
Output is correct |
25 |
Correct |
77 ms |
134960 KB |
Output is correct |
26 |
Correct |
79 ms |
134976 KB |
Output is correct |
27 |
Correct |
67 ms |
134996 KB |
Output is correct |
28 |
Correct |
71 ms |
134928 KB |
Output is correct |
29 |
Correct |
67 ms |
134956 KB |
Output is correct |
30 |
Correct |
66 ms |
134964 KB |
Output is correct |
31 |
Correct |
66 ms |
134892 KB |
Output is correct |
32 |
Correct |
67 ms |
134960 KB |
Output is correct |
33 |
Correct |
68 ms |
134992 KB |
Output is correct |