luogu 1080 King game Sort precision

The product numbers in ascending order minister hand, is the optimal queuing scheme Proof: If mul * a1 / b2> mul * a2 / b1 ==> a1 * b1> a2 * b2 exchange 1 and then 2, more can be made a maximum value small (the maximum value is not necessarily the last minister, but to indicate whether the adjacent two ministers need to exchange) when comparing the product, if they are equal, the number on the right hand relatively small place in front. The equivalent of a sort we follow the rules of this comparison. Then with high-precision can.

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cstring>
  4 using namespace std;
  5 struct peo
  6 {
  7     int l,r,m;
  8     friend bool operator < (peo a,peo b)
  9     {
 10         if (a.m == b.m)
 11             return a.r < b.r;
 12         return a.m < b.m;
 13     }
 14 } c[1100];
 15 int ans,len,hloc,maxl,n;
 16 int a[10000],a2[10000],d[10000],maxx[10000];
 17 char str[10000];
 18 
 19 void mul(int a[],int b)
 20 {
 21     memset(d,0,sizeof(d));
 22     for(int i = 1; i <= len; i++)
 23         d[i] = a[i] * b;
 24     for(int i = 1; i <= len; i++)
 25         if (d[i] / 10)
 26         {
 27             d[i + 1] += d[i] / 10;
 28             d[i] %= 10;
 29         }
 30     if(d[len+1])
 31         len++;
 32     while(d[len] / 10)
 33     {
 34         d[len + 1] += d[len] / 10;
 35         d[len] %= 10;
 36         len++;
 37     }
 38     for(int i = 1; i <= len; i++)
 39         a[i] = d[i];
 40 }
 41 
 42 void div(int a[],int b)
 43 {
 44     memset(d,0,sizeof(d));
 45     int x = 0;
 46     for(int i = 1;i <= len;i++)
 47     {
 48         d[i] = (a[i] + x * 10) / b;
 49         x = (a[i] + x * 10) % b;
 50     }
 51     bool hav = 0;
 52     for (int i = 1;i <= len;i++) 
 53         if (d[i])
 54         {
 55             hav = 1;
 56             break;
 57         }
 58     if (hav)
 59     {
 60         int i = 1;
 61         while (d[i] == 0)
 62             i++;
 63         hloc = i;//最高为位置 
 64         for (;i <= len;i++)
 65         {
 66             if (d[i] > maxx[i] && len - hloc + 1 == maxl || len - hloc + 1 > maxl)
 67             {
 68                 for (int j = hloc;j <= len;j++) maxx[j] = d[j];
 69                 maxl = len - hloc + 1;
 70                 break;
 71             }
 72             if (maxl > len - hloc + 1 || d[i] < maxx[i]) 
 73                 break;
 74         }
 75     }
 76 }
 77 int main()
 78 {
 79     scanf("%d",&n);
 80     for (int i = 0; i <= n; i++)
 81     {
 82         scanf("%d%d",&c[i].l,&c[i].r);
 83         c[i].m = c[i].l * c[i].r;
 84     }
 85     sort(c + 1,c + n + 1);
 86     for(int i = 0; c[0].l; i++)
 87     {
 88         str[i] = c[0].l % 10 + '0';
 89         c[0].l /= 10;
 90     }
 91     len = strlen(str);
 92     for ( int I = . 1 ; I <= len; I ++ )
 93          A [I] = STR [I - . 1 ] - ' 0 ' ;
 94      for ( int I = . 1 ; I <= len; I ++ )
 95          A2 [I ] = STR [len - I] - ' 0 ' ;
 96      for ( int I = . 1 ; I <= n-; I ++ )
 97      {
 98          // Note that the multiplication from the low count goes high, the division count from high to low, so a positive and negative. 
99          div (A2, C [I] .r); // product of the previous calculation, in addition to the current hand.
100          MUL (A, C [I] .L); // the current multiplication into the left hand. 
101          for ( int J = . 1 ; J <= len; J ++ )
 102              A2 [J] = A [len - J + . 1 ];
 103      }
 104      for ( int I = hloc; I <= MaxL + hloc - . 1 ; I ++ )
 105          the printf ( " % D " , Maxx [I]);
 106      return  0 ;
 107 }

 

 

 

Guess you like

Origin www.cnblogs.com/iat14/p/11482442.html