[NOIP2012] King game puzzle

Source: Internet
Author: User

Main topic:

n individuals in a row, the Pai Tau fixed, the other can be changed. Each person has an integer, and a person's score is the product of the number on the left hand of all the people in front of him divided by the number on his right hand (rounded down), the minimum value of the maximum fraction in the whole column.

Ideas:

First of all, all sequences can be converted through the exchange of several neighboring people, and the exchange of neighboring persons will only affect the scores of these two individuals.

Assuming that two adjacent persons are i,i+1, then a[i]*b[i]<=a[i+1]*b[i+1], set before I and for S, then before the exchange of Ans1=max{s/b[i],s*a[i]/b[i+1]}, after Exchange ans2=max{s/b[i+1], S*a[i+1]/b[i]}. ∵a[i],a[i+1],b[i],b[i+1],s are positive integers, ∴s*a[i]>=s,∴s*a[i]/b[i+1]>=s/b[i+1].

Similarly: S*a[i+1]/b[i]>=s/b[i].

∵A[I]*B[I]<=A[I+1]*B[I+1] and a[i],a[i+1],b[i],b[i+1],s are positive integers, ∴s*a[i]*b[i]<=s*a[i+1]*b[i+1],∴s*a[i]/b[i+1] <=s*a[i+1]/b[i],∴ans2>=ans1,∴ to make the final ans minimum, the product of each person's hand number is arranged from small to large, in the calculation of the answer.

Due to the large data, it is necessary to use high precision (pressure level), multiplication no problem, division pay attention to the boundary conditions!

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intm=10005, n=100000;6 intN,i,l,p,len,a[m],b[m],c[m],s[m],d[m],z[m],id[m],ans[m];7 8 BOOLcmpintXintY) {returns[x]<s[y];}9 Ten voidChengintx) One { A     inti,t=0; -      for(i=1; i<=len;++i) -     { thec[i]=t+c[i]*x; -t=c[i]/N; -c[i]=c[i]%N; -     } +     if(t) c[++len]=T; - } +  A BOOLPD () at { -     if(l>p)return 1; -      for(intI=1; i<=l;i++) -         if(Ans[i]<d[i])return 1; -     return 0; - } in  - voidChuinty) to { +     inti=len,t,x=Z[len]; -      for(l=0; i;) the     { *         if(i>1) {if(X<y) x=x*n+z[--i]; }Else  Break; $d[++l]=x/y;x=x%y;Panax Notoginseng     } -     if(x>=y) d[++l]=x/y; the     if(PD ()) for(p=l,i=1; i<=l;i++) ans[i]=D[i]; + } A  the intMain () + { -scanf"%d%d%d", &n,&a[0],&b[0]); $      for(i=1; i<=n;i++) scanf ("%d%d", &a[i],&b[i]), s[i]=a[i]*b[i],id[i]=i; $Sort (id+1, id+1+N,CMP); c[len=1]=a[0]; -      for(i=1; i<=n;i++) memcpy (Z,c,sizeofc), Chu (B[id[i]]), Cheng (a[id[i]); -printf"%d", ans[1]); the      for(i=2; i<=p;i++)//printf ("%05d", Ans[i]); -         if(ans[i]>10000) printf ("%d", Ans[i]);Wuyi         Else if(ans[i]> +) printf ("0%d", Ans[i]); the              Else if(ans[i]> -) printf ("00%d", Ans[i]); -                   Else if(ans[i]>Ten) printf ("000%d", Ans[i]); Wu                         Elseprintf"0000%d", Ans[i]); -     return 0; About}

[NOIP2012] King game puzzle

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.