답안 #643397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
643397 2022-09-22T01:45:32 Z christinelynn Martian DNA (BOI18_dna) C++17
100 / 100
149 ms 25924 KB
#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

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;
      |          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 5 ms 9644 KB Output is correct
3 Correct 5 ms 9688 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 5 ms 9648 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9812 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 7 ms 9892 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 14564 KB Output is correct
2 Correct 44 ms 16700 KB Output is correct
3 Correct 76 ms 15692 KB Output is correct
4 Correct 64 ms 15036 KB Output is correct
5 Correct 71 ms 14256 KB Output is correct
6 Correct 32 ms 13240 KB Output is correct
7 Correct 36 ms 13356 KB Output is correct
8 Correct 91 ms 17564 KB Output is correct
9 Correct 65 ms 13712 KB Output is correct
10 Correct 74 ms 15272 KB Output is correct
11 Correct 6 ms 9784 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 6 ms 9780 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 7 ms 9760 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 6 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9700 KB Output is correct
25 Correct 5 ms 9680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 23096 KB Output is correct
2 Correct 139 ms 22536 KB Output is correct
3 Correct 89 ms 18116 KB Output is correct
4 Correct 47 ms 19388 KB Output is correct
5 Correct 112 ms 17064 KB Output is correct
6 Correct 149 ms 25924 KB Output is correct
7 Correct 63 ms 17576 KB Output is correct
8 Correct 83 ms 18692 KB Output is correct
9 Correct 58 ms 15004 KB Output is correct
10 Correct 43 ms 17064 KB Output is correct
11 Correct 75 ms 16156 KB Output is correct
12 Correct 64 ms 15444 KB Output is correct
13 Correct 70 ms 15304 KB Output is correct
14 Correct 33 ms 13608 KB Output is correct
15 Correct 39 ms 13928 KB Output is correct
16 Correct 89 ms 18748 KB Output is correct
17 Correct 64 ms 14592 KB Output is correct
18 Correct 86 ms 15668 KB Output is correct
19 Correct 7 ms 9812 KB Output is correct
20 Correct 7 ms 9812 KB Output is correct
21 Correct 7 ms 9844 KB Output is correct
22 Correct 6 ms 9828 KB Output is correct
23 Correct 6 ms 9784 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 6 ms 9684 KB Output is correct
26 Correct 5 ms 9704 KB Output is correct
27 Correct 5 ms 9684 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 5 ms 9700 KB Output is correct
30 Correct 5 ms 9684 KB Output is correct
31 Correct 5 ms 9684 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9684 KB Output is correct