Submission #830343

#TimeUsernameProblemLanguageResultExecution timeMemory
830343karriganBad Codes (CCO19_day2problem3)C++14
0 / 100
1 ms1364 KiB
#include<bits/stdc++.h> using namespace std; #define all(fl) fl.begin(),fl.end() #define pb push_back #define fi first #define se second #define for1(i,j,k) for(int i=j;i<=k;i++) #define for2(i,j,k) for(int i=j;i>=k;i--) #define for3(i,j,k,l) for(int i=j;i<=k;i+=l) #define lb lower_bound #define ub upper_bound #define sz(a) (int)a.size() #define pii pair<int,int> #define pli pair<long long,int> #define gcd __gcd #define lcm(x,y) x*y/__gcd(x,y) string s[59]; int id[59]; bool cmp(int i,int j){ if (s[i]==s[j])return i<j; int x=min(sz(s[i]),sz(s[j])); for1(i1,0,x-1){ if (s[i][i1]!=s[j][i1]){ return s[i][i1]<s[j][i1]; } } return sz(s[i])<sz(s[j]); } bool vis[109][59][59]; long long d[109][59][59]; struct cus{ int diff,l,sf; long long val; bool operator < (const cus &p)const { return val>p.val; } }; int n,m; int bonus=50; const long long inf=1e18; long long f[59][59]; pii can[51][51]; void sp(){ priority_queue<cus>t; for1(i,-50,50){ for1(j,0,n){ for1(k,0,m){ d[i+bonus][j][k]=inf; } } } for1(i,1,n){ for1(j,1,n){ if (i==j)continue; int m1=sz(s[id[i]]),m2=sz(s[id[j]]); if (f[i][min(m1,m2)]==f[j][min(m1,m2)]){ if (m1==m2){ d[0+bonus][0][0]=m1; t.push({0,0,0,m1}); } else { if (m1<m2){ t.push({m1-m2,j,m2-m1,m2}); d[m1-m2+bonus][j][m2-m1]=m2; } else { t.push({m1-m2,i,m1-m2,m1}); d[m1-m2+bonus][i][m1-m2]=m1; } } } } } while (!t.empty()){ auto u=t.top(); t.pop(); if (vis[u.diff+bonus][u.l][u.sf])continue; vis[u.diff+bonus][u.l][u.sf]=1; //cout<<u.diff<<" "<<u.l<<" "<<u.sf<<'\n'; if (u.l==0)continue; if (u.diff==0)continue; long long prefix=f[u.l][sz(s[id[u.l]])]; int need=sz(s[id[u.l]])-u.sf; prefix/=(1ll<<need); //cout<<u.diff<<" "<<u.l<<" "<<u.sf<<" "<<prefix<<'\n'; for1(i,1,n){ int len=sz(s[id[i]]); if (f[i][min(len,u.sf)]!=prefix)continue; if (len==u.sf){ if (d[0+bonus][0][0]>u.val){ d[0+bonus][0][0]=u.val; t.push({0,0,0,u.val}); } } else if (len>u.sf){ int newdiff=u.diff; if (u.diff<0)newdiff+=len; else newdiff-=len; if (d[newdiff+bonus][i][len-u.sf]>u.val+len-abs(u.diff)){ d[newdiff+bonus][i][len-u.sf]=u.val+len-abs(u.diff); t.push({newdiff,i,len-u.sf}); } } else{ int newdiff=u.diff; if (u.diff<0)newdiff+=len; else newdiff-=len; if (d[newdiff+bonus][u.l][u.sf-len]>u.val){ d[newdiff+bonus][u.l][u.sf-len]=u.val; t.push({newdiff,u.l,u.sf-len}); } } } } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); //freopen("CODE.INP","r",stdin); //freopen("CODE.OUT","w",stdout); cin>>n>>m; for1(i,1,n){ id[i]=i; cin>>s[i]; } sort(id+1,id+1+n,cmp); for1(i,1,n){ for1(j,0,m-1){ if (sz(s[id[i]])<j+1){ f[i][j+1]=-1; } else { f[i][j+1]=f[i][j]; if (s[id[i]][j]=='1'){ f[i][j+1]+=(1ll<<j); } } } } for1(i,1,n){ int len=sz(s[id[i]]); for1(j,1,len){ //can[i][j] int xd=len-j; long long mask=f[i][len]/(1ll<<xd); for1(k,1,n){ if (sz(s[id[k]])<j)continue; if (f[k][j]==mask){ if (!can[i][j].fi){ can[i][j].fi=k; } can[i][j].se=k; } } } } sp(); long long best=d[0+bonus][0][0]; if (best>=1e18){ cout<<-1<<'\n'; } else cout<<best<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...