Rokua P1080 (NOIP2012) Kings game-greedy sort and high precision

Source: Internet
Author: User
Tags mul

Title: https://www.luogu.org/problemnew/show/P1080

The determination of sorting method, just take two people, by comparison and deduction, can be drawn ai*bi small people in front;

High-precision writing when you make some details of the error, see the comments.

The code is as follows:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXN 200005using namespaceStd;typedefLong Longll//with a long long!ll N,KA,KB,ANS[MAXN],TMP[MAXN],TMP2[MAXN],C[MAXN];structn{ll a a B;} p[1005];BOOLcmp (N x,n y) {returnx.a*x.b<y.a*y.b;}voidChu (ll a[],ll b) {ll x=0; c[0]=a[0];  for(LL i=a[0];i>0; i--)//reverse storage, positive order except{C[i]= (x*10000+a[i])/b; X= (x*10000+a[i])%b; }     while(c[c[0]]==0&&c[0]>0) c[0]--;}voidCH (ll A[],ll b) {ll x=0;  for(LL i=1; i<=a[0];i++)//Reverse Multiplication{A[i]*=b;a[i]+=x; X=a[i]/10000; A[i]%=10000; }     while(x) a[0]++,a[a[0]]=x%10000, x/=10000;}intcom (ll a[],ll b[]) {if(a[0]>b[0])return 1; if(a[0]<b[0])return-1;  for(LL i=a[0];i;i--)//Reverse!         if(A[i]>b[i])return 1; return 0;}voidprint (ll a[]) {//if (a[a[0]]==0)//?//    {//printf ("1");//return;//    }printf"%lld", a[a[0]]);  for(LL i=a[0]-1;i>0; i--) {printf ("%lld", a[i]/ +); A[i]%= +; printf ("%lld", a[i]/ -); A[i]%= -; printf ("%lld", a[i]/Ten); A[i]%=Ten; printf ("%lld", A[i]); }}intMain () {scanf ("%lld%lld%lld",&n,&ka,&KB);  for(LL i=1; i<=n;i++) scanf ("%d%d",&p[i].a,&p[i].b); Sort (P+1, p+n+1, CMP); tmp[0]=1; tmp[1]=ka;//pressure level     for(LL i=1; i<=n;i++) {memcpy (tmp2,tmp,sizeoftmp); Memset (c,0,sizeofc);        Chu (tmp2,p[i].b); if(COM (C,ans) >0) memcpy (Ans,c,sizeofc);//must be >0, otherwise-1 will also be counted! CH (tmp,p[i].a);    } print (ANS); return 0;}

This is a bit slow to run, some places are not well written, there is a code to run quickly, you can compare the method to see the speed up.

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intinf=10000; typedefLong Longll;ll n,gb,ljc[1005],max,tmp[1005],ans[1005];Chartp[5];structnode{ll A,b,w;} r[1005];BOOLcmp (Node x,node y) {returnx.w<Y.W;}voidChu (ll b) {ll x=0; tmp[0]=ljc[0];  for(LL i=ljc[0];i;i--) {ll s=ljc[i]+x*INF; Tmp[i]=s/b; X=s%b; }     while(!tmp[tmp[0]]&&tmp[0]>1) tmp[0]--;}BOOLComp (ll ans[],ll b[]) {if(ans[0]>b[0])return 0; if(ans[0]<b[0])return 1;  for(LL i=ans[0];i;i--)    {        if(Ans[i]>b[i])return 0; if(Ans[i]<b[i])return 1; }    return 0;}voidMul (ll a) {ll x=0;  for(LL i=1; i<=ljc[0];i++) {ll s=ljc[i]*a+x; X=s/INF; Ljc[i]=s%INF; }    if(x) ljc[0]++,ljc[ljc[0]]=x;//Enter at most one}intMain () {scanf ("%lld%lld%lld", &n,&ljc[1],&GB); ljc[0]=1;  for(LL i=1; i<=n;i++) {scanf ("%lld%lld",&r[i].a,&r[i].b); R[I].W=r[i].a*r[i].b; } sort (R+1, r+n+1, CMP);  for(LL i=1; i<=n;i++) {memset (TMP,0,sizeoftmp);        Chu (r[i].b); if(Comp (ans,tmp)) memcpy (Ans,tmp,sizeoftmp);    Mul (R[I].A); } printf ("%lld", ans[ans[0]]);  for(LL i=ans[0]-1; i;i--) {printf ("%lld", ans[i]/ +); printf ("%lld", ans[i]/ -%Ten); printf ("%lld", ans[i]/Ten%Ten); printf ("%lld", ans[i]%Ten); }    return 0;}
View Code

Rokua P1080 (NOIP2012) Kings game-greedy sort and high precision

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.