Submission #1026654

# Submission time Handle Problem Language Result Execution time Memory
1026654 2024-07-18T09:04:01 Z Malix Mechanical Doll (IOI18_doll) C++14
100 / 100
89 ms 13252 KB
#include "doll.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;

#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LSOne(s) ((s)&(-s))

ll INF=1e18+10;
int inf=1e9+10;
ll M=1e9+7;

void create_circuit(int M, std::vector<int> A) {
  int n=A.size();
vi tm(M+1);
REP(i,0,n)tm[A[i]]++;
int mm=0;
REP(i,0,M+1)mm=max(mm,tm[i]);
if(mm<=4){
  vi c,x,y;
  vi a(M+1,0);
  vii b(M+1);
  REP(i,0,n)a[A[i]]++;
  REP(i,0,n-1)b[A[i]].PB(A[i+1]);
  b[A[n-1]].PB(0);
  c.resize(M+1,0);
  c[0]=A[0];
  int k=0;
  REP(i,1,M+1){
    if(a[i]==0)continue;
    if(a[i]==1)c[i]=b[i][0];
    else if(a[i]==2){
      c[i]=k-1;
      k--;
      x.PB(b[i][0]);
      y.PB(b[i][1]);
    }
    else if(a[i]==3){
      c[i]=k-1;
      k--;
      x.PB(k-1);
      y.PB(k-2);
      x.PB(k);
      y.PB(b[i][1]);
      x.PB(b[i][0]);
      y.PB(b[i][2]);
      k-=2;
    }
    else{
      c[i]=k-1;
      k--;
      x.PB(k-1);
      y.PB(k-2);
      x.PB(b[i][0]);
      y.PB(b[i][2]);
      x.PB(b[i][1]);
      y.PB(b[i][3]);
      k-=2;
    }
  }
  // for(auto u:c)cerr<<u<<" ";
  // cerr<<"\n";
  // for(auto u:x)cerr<<u<<" ";
  // cerr<<"\n";
  // for(auto u:y)cerr<<u<<" ";
  // cerr<<"\n";
  answer(c,x,y);
  return;
}

  vi c,x,y;
  vi p;
  REP(i,0,21)p.PB(pow(2,i));
  c.resize(M+1,-1);
  c[0]=A[0]; 
  vi a(M+1,0);
  vii b(M+1);
  a[0]=n;
  REP(i,1,n)b[0].PB(A[i]);
  b[0].PB(0);
  int k=0;
  REP(j,0,1){
    if(a[j]==0)continue;
    if(a[j]==1)c[j]=b[j][0];
    else if(a[j]==2){
      x.PB(b[j][0]);
      y.PB(b[j][1]);
    }
    else{
        k--;
        vi x1,y1;
        auto it=lower_bound(p.begin(),p.end(),a[j]);
        int k1=log2(*it);
        int pos=k;
        int t=0;
        x1.PB(inf);y1.PB(inf);
        REP(i,0,k1-1){
          REP(v,0,pow(2,i)){
            t++;
            x1.PB(pos-1);
            y1.PB(pos-2);
            pos-=2;
          }
        }
        REP(v,0,pow(2,k1-1)){
            x1.PB(k);
            y1.PB(k);
        }
        vi arr(*it,0);
        int g=*it;
        int pp=0;
        int r=0;
        g/=2;
        while(g){
          while(pp<*it){
            pp+=g;
            REP(i,pp,pp+g)arr[i]|=(1<<r);
            pp+=g;
          }
          r++;g/=2;pp=0;
        }
        pii brr;
        REP(i,1,a[j]+1)brr.PB({arr[*it-i],*it-i});
        sort(brr.begin(),brr.end());
        REP(i,0,a[j]){
          if(brr[i].S%2==0)x1[t+brr[i].S/2+1]=b[j][i];
          else y1[t+brr[i].S/2+1]=b[j][i];
        }
        int s=x1.size();
        for(int i=s-1;i>=2;i--)if(x1[i]==y1[i]){    
          if(i%2==0)x1[i/2]=y1[i];
          else y1[i/2]=y1[i];
          x1[i]=inf;y1[i]=inf;
        }
        pos=k;
        REP(i,1,s){
          if(x1[i]!=pos&&x1[i]!=inf){
            if(x1[i]<0){
              x.PB(k-1);
              k--;
            }
            else x.PB(x1[i]);
          }
          else if(x1[i]!=inf)x.PB(pos);
          if(y1[i]!=pos&&y1[i]!=inf){
            if(y1[i]<0){
              y.PB(k-1);
              k--;
            }
            else y.PB(y1[i]);
          }
          else if(y1[i]!=inf)y.PB(pos);
        }
    }
  }
  // for(auto u:c)cerr<<u<<" ";
  // cerr<<"\n";
  // for(auto u:x)cerr<<u<<" ";
  // cerr<<"\n";
  // for(auto u:y)cerr<<u<<" ";
  // cerr<<"\n";
  answer(c,x,y);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 30 ms 7768 KB Output is correct
3 Correct 16 ms 6236 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 7 ms 4700 KB Output is correct
6 Correct 30 ms 9136 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 30 ms 7768 KB Output is correct
3 Correct 16 ms 6236 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 7 ms 4700 KB Output is correct
6 Correct 30 ms 9136 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 40 ms 8656 KB Output is correct
9 Correct 38 ms 10500 KB Output is correct
10 Correct 46 ms 13252 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 30 ms 7768 KB Output is correct
3 Correct 16 ms 6236 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 7 ms 4700 KB Output is correct
6 Correct 30 ms 9136 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 40 ms 8656 KB Output is correct
9 Correct 38 ms 10500 KB Output is correct
10 Correct 46 ms 13252 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 53 ms 12988 KB Output is correct
15 Correct 27 ms 7116 KB Output is correct
16 Correct 55 ms 10660 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 52 ms 12868 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 27 ms 5420 KB Output is correct
3 Correct 28 ms 7916 KB Output is correct
4 Correct 38 ms 9076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 27 ms 5420 KB Output is correct
3 Correct 28 ms 7916 KB Output is correct
4 Correct 38 ms 9076 KB Output is correct
5 Correct 89 ms 12772 KB Output is correct
6 Correct 81 ms 12168 KB Output is correct
7 Correct 82 ms 12332 KB Output is correct
8 Correct 63 ms 11852 KB Output is correct
9 Correct 40 ms 7768 KB Output is correct
10 Correct 82 ms 11844 KB Output is correct
11 Correct 57 ms 11372 KB Output is correct
12 Correct 38 ms 8056 KB Output is correct
13 Correct 55 ms 6988 KB Output is correct
14 Correct 42 ms 9592 KB Output is correct
15 Correct 61 ms 9896 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 45 ms 6472 KB Output is correct
18 Correct 34 ms 6468 KB Output is correct
19 Correct 39 ms 8080 KB Output is correct
20 Correct 62 ms 11484 KB Output is correct
21 Correct 72 ms 11472 KB Output is correct
22 Correct 88 ms 11376 KB Output is correct