Submission #415256

#TimeUsernameProblemLanguageResultExecution timeMemory
415256cfalasMechanical Doll (IOI18_doll)C++14
85.55 / 100
427 ms25476 KiB
#include "doll.h" #include<bits/stdc++.h> using namespace std; #define mp make_pair #define INF 10000000 #define MOD 1000000007 #define MID ((l+r)/2) #define HASHMOD 2305843009213693951 #define ll long long #define ull unsigned long long #define F first #define S second typedef pair<ll, ll> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef map<int, int> mii; #define EPS 1e-6 #define FOR(i,n) for(int i=0;i<((int)(n));i++) #define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++) #define FOA(v, a) for(auto v : a) int s=0, n; vector<vi> nxt; vector<set<int> > nxts; vector<int> C, X, Y; vi pr; int b; int inv(int x){ vi a; //cout<<x<<" "; FOR(i,b) a.push_back(x%2), x/=2; ll ans=0; FOA(v,a) ans*=2, ans+=v; //cout<<ans<<endl; return ans; } vi rep; ii cnt(int x, int l, int r){ set<int> all; if((1<<b)-r > (int)nxt[x].size()) return {1,C[x]}; return {2,0}; if(nxts[x].size()==1) return {1, *nxts[x].begin()}; FORi(i,l,r+1){ if(inv(i)<(int)nxt[x].size()-1) all.insert(nxt[x][inv(l)]); else if(i==(1<<b)-1) all.insert(nxt[x].back()); else all.insert(C[x]); if(all.size()>1) break; } if(all.size()>1) return {2,0}; else return {1, *all.begin()}; } void rec(int x, int l, int r){ //cout<<x<<": "<<l<<" "<<r<<endl; if(l==r) return; if(r==l+1){ //cout<<l<<" "<<r<<" "<<cnt(x,l,r).F<<endl; if(cnt(x,l,r).F==1) return; assert(pr.size()==nxt[x].size()); if((1<<b)-l <= (int)nxt[x].size()) X.push_back(nxt[x][pr[pr.size() - (1<<b) + l]]); else X.push_back(C[x]); l++; assert(l<=(1<<b)); if((1<<b)-l <= (int)nxt[x].size()) Y.push_back(nxt[x][pr[pr.size() - (1<<b) + l]]); else Y.push_back(C[x]); } else{ ii p = cnt(x,l,MID); int q = Y.size(); Y.push_back(s); if(p.F==1) X.push_back(p.S); else{ X.push_back(-(++s)); rec(x, l, MID); } p = cnt(x,MID+1,r); //cout<<MID+1<<" "<<r<<" "<<p.F<<endl; if(p.F==1) Y[q] = (p.S); else { Y[q] = -(++s); rec(x, MID+1,r); } } } void create_circuit(int M, std::vector<int> A) { n = A.size(); nxt.assign(M+1, vi()); nxts.assign(M+1, set<int>()); C.assign(M+1, 0); nxt[0].push_back(A[0]); nxts[0].insert(A[0]); FORi(i,1,n) nxt[A[i-1]].push_back(A[i]); FORi(i,1,n) nxts[A[i-1]].insert(A[i]); nxt[A[n-1]].push_back(0); nxts[A[n-1]].insert(0); FOR(x,M+1){ int q = nxt[x].size(); if(q==0) continue; else if(nxt[x].size()==1) C[x] = nxt[x][0]; else{ int t = q-1; int l=0; while(t) l++, t/=2; b = l; //cout<<q<<": "<<b<<endl; // generate permutation pr.clear(); FORi(i,(1<<b)-nxt[x].size(),(1<<b)) pr.push_back(inv(i)); set<int> all; map<int, int> ordered; int cnt=0; FOA(v,pr) all.insert(v); FOA(v,all) ordered[v] = cnt++;; FOR(i,pr.size()) pr[i] = ordered[pr[i]]; #ifdef LOCAL cout<<x<<": "; FOA(v,pr) cout<<v<<" "; cout<<endl; #endif C[x] = -(++s); rec(x, 0, (1<<l)-1); } } #ifdef LOCAL FOA(v,C) cout<<v<<" "; cout<<endl; FOA(v,X) cout<<v<<" "; cout<<endl; FOA(v,Y) cout<<v<<" "; cout<<endl; #endif answer(C, X, Y); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...