// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
int n, k, r;
vi dna;
vector<pair<int, int>> req;
int main() {
cin>>n>>k>>r;
dna.resize(n);
for(int i=0; i<n; i++) cin>>dna[i];
req.resize(r);
for(int i=0; i<r; i++){
cin>>req[i].first;
cin>>req[i].second;
}
vi t(n);
sort(req.begin(), req.end());
//check if n works
vvi where(r);
for(int i=0; i<n; i++){
//find which of the r it is
if(dna[i]<req[0].first || dna[i]>req[r-1].first){
t[i]=-1;
}
if(dna[i]==req[0].first){
where[0].push_back(i);
t[i]=0;
continue;
}
int a=0;
int b=r-1;
while(a+1<b){
int c=(a+b)/2;
if(req[c].first>=dna[i]) b=c;
else a=c;
}
if(req[b].first==dna[i]){
where[b].push_back(i);
t[i]=b;
}
else t[i]=-1;
}
for(int i=0; i<r; i++){
if(where[i].size()<req[i].second){
cout<<"impossible";
return 0;
}
}
//now for every index I want to find the minimal length that starts with it that works.
//rnlogn easy
vi ans(n, 1e9);
vi indexes(r);
for(int i=0; i<r; i++){
indexes[i]=req[i].second-1;
}
set<int> places;
for(int i=0; i<r; i++){
places.insert(-where[i][indexes[i]]);
}
for(int i=0; i<n; i++){
if(i>0){
if(t[i-1]>-1){
places.erase(-where[t[i-1]][indexes[t[i-1]]]);
indexes[t[i-1]]++;
if(indexes[t[i-1]]>=where[t[i-1]].size()){
break;
}
places.insert(-where[t[i-1]][indexes[t[i-1]]]);
}
}
ans[i]=-*places.begin()-i+1;
}
for(int i=0; i<n; i++){
ans[0]=min(ans[0], ans[i]);
}
cout<<ans[0];
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... |