This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define arr array
#define pii pair<int, int>
#define pq priority_queue
const int MX_N = 2e5 + 5;
int n;
arr<int, MX_N> vl, rq;
bool chck(int sz) {
arr<int, MX_N> scr = {0};
pq<pii, vector<pii>, greater<pii>> ord;
for (int i = 1; i <= n; i++) scr[i] = -rq[i];
for (int i = 1; i <= sz; i++) scr[vl[i]]++;
for (int i = 1; i <= n; i++) ord.push({scr[i], i});
if (ord.top().first >= 0) return true;
for (int i = sz + 1; i <= n; i++) {
scr[vl[i]]++, scr[vl[i - sz]]--;
ord.push({scr[vl[i - sz]], vl[i - sz]});
while (scr[ord.top().second] != ord.top().first) {
int j = ord.top().second; ord.pop();
ord.push({scr[j], j});
}
if (ord.top().first >= 0) return true;
}
return false;
}
int bn_srch() {
int lw = 1, hgh = n;
while (lw != hgh) {
// cout << lw << " " << hgh << endl;
int md = (lw + hgh) / 2;
if (chck(md)) hgh = md;
else lw = md + 1;
}
return ((chck(lw)) ? lw : -1);
}
int main() {
// freopen("mrt.in", "r", stdin);
int tmp1, tmp2; cin >> n >> tmp1 >> tmp2;
for (int i = 1; i <= n; i++) { cin >> vl[i]; vl[i]++; }
for (int i = 1; i <= tmp2; i++) { int x; cin >> x; cin >> rq[x + 1]; }
if (!chck(n)) { cout << "impossible" << endl; exit(0); }
int ans = bn_srch();
cout << ans << endl;
}
# | 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... |