This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);
#define what(x) cerr << #x << " is " << x << '\n';
#define kill(x) {cout << x << '\n'; return 0;}
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ll long long
#define F first
#define S second
const ll inf = 1e18, mod = 1e9 + 7, delta = 1e9 + 9, SQ = 450, P = 6065621;
using namespace std;
const ll N = 2e5 + 10, LG = 20;
int n, k, q, fine[N], a[N];
bool check (int x) {
int cnt[N];
memset(cnt, 0, sizeof cnt);
int bad = 0;
for (int i = 0; i < k; i++)
bad += (fine[i] > 0);
for (int i = 1; i <= x; i++) {
cnt[a[i]]++;
if (cnt[a[i]] == fine[a[i]]) {
--bad;
}
if (bad == 0) return true;
}
int b = 1, e = x;
while (e != n) {
cnt[a[b]]--;
if (cnt[a[b]] == fine[a[b]] - 1) ++bad;
cnt[a[e + 1]]++;
if (cnt[a[e + 1]] == fine[a[e + 1]]) --bad;
++b, ++e;
if (!bad) return true;
}
return false;
}
int main() {
fast_io;
cin >> n >> k >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
fine[x] += y;
}
int l = 1, r = n + 1;
while (r - l) {
int md = (r + l) >> 1;
if (check(md)) r = md;
else l = md + 1;
}
if (r > n) cout << "impossible\n";
else cout << r << '\n';
return 0;
}
# | 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... |