/*
it could have been better :)
it will next time ;)
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e18
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>
const int MOD = 1'000'000'000 + 7;
void setIO(string name = "")
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#ifdef LOCAL
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
if (!name.empty())
{
freopen((name + ".INP").c_str(), "r", stdin);
freopen((name + ".OUT").c_str(), "w", stdout);
}
#endif
}
// START OF DEBUG
#define db(val) "["#val" = "<<(val)<<"] "
#define print_op(...) ostream& operator<<(ostream& out, const __VA_ARGS__& u)
// for printing std::pair
template<class U, class V> print_op(pair<U, V>) {
return out << "(" << u.first << ", " << u.second << ")";
}
// for printing collection
template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator<<(ostream& out, const Con& con) {
out << "{";
for (auto beg = con.begin(), it = beg; it != con.end(); ++it)
out << (it == beg ? "" : ", ") << *it;
return out << "}";
}
// for printing std::tuple
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
if constexpr(i == tuple_size<T>::value) return out << ")";
else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U> print_op(tuple<U...>) {
return print_tuple_utils<0, tuple<U...>>(out, u);
}
// END OF DEBUG
const int MAXN = 2e5;
int n, k, q;
int a[MAXN + 5];
int req[MAXN + 5];
int cnt[MAXN + 5];
void solve()
{
cin >> n >> k >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0; i <= k; i++) req[i] = INF;
for(int i = 1; i <= q; i++) {
int b; cin >> b;
cin >> req[b];
}
int l = 1, r = 1;
set<int> s;
while(r <= n) {
cnt[a[r]]++;
if(cnt[a[r]] >= req[a[r]]) s.insert(a[r]);
if(s.size() == q) break;
r++;
}
int res = r - l + 1;
if(r == n + 1) {
cout << "impossible\n";
return;
}
r++;
while(r <= n) {
cnt[a[r]]++;
if(cnt[a[r]] >= req[a[r]]) s.insert(a[r]);
while(l <= r) {
// cout << l << ' ' << r << ' ' << s.size() << '\n';
// cout << a[l] << '\n';
// cout << db(cnt[a[l]]) << '\n';
cnt[a[l]]--;
// cout << db(cnt[a[l]]) << '\n';
if(cnt[a[l]] < req[a[l]]) s.erase(a[l]);
// cout << a[l] << '\n';
// for(int i = 0; i < k; i++) cout << cnt[i] << ' ' << req[i] << '\n';
if(s.size() == q) l++;
else {
cnt[a[l]]++;
if(cnt[a[l]] >= req[a[l]]) s.insert(a[l]);
break;
}
}
res = min(res, r - l + 1);
l = max(l, r + 1);
r++;
}
cout << res << '\n';
}
signed main()
{
setIO();
int t = 1;
// cin >> t;
while (t--)
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
dna.cpp: In function 'void setIO(std::string)':
dna.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((name + ".INP").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dna.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((name + ".OUT").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |