1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f3f3fll #define debug(x) cerr<<#x<<"="<<x<<endl using namespace std; using ll=long long; using ld=long double; using pli=pair<ll,int>; using pi=pair<int,int>; template<typename A> using vc=vector<A>; using pl=pair<ll,ll>; using ti=tuple<int,int,int>; inline int read() { int s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } inline ll lread() { ll s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } const int P=114514191; map<pl,bool>vis[100001]; vc<vc<int> >b[100001]; vc<ti>nx[100001]; int a[100001]; vc<int>ans[51]; int val[70],siz[70],dfn[70]; int ls[70],rs[70]; int wt[70]; int n,cnt,tim; inline pl run(vc<int>&a) { ll ans1=0,ans2=0; for(int i:a) { ans1+=i^P; ans2+=(ll)(i^P)*(i^P); } return pl(ans1,ans2); } inline int S(ll v) { if(v<0) return -1; int p=sqrt(v)+0.5; if((ll)p*p!=v) return -1; return p; } inline int check(int v1,int v2) { int num=S((ll)v1*v1-4ll*v2); if(num==-1||num>v1) return -1; if((num+v1)&1) return -1; return (num+v1)>>1; } inline void output(int p) { dfn[p]=tim; if(val[p]) printf("%d ",val[p]),tim++; else assert(ls[p]&&rs[p]),output(ls[p]),output(rs[p]); } int main() { n=read(); for(int i=1;i<=n;i++) a[i]=read(); b[n].push_back(vc<int>(1,a[n]));
int c=1,v=0,lst=n; for(int i=n-1;i;i--) { if(a[i]>a[i+1]) { if(a[i]!=a[i+1]+1) { printf("-1\n"); return 0; } v++; } else { c++;int d=a[i+1]-a[i]; for(unsigned id=0;id<b[lst].size();id++) { auto &p=b[lst][id]; map<int,bool>vvis; for(unsigned I=0;I<p.size();I++) { int v=p[I]; if(vvis.count(v)) continue; vvis[v]=1;if(v<d) continue; int a=check(v-d,v),b=v-d-a; if(a==-1) continue; auto q=p;q[I]=a,q.push_back(b); pl V=run(q);if(vis[i].count(V)) continue; vis[i][V]=1,::b[i].push_back(q); nx[i].push_back(ti(lst,id,I)); } } lst=i; } } if(b[lst].size()==0) { printf("-1\n"); return 0; } int id=0,all=c; for(int v:b[lst][0]) cnt++,val[cnt]=v,siz[cnt]=1,wt[cnt-1]=cnt;
while(lst<n) { auto [to,w,i]=nx[lst][id]; cnt++,c--,ls[cnt]=wt[i],rs[cnt]=wt[c],wt[i]=cnt; siz[cnt]=siz[ls[cnt]]+siz[rs[cnt]]; lst=to,id=w; }
output(cnt); for(int i=all+1;i<=n;i++) printf("1 "); putchar('\n');
int now=n-1,Y=all; for(int i=1;i<n;i++) { if(a[i]>a[i+1]) printf("%d\n",now--); else Y++,printf("%d\n",dfn[Y]+siz[ls[Y]]); } return 0; }
|