ACM/ICPC ec-final

Source: Internet
Author: User

A. Boxes and Balls

Two points to find the largest $\frac{x (x+1) not exceeding $n$ can be the number of}{2}$ form.

#include <bits/stdc++.h>using namespace std; typedef long long LL; void Solve () {LL n; scanf ("%lld", &n); L L l = 1, r = 2e9, while (L < R) {LL m = l + R + 1 >> 1; LL t = m * (M + 1)/2; if (t <= N) L = m; else R = m-1;} printf ("%lld\n", L * (l + 1)/2);} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", I); solve (); }return 0;}

  

B. Business Cycle

Two-point answer, then the violent simulation, if there is no detonation negative, then the description entered the cycle section, directly after the calculation, pay attention to the final set aside several rounds of violence simulation.

#include <bits/stdc++.h>using namespace std; typedef long long ll; int N,i;ll G,p,l,r,mid,ans,a[1111111];bool Check (ll have)  {if (have>=g) return 1;  ll Ret=p;  ll Old=-1;    while (ret) {ll st=have;    BOOL flag=0;      for (int i=0;i<n;i++) {have+=a[i];      if (have<0) flag=1,have=0;      if (have>=g) return 1;      ret--;    if (!ret) return 0;    } if (flag) continue;    if (have<=st) return 0;    Old=have-st;  Break  } ll p=ret/n;  p-=3;  if (p<0) p=0;  if (p> (g-have)/old) return 1;  Have+=p*old;  Ret-=p*n;  if (have>=g) return 1;  int i=0;    while (ret--) {Have=max (0ll,have+a[i]);    i++;    I%=n;  if (have>=g) return 1; } return 0;}  void Solve () {scanf ("%d%lld%lld", &n,&g,&p);  for (i=0;i<n;i++) scanf ("%lld", &a[i]);  l=0,r=g-1,ans=g;  while (l<=r) {if (check (mid= (l+r) >>1)) r= (ans=mid) -1;else l=mid+1; } printf ("%lld\n", ans);} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d: ", i); solve ();} return 0;}

  

C. Suffixes and Palindromes

Depending on the manacher algorithm, you can get $o (n) $ for equality and inequality, and then construct the $sa$ array one by one.

First of all $sa[1]$ must fill in $ ' a ' $, and then for $sa[i]$, first through the $rank$ array to determine whether it and $sa[i-1]$ equal, if it can be equal, but because the unequal relationship contradiction, then it can only be greater than $sa[i-1]$.

So we can construct a set of solutions with the smallest dictionary order in $o (n) $ time, and pay attention to the processing without solution.

#include <bits/stdc++.h>using namespace std; typedef long long ll; const int N=1000110;int N,m,i,j,r,p,f[n];char A[n ],s[n];int len[n],e[n],fa[n],g[n],g[n],v[n],nxt[n],ed;int sa[n],rk[n],col[n];void NIE () {puts ("Wrong calculation!");} inline void Addedge (int x,int y) {v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} inline void addedge2 (int x,int y) {v[++ed]=y;nxt[ed]=g[x]; g[x]=ed;} int F (int x) {return fa[x]==x?x:fa[x]=f (fa[x]);} inline bool Makesame (int x,int y) {if (x<0| |  Y&GT;M) return 0;  if (x==y) return 1; if (x==0| |  Y==M) return 0;  if (x%2!=y%2) return 0;  if (x%2==1) return 1;  x>>=1;  y>>=1;  if (F (x)!=f (y)) fa[fa[x]]=fa[y];  printf ("Same%d%d\n", x, y); return 1;}  inline bool Makediff (int x,int y) {if (x==y) return 0; if (x<0| |  Y&GT;M) return 1; if (x==0| |  Y==M) return 1;  if (x%2!=y%2) return 1;  if (x%2==1) return 0;  x>>=1;  y>>=1;  Addedge (x, y);  Addedge (Y,X);  printf ("diff%d%d\n", x, y); return 1;}  inline bool Bigger (int x,int y) {//suffix[x] > suffix[y]? if (x>n) rEturn 0;  if (y>n) return 1; return rk[x]>rk[y];}  void Solve () {scanf ("%d", &n);  scanf ("%s", a+1);  for (i=1;i<=n;i++) s[i<<1]=a[i],s[i<<1|1]= ' # ';  S[0]= ' $ ';  s[1]= ' # ';  S[m= (n+1) <<1]= ' @ ';    /*for (r=p=0,f[1]=1,i=2;i<m;i++) {for (F[i]=r>i?min (R-i,f[p*2-i]): 1;s[i-f[i]]==s[i+f[i]];f[i]++);  if (i+f[i]>r) r=i+f[i],p=i;  } for (i=1;i<=m;i++) Putchar (S[i]);p UTS ("");  for (i=1;i<=m;i++) printf ("%d", F[i]);p UTS ("");  */For (i=1;i<=n;i++) scanf ("%d", &sa[i]), sa[i]++;  for (i=1;i<=n*2-1;i++) {scanf ("%d", &len[i]);        } for (i=1;i<=n*2-1;i++) {if (i&1) {if (len[i]%2==0) {NIE ();      Return        }}else{if (len[i]%2) {NIE ();      Return  }} e[i+1]=len[i]+1;  } e[1]=1;      E[m-1]=e[m]=1;  for (i=1;i<=n;i++) fa[i]=i;    for (ed=0,i=1;i<=n;i++) g[i]=g[i]=0; for (r=p=0,f[1]=1,i=2;i<m;i++) {for (F[i]=r>i?min (R-i,f[p*2-i]): 1;f[i]<e[i];f[i]++) {int x=i-f[i],y=i+f[i]      ; if (!makEsame (x, y)) {NIE ();      Return      }} if (F[i]!=e[i]) {NIE ();    Return    } int x=i-f[i],y=i+f[i];      if (!makediff (x, y)) {NIE ();    Return  } if (I+f[i]>r) r=i+f[i],p=i;  } for (i=1;i<=n;i++) F (i);      for (i=1;i<=n;i++) for (J=g[i];j;j=nxt[j]) {if (Fa[i]==fa[v[j])) {NIE ();    Return  } addedge2 (Fa[i],fa[v[j]]);  } for (i=1;i<=n;i++) rk[sa[i]]=i;  for (i=1;i<=n;i++) col[i]=0;  Col[fa[sa[1]]]=1;    for (i=2;i<=n;i++) {int x=sa[i];    BOOL cansame=1;    if (bigger (sa[i-1]+1,x+1)) cansame=0;    printf ("%d%d\n", i,cansame);    int pre=col[fa[sa[i-1]];          if (Col[fa[x]]) {if (cansame) {if (col[fa[x]]<pre) {NIE ();        Return          }}else{if (col[fa[x]]<=pre) {NIE ();        Return    }} continue;    } for (J=g[fa[x]];j;j=nxt[j]) if (col[v[j]]==pre) cansame=0;    if (!cansame) pre++;      if (pre>26) {NIE ();    Return  } Col[fa[x]]=pre; } for (I=1;i<=n;i++) Putchar (' a ' +col[fa[i]]-1); Puts ("");} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", I); solve (); }return 0;}

  

D. Change

Category discussion.

#include <bits/stdc++.h>using namespace std; void solve () {double x, y; scanf ("%lf%lf", &x, &y); int  A = x * + 0.5; int b = y * + 0.5; if (b = = 1 | | b = = Ten | | b = = | | b = = + | | b = = 10000) {if (a = = 2 * b ) printf ("0.01\n"); else printf ("0.02\n");} else printf ("0.01\n");} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", I); solve (); }return 0;}

  

E. Colorful Floor

Leave the pit.

F. Hungry Game of Ants

DP out each ant to the left to the right to eat the number of programs, with the prefix and optimization. Time complexity $o (n) $.

#include <bits/stdc++.h>using namespace std; typedef long long LL; const int MAXN = 1000005; const int mod = 1E9 + 7 ; LL DP[MAXN], F[MAXN], SUM[MAXN]; int n, k; void Up (ll& x, LL y) {x + = y; if (x >= mod) x-= mod;} void Solve () {scanf ("%d%d", &n, &k); if (k = = 1) {printf ("0\n"); return;} for (int i = 0; I <= N; + + i) {dp[i] = f[i] = 0; if (i) sum[i] = sum[i-1] + i;} LL ans = 2, p = 1, for (int i = 2; i < K; + + i) {up (P, p); if (Sum[i] < Sum[k] – Sum[i]) up (ans, p); }if (k = = N) {printf ("%lld\n", ans * 2 mod); return;} DP[K] = f[k] = ans; for (int i = 1, j = 1, I <= N, + + i) {while (Sum[i] > 2 * sum[j]) + + J; up (Dp[i), (f[ I-1]-f[j-1] + MoD)% mod); up (F[i], (f[i-1] + dp[i])% mod);} printf ("%lld\n", Dp[n]);} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", I); solve (); }return 0;}

  

G. Legacy of the Void

Leave the pit.

H. Open face Chinese Poker

Leave the pit.

I. Champions League

Leave the pit.

J. Dome and Steles

Two-point answer, and then solve each brick can put the range, according to the range from large to small, each greedy to enlarge the side can.

#include <bits/stdc++.h>using namespace std; typedef long long LL; const int MAXN = 100005; Const double EPS=1E-10;D Ouble a[maxn],ned[maxn];int n;double Sqr (double x) {return x*x;} int dcmp (double x) {if (Fabs (x) <eps) return 0;if (x>eps) return 1;return-1;} BOOL Check (double MD) {for (int i=0;i<n;i++) {if (dcmp (Md*md-a[i]) <=0) return 0;} for (int i=0;i<n;i++) printf ("%.3f", Ned[i]);p UTS ("");d ouble l=md,r=md;for (int i=0;i<n;i++) {if (l<r) swap (l , r); L=min (L,sqrt (md*md-a[i)); if (dcmp (L-1) >=0) {l-=1;continue;} Else{if (dcmp (l+r-1) <0) return 0;if ((1-l) * (1-l) +A[I]&GT;MD*MD) return 0;if (i!= (n-1)) return 0;}} return 1;} void Solve () {scanf ("%d", &n), for (int i=0;i<n;i++) {double x,y;scanf ("%lf%lf", &x,&y), A[i]=min (SQR (x) + Sqr (Y/2), Sqr (y) +sqr (X/2));} Sort (a,a+n);d ouble l=0,r=1e6;for (int i=0;i<200;i++) {double md= (l+r)/2;if (check (MD)) R=md;else L=MD;} printf ("%.12f\n", r);} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", i); solve ();} return 0;}

  

K. Convex polyhedron

After the three-dimensional convex hull, if you know the direction of the projection vector, then the cross product of the positive and negative number of greedy to all the normal vector add up can be. A random enumeration of 10,000 directional vectors is then possible.

#include <bits/stdc++.h>using namespace std; #define PR 1e-8 #define N 620struct tpoint {Double x, y, Z; Tpoint () {}tpoint (double x, double y, double z): X (x), Y (y), Z (z) {}tpoint operator+ (const Tpoint p) {RET Urn Tpoint (x+p.x,y+p.y,z+p.z);} Tpoint operator-(const Tpoint p) {return tpoint (X-P.X,Y-P.Y,Z-P.Z);} Tpoint operator* (const Tpoint p) {return tpoint (y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);} Tpoint operator* (double p) {return tpoint (x*p,y*p,z*p);} Tpoint operator/(double p) {return tpoint (x/p,y/p,z/p);} Double operator^ (const Tpoint p) {return x * p.x + y * p.y + z * P.Z;} void Show () {printf ("%.2f%.2f%.2f\n", X, Y, z);}} Center;struct Fac{int A, B, C; bool OK;}; struct T3dhull{int n; Tpoint ply[n];int TRIANGLECNT;FAC tri[n]; int vis[n][n];d ouble Dist (Tpoint a) {return sqrt (a.x * a.x + a.y * a.y + a.z * a.z);} Double area (Tpoint A, tpoint B, Tpoint c) {return dist ((b-a) * (c-a));} Tpoint fa (Tpoint a,tpoint b,tpoint c) {return (b-a) * (C-A);}Double Volume (Tpoint A, tpoint B, Tpoint C, Tpoint D) {return (b-a) * (c-a) ^ (d-a);} Double Ptoplane (Tpoint &p, fac& f) {tpoint m = Ply[f.b]-ply[f.a],n=ply[f.c]-ply[f.a],t=p-ply[f.a];return (m*n) ^t;} void deal (int p,int a,int b) {int f = VIS[A][B];FAC add;if (tri[f].ok) {if ((Ptoplane (ply[p],tri[f))) &GT;PR) DFS (p,f); else{ Add.a=b,add.b=a,add.c=p,add.ok=1;vis[p][b]=vis[a][p]=vis[b][a]=trianglecnt;tri[trianglecnt++]=add;}}} void Dfs (int p,int cnt) {Tri[cnt].ok = 0;d eal (p,tri[cnt].b,tri[cnt].a);d eal (p,tri[cnt].c,tri[cnt].b);d EAL (p,tri[cnt] . a,tri[cnt].c);} BOOL Same (int s, int e) {Tpoint a = Ply[tri[s].a],b=ply[tri[s].b],c=ply[tri[s].c];return fabs (Volume (A, B, C, ply [TRI[E].A]) ) < pr&& Fabs (volume (A, B, C, ply[tri[e].b])) <PR&& Fabs (Volume (A, B, C, ply[tri[e].c])) <p R;} void construct () {int i,j;trianglecnt=0;if (n < 4) return, BOOL TMP = 1, for (i = 1; i < n; + + i) {if (Dist ( Ply[0]-ply[i]) &GT;PR) {Swap (ply[1],ply[i]); tmp=0;break;}} if (TMP) return, TMP = 1, for (i = 2; i < n; + + i) {if ((Dist ((ply[0]-ply[1]) * (ply[1]-ply[i))) &GT;PR) {Swap (P Ly[2],ply[i]); tmp=0;break;}} if (TMP) return;tmp=1;for (i=3;i<n;++i) {if (Fabs ((ply[0]-ply[1]) * (ply[1]-ply[2]) ^ (ply[0]-ply[i])) (&GT;PR) {swap ( Ply[3],ply[i]); tmp=0;break;}} if (TMP) RETURN;TMP=1;FAC add;for (i=0;i<4;++i) {add.a= (i+1)%4,add.b= (i+2)%4,add.c= (i+3)%4,add.ok=1;if ((Ptoplane (Ply[i],add)) >0) swap (ADD.B,ADD.C); vis[add.a][add.b]=vis[add.b][add.c]=vis[add.c][add.a]=trianglecnt;tri[trianglecnt++]= add;} for (I=4;i<n;++i) {for (j=0;j<trianglecnt;++j) {if (tri[j].ok&& (Ptoplane (ply[i],tri[j)) >PR) {DFS (i , j); break;}}} int cnt = trianglecnt;trianglecnt=0;for (i=0;i<cnt;++i) {if (Tri[i].ok) {tri[trianglecnt++]=tri[i];}}} void Show () {for (int i = 0; i < trianglecnt;++i) {printf ("------%d------\ n", i);p ly[tri[i].a].show ();p Ly[tri[i] . B].show ();p ly[tri[i].c].show ();}} Double check (Tpoint mydi) {tpoint ret=tpoint (. 0,.0,.0); for (int i=0;i<trianglecnt;i++) {Tpoint cur=fa (ply[tri[i].a],ply[tri[i].b],ply[tri[i].c]), if ((mydi^cur) >=0) ret=ret+ cur;} Return Dist (ret);} int Getrand () {int Ret=rand ()%200;if (rand ()%2) ret=-ret;return ret;} void Solve () {double ans=0;for (int i=0;i<10000;i++) {tpoint mydi;mydi.x=getrand (); Mydi.y=getrand (); Mydi.z=getrand (); Ans=max (Ans,check (Mydi));} printf ("%.12f\n", ANS/2);}} A;void solve () {scanf ("%d", &AMP;A.N), for (int i = 0; i < A.N; + + i) {scanf ("%lf%lf%lf", &a.ply[i].x,& A.PLY[I].Y,&AMP;A.PLY[I].Z);} A.construct ();//a.show (); A.solve ();} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", I); solve (); }return 0;}

  

L. multiplication Table

Enumerate the approximations and then test.

#include <bits/stdc++.h>using namespace std; typedef long long LL; int n,m;int a[1020][1020];struct node{int x,y,z;n Ode () {}node (int x,int y,int z): X (x), Y (y), Z (z) {}}nd[1000020];int cnt;int csx,csy;bool check (int ox,int oy,int rx,int ry) { if (rx-(ox-1) <1) return 0;if (ry-(oy-1) <1) return 0;for (int i=0;i<cnt;i++) {int x=nd[i].x,y=nd[i].y,z=nd[i].z; if (1ll* (rx+ (X-ox)) * (ry+ (Y-oy))!=z) return 0; return 1;} void Solve () {scanf ("%d%d", &n,&m), cnt=0;for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {char s[20];scanf ( "%s", s); if (s[0]== '? ') Continue;int x=0;for (int k=0;s[k];k++) {x=x*10+s[k]-' 0 ';} Nd[cnt++]=node (i,j,x);}} if (!cnt) {puts ("Yes"); return;} BOOL Flag=0;int num=nd[0].z;csx=nd[0].x,csy=nd[0].y;for (int i=1;i<cnt;i++) {if (num>nd[i].z) {num=nd[i].z;csx= Nd[i].x;csy=nd[i].y;}} for (int i=1;i<=num/i&&!flag;i++) {if (num%i==0) {if (check (csx,csy,i,num/i)) {flag=1;break;} if (I*i!=num&&check (csx,csy,num/i,i)) {Flag=1;break;}}} Puts (flag? ") Yes ":" No ");} int main () {int T; scanf ( "%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", I); solve ();} return 0;}

  

M. November 11th

For $n$ lengths, the contribution to the minimum value is $\lfloor\frac{n+1}{2}\rfloor$, and the contribution to the maximum value is $\lfloor\frac{n+2}{3}\rfloor$.

#include <bits/stdc++.h>using namespace std; typedef long long LL; int n,m,i,j,k,x,y,f[1111][1111],ans0,ans1; inline int F (int n) {  return (n+1)/2;} inline int G (int n) {  if (n==1) return 1;  Return (n+2)/3;} void Solve () {scanf ("%d%d", &n,&m), for (i=1;i<=n;i++) for (j=1;j<=m;j++) f[i][j]=1;scanf ("%d", &k); while (k--) scanf ("%d%d", &x,&y), F[x+1][y+1]=0;ans0=ans1=0;for (i=1;i<=n;i++) {  j=1;  for (;j<=m;) {    if (!f[i][j]) {j++;continue;}    for (k=j;k<=m&&f[i][k];k++);    Ans0+=f (k-j);    Ans1+=g (k-j);    j=k;          }        }        printf ("%d%d\n", ans0,ans1);} int main () {int t; scanf ("%d", &t), for (int i = 1; I <= T; + + i) {printf ("Case #%d:", I); solve (); }return 0;}

  

Summarize:

    • b Forget to consider reserving the situation, leading to WA5 hair.
    • The question of C is not enough to judge the solution, and the array is less than half, resulting in WA.
    • The M question did not think clearly, leading to WA.

ACM/ICPC ec-final

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.