# Kings game (greedy + + multiplication and division of large numbers perturbation method to prove)

King's game (greedy + + multiplication and division of large numbers perturbation method to prove)

  • Meaning of the questions: n was a minister and a king, has a number of right-hand man, queuing, the king had been shot in the front, each minister's award is the product of the number of ministers on the front of this man's left hand on the Minister divided by the right hand number. Optimal team structure, making the most of the ministers of the least reward incentives.

Proof: using perturbation method

We will now swap the position of any two people, then consider rewarding them before switching and after switching obtained is how much other people's rewards unchanged:

Before the exchange:
first \ (I \) Personal is: \ (A_1 A_0 * * .... *. 1-A_ {I} / B_i \)
of \ (i + 1 \) Personal is: \ (A_1 A_0 * * .... * A_ {i} / B_ {i + 1} \)

After the exchange:
first \ (I \) Personal is: \ (A_1 A_0 * * .... *. 1-A_ {I} / I {B_. 1} + \)
of \ (i + 1 \) Personal is: \ (A_0 * A_1 * .... * A_ {i-1} * A_ {i + 1} / B_i \)

Comparison before and after exchange of only two individuals in maximum, after the extraction is to compare the common factor \ (max (B_ {i + 1}, A_i * B_i) \) and \ (max (B_i, A_ { i + 1 } * B_ {i + 1} ) \)

Since the Minister hands are positive, so \ [B_ {I +. 1} <B_ {I +. 1} * A_ {I +. 1} \] \ [A_ {I} * B_ {I}> B_i \] Thus, when \ (A_i B_i * <= {I + A_ B_. 1} * {I}. 1 + \) , the left and right type of formula is less than, more preferably before the exchange, and vice versa after switching better, that is to say, in any situation, to reduce the reverse do not make for poor overall results, increasing the reverse of the overall result will deteriorate. According thought bubble sort, from small to large sorted sequence is the optimal solution.

The time complexity of
sorting time complexity is \ (O} {nlogn \) .
This question object time complexity bottleneck in high-precision calculations, the worst case all \ (9999 A_i = \) , then the product number of the former is about 4i i bits by a new number every time it is necessary to 4i calculation, the total calculation amount is \ (4 * \ sum_ {n = 1} ^ i {i} = O {n2} \)

AC code

#include <bits/stdc++.h>
using namespace std;
//宏定义,编译时展开,占用编译时间
#define fre freopen("C:\\Users\\22765\\Desktop\\in.txt","r",stdin);
#define mem(a) memset((a),0,sizeof(a))
#define re(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define sf(x) scanf("%d",&(x))
typedef long long LL;
const int inf=(0x7f7f7f7f);
const int maxn=1004;
typedef pair<int,int> pii;
int n;
pii p[maxn];
vector<int> ans,MAX,anst;
vector<int> bmul(int a){
    vector<int> c;
    int t=0;
    for(int i=0;i<ans.size();i++){
        t+=(ans[i]*a);
        c.push_back(t%10);
        t/=10;
    }
    while(t){
        c.push_back(t%10);
        t/=10;
    }

    return c;
}

vector<int> bdiv(int a){
    vector<int> c;
    int t=0;
    bool is_first=1;
    for(int i=ans.size()-1;i>=0;i--){
        t=t*10+ans[i];
     
        int x=t/a;
        //注意这里判断,除法过程中,首部的0无效,中间的0是有效数字
        //000123045 ,000是无效数字,304中间的0是有效数字 
        if(!is_first||x){
            is_first=0;
            c.push_back(x);
        }
        t%=a;
        
    }
    if(c.empty())c.push_back(0);
    reverse(c.begin(),c.end()) ;
    return c;
}
bool judge(){//ans>MAX?
    if(anst.size()>MAX.size())return true;
    if(anst.size()<MAX.size())return false;
    for(int i=anst.size()-1;i>=0;i--){
        if(anst[i]>MAX[i])return true;
        if(anst[i]<MAX[i])return false;
    }
    return false;//==
}
int main(){

    sf(n);
    int a,b;
    re(i,0,n+1){
        sf(a);sf(b);
        p[i]=make_pair(a*b,a);
    }
    
    sort(p+1,p+n+1);
    a=p[0].second;
    while(a){
        ans.push_back(a%10);
        a/=10; 
    }
    
    for(int i=1;i<=n;i++){

        anst=bdiv(p[i].first/p[i].second);

        ans=bmul(p[i].second);

        if(judge()){
            MAX=anst;
        }
    }

    for(int i=MAX.size()-1;i>=0;i--)printf("%d",MAX[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/sstealer/p/11209320.html