[Bigint] Greedy [Template]vijosp1779 King Game _ High Precision

Source: Internet
Author: User

P1779 King Game Please login and submit
Tags: Noip improve group 2012[display label]
Describe
Coincides with the H National Day, the King invited N ministers to play an award-winning game. First, he had each minister write an integer on his left and right hand, and the king himself wrote an integer on his left and right hand. Then, let the N ministers in a row, the king stood at the front of the line. After the line, all the ministers will receive the King's reward for a number of gold coins, each minister received the number of gold coins: The number of people in front of the minister's left hand is the product divided by the number on his right hand, and then down to the results of the whole.
The king does not want a particular minister to receive a special reward, so he wants you to help him rearrange the order so that the most rewarded ministers receive as few rewards as possible. Note that the king's position is always at the front of the line.
Format
Input format

The first line contains an integer n, representing the number of ministers.
The second line contains two integers a and b, separated by a space, representing the whole number on the left and right hand of the king. The next n rows, each containing two integers a and b, are separated by a space, representing the integers on the left and right hand of each minister.
Output format

The output has only one row and contains an integer that represents the number of gold coins received by the most honored ministers in the rearranged ranks.
Sample 1
Sample input 1[replication]

3
1 1
2 3
7 4
4 6
Sample output 1[Copy]

2
Limit
1s per test point
Tips
For 20% of the data, there are 1≤n≤10,0 < A, b < 8;
For 40% of the data, there are 1≤n≤20,0 < A, b < 8;
For 60% of the data, there are 1≤n≤100;
For 60% of the data, ensure that the answer does not exceed 10^9;
For 100% of the data, there are 1≤n≤1,000,0 < A, b < 10000.
Source
Noip2012 to improve the group-rematch DAY1T2

Greedy idea:
Multiply greedy
*
*
*
I
J
*
*
If queued like this, the I,J Exchange does not affect the * person, the preceding product is sum
If I is in front, fetch MAX (sum/ RI,SUM*LI/RJ)
If J is in front, take Max (Sum/rj,sum*lj/ri)
The current case is the most value, that is, in Max (Sum*li/rj,sum*lj/ri)
together, you can find if LI*RI>LJ *RJ,
that is, if I is in front, the product is bigger, which is to put the product back.

#include <iostream> #include <cstdio> #include <ctime> #include <cstdlib> #include <cmath > #include <cstring> #include <string> #include <set> #include <map> #include <vector> include<queue> #include <algorithm> #ifdef WIN32 #define AUTO "%i64d" #else #define AUTO "%LLD" #endif #define INF 0x3f3f3f3f #define CLOCK clocks_per_sec #define CLE (x) memset (x,0,sizeof (x)) #define MAXCLE (x) memset (x,0x3f,sizeof ( x) #define MINCLE (x) memset (x,-1,sizeof (x)) #define MAXX (X1,X2,X3) Max (X1,max (X2,X3)) #define MINN (x1,x2,x3) min (x1, Min (x2,x3)) #define COP (A,X) memcpy (X,a,sizeof (a)) #define FROP "HDU" #define LL long #define SMIN (x,tmp) x=min (x,tmp
) #define SMAX (x,tmp) X=max (x,tmp) using namespace std;
const int N = 1005;
    struct bigint {static const int P =1,m=10;
    int w[4050];
    BigInt () {CLE (w); w[0]=1;}
        void Read () {string S;
        cin>>s;
        int now=1,c1=1,ct=0; for (int i = S.size ()-1;i>=0;i--) {w[now]+= (s[i]-' 0 ') *c1;
            c1*=10;
            ct++;
        if (ct==p&&i) {c1=1;ct=0;now++;}
    } W[0]=now;
        } bigint operator * (const int &b) {bigint a=*this;
        BigInt c;
        int &len=c.w[0];
        len=a.w[0]+5;
            for (int i = 1; I <= len;i++) {c.w[i]+=a.w[i]*b;
            c.w[i+1]=c.w[i]/m;
        C.w[i]%=m;
        while (Len>1&&!c.w[len]) len--;
    return C;
        } bigint operator/(const int &b) {bigint a=*this;
        BigInt c;
        int &len=c.w[0];
        LEN=A.W[0];
        int tmp=0;
            for (int i = len; i; i--) {tmp+=a.w[i];
                if (tmp>=b) {c.w[i]+=tmp/b;
            Tmp%=b;
        } tmp*=m;
        while (Len>1&&!c.w[len]) len--;
    return C; BOOL Operator < (cOnst bigint &b) const {bigint a=*this;
        if (A.w[0]^b.w[0]) return a.w[0]<b.w[0];
        for (int i = a.w[0];i;i--) if (A.w[i]^b.w[i]) return a.w[i]<b.w[i];
    return false;
        } void Print () {printf ("%d", w[w[0]]);
    for (int i = w[0]-1;i;i--) printf ("%0*d", P,w[i]);
}}l,r;
int n;
    struct II {int l,r;
    II (int l =0,int r=0): L (L), R (r) {} BOOL operator < (CONST II &b) Const {return l*r<b.l*b.r;
}}node[n];
    int main () {Freopen (frop ". In", "R", stdin);
    Freopen (Frop ". Out", "w", stdout);
    scanf ("%d", &n);
    L.read ();
    R.read ();
        for (int i = 1;i <= n; i++) {int x,y;
        scanf ("%d%d", &x,&y);
    Node[i]=ii (X,y);
    Sort (node+1,node+n+1);
    BigInt ans,tmp=l;
        for (int i = 1; I <= n; i++) {bigint X=TMP/NODE[I].R;
        if (ans<x) ans=x;
    TMP=TMP*NODE[I].L;
    } ans.print ();
return 0;
 }

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.