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 ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll MOD=1e9+7;
using namespace std;
ll N,M,K,a[200005],point[200005];
vector <ll> vec[200005];
vector <pair<ll,ll> > pr[200005];
int main(){
cin>>N>>M>>K;
if(K<=10){
for(int i=1;i<=N;i++){
cin>>a[i];
vec[a[i]].push_back(i);
}
bool ok=true;
for(int i=1;i<=K;i++){
ll x,y;
cin>>x>>y;
// cout<<vec[x].size()<<' '<<y<<endl;
if(vec[x].size()<y){
ok=false;
}
else{
for(int j=y-1;j<vec[x].size();j++){
pr[i].push_back({vec[x][j-(y-1)],vec[x][j]});
}
}
}
if(!ok){
cout<<"impossible"<<endl;
return 0;
}
ll minn=1e18;
for(int i=1;i<=N;i++){
ll maks=0;
bool ok=true;
for(int j=1;j<=K;j++){
ll l=0;
ll r=pr[j].size()-1;
ll ans=-1;
while(l<=r){
ll m=(l+r)/2;
if(pr[j][m].fi>=i){
ans=m;
r=m-1;
}
else{
l=m+1;
}
}
if(ans==-1){
ok=false;
break;
}
maks=max(maks,pr[j][ans].se);
}
if(ok){
minn=min(minn,maks-i+1);
}
}
cout<<minn<<endl;
}
else{
for(int i=1;i<=N;i++){
cin>>a[i];
vec[a[i]].push_back(i);
}
bool ok=true;
for(int i=1;i<=K;i++){
ll x,y;
cin>>x>>y;
if(vec[x].size()<y){
ok=false;
}
else{
for(int j=y-1;j<vec[x].size();j++){
pr[i].push_back({vec[x][j-(y-1)],vec[x][j]});
}
}
}
if(!ok){
cout<<"impossible"<<endl;
return 0;
}
priority_queue <pair<pair<ll,ll>,ll>,vector<pair<pair<ll,ll>,ll> >,greater<pair<pair<ll,ll>,ll> > > pq;
ll l=1e18;
ll r=-1e18;
for(int i=1;i<=K;i++){
pq.push({{pr[i][0].fi,pr[i][0].se},i});
l=min(l,pr[i][0].fi);
r=max(r,pr[i][0].se);
point[i]=0;
}
ll minn=r-l+1;
while(true){
ll x=pq.top().fi.fi;
ll y=pq.top().fi.se;
ll z=pq.top().se;
pq.pop();
if(point[z]==pr[z].size()-1){
break;
}
point[z]++;
pq.push({pr[z][point[z]],z});
l=pq.top().fi.fi;
r=max(r,pr[z][point[z]].se);
minn=min(minn,r-l+1);
}
cout<<minn<<endl;
}
}
Compilation message (stderr)
dna.cpp: In function 'int main()':
dna.cpp:23:23: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
23 | if(vec[x].size()<y){
| ~~~~~~~~~~~~~^~
dna.cpp:27:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int j=y-1;j<vec[x].size();j++){
| ~^~~~~~~~~~~~~~
dna.cpp:75:23: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
75 | if(vec[x].size()<y){
| ~~~~~~~~~~~~~^~
dna.cpp:79:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int j=y-1;j<vec[x].size();j++){
| ~^~~~~~~~~~~~~~
dna.cpp:103:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | if(point[z]==pr[z].size()-1){
| ~~~~~~~~^~~~~~~~~~~~~~~~
dna.cpp:99:10: warning: unused variable 'x' [-Wunused-variable]
99 | ll x=pq.top().fi.fi;
| ^
dna.cpp:100:10: warning: unused variable 'y' [-Wunused-variable]
100 | ll y=pq.top().fi.se;
| ^
# | 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... |