#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
void solve() {
int n, m, q;
cin >> n >> m >> q;
int arr[n];
for (auto& a : arr) {
cin >> a;
}
int req[m] {};
while (q--) {
int a, b;
cin >> a >> b;
req[a] = max(req[a], b);
}
int nxtv[m];
memset(nxtv, -1, sizeof(nxtv));
int nxt[n];
for (int i = n - 1; i >= 0; i--) {
nxt[i] = nxtv[arr[i]];
nxtv[arr[i]] = i;
}
int ptr[m], cans = 0;
for (int i = 0; i < m; i++) {
if (!req[i]) {
continue;
}
int j = nxtv[i];
for (int k = 1; k < req[i]; k++) {
j = j == -1 ? -1 : nxt[j];
}
if (j == -1) {
cout << "impossible" << endl;
return;
}
ptr[i] = j;
cans = max(cans, j);
}
int ans = cans + 1;
for (int i = 0; i < n; i++) {
if (req[arr[i]]) {
ptr[arr[i]] = nxt[ptr[arr[i]]];
if (ptr[arr[i]] == -1) {
break;
}
cans = max(cans, ptr[arr[i]]);
}
ans = min(ans, cans - i);
}
cout << ans << endl;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin.exceptions(ios::failbit);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 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 |
1 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 |
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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
0 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
2264 KB |
Output is correct |
2 |
Correct |
13 ms |
2268 KB |
Output is correct |
3 |
Correct |
13 ms |
2232 KB |
Output is correct |
4 |
Correct |
14 ms |
2276 KB |
Output is correct |
5 |
Correct |
17 ms |
3924 KB |
Output is correct |
6 |
Correct |
13 ms |
2260 KB |
Output is correct |
7 |
Correct |
14 ms |
2388 KB |
Output is correct |
8 |
Correct |
19 ms |
5436 KB |
Output is correct |
9 |
Correct |
16 ms |
2880 KB |
Output is correct |
10 |
Correct |
12 ms |
2240 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 |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
348 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 |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
276 KB |
Output is correct |
24 |
Correct |
0 ms |
316 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4556 KB |
Output is correct |
2 |
Correct |
25 ms |
4152 KB |
Output is correct |
3 |
Correct |
20 ms |
3768 KB |
Output is correct |
4 |
Correct |
13 ms |
2240 KB |
Output is correct |
5 |
Correct |
30 ms |
5172 KB |
Output is correct |
6 |
Correct |
34 ms |
5964 KB |
Output is correct |
7 |
Correct |
18 ms |
2872 KB |
Output is correct |
8 |
Correct |
19 ms |
3244 KB |
Output is correct |
9 |
Correct |
13 ms |
2264 KB |
Output is correct |
10 |
Correct |
13 ms |
2244 KB |
Output is correct |
11 |
Correct |
14 ms |
2260 KB |
Output is correct |
12 |
Correct |
12 ms |
2260 KB |
Output is correct |
13 |
Correct |
17 ms |
3900 KB |
Output is correct |
14 |
Correct |
12 ms |
2256 KB |
Output is correct |
15 |
Correct |
13 ms |
2388 KB |
Output is correct |
16 |
Correct |
20 ms |
5440 KB |
Output is correct |
17 |
Correct |
16 ms |
2928 KB |
Output is correct |
18 |
Correct |
12 ms |
2252 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
316 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |