King's game (perturbation greedy plus precision)

H coincides with National Day, the king invited the Minister of n bits to play a game with prizes.

First, he let each minister in the left and right hand on top of each wrote an integer king himself was left, each to write an integer on the right hand.

Then, let the n ministers in a row, the king stood in the front ranks.

After lined up, all the ministers will receive a number of gold coins king reward, the number of coins for each minister were obtained:

Front row on the left hand of the Minister for all the product divided by the number his number on their right hand, and then take the results of the whole get down.

The king does not want a minister to get a particularly large prize, so he would like to ask you to help him rearrange the order of the team, so to get the most reward minister, received the reward as little as possible.

Note that the king's position has always been in the front ranks.

Input Format

The first line contains an integer n, the number of ministers.

The second line contains two integers a and B, separated by a space between, each represent an integer of left and right on the king.

Next n lines, each line contains two integers a and B, separated by a space between, each represent an integer minister on each left and right.

Output Format

Output only one line containing an integer representing the number of gold coins after the team re-arrangement of the most award-winning tours minister obtained.

data range

1n10001≤n≤1000
0<a,b<100000<a,b<10000

Sample input:

3
1 1
2 3
7 4
4 6

Sample output:

2

代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
struct node{
    int l,r;
};
node a[maxn];
vector<int> now,ans;
int cmp(const node &x,const node &y)
{
    if(x.l*x.r==y.l*y.r)
    return x.r<y.r;
    return (x.l*x.r<y.l*y.r);
}
vector<int> mul(vector<int> a,int x)
{
    reverse(a.begin(),a.end());
    for(int i=0;i<a.size();i++)
    {
        a[i]=a[i]*x;
    }
    for(int i=0;i<a.size();i++)
    {
        if(i<a.size()-1)
            a[i+1]=a[i+1]+a[i]/10;
        else if(a[i]>=10)
            a.push_back(a[i]/10);
        a[i]=a[i]%10;
    }
    reverse(a.begin(),a.end());
    return a;
}
vector<int> dis(vector<int> a,int x)
{
    vector<int> c;
    int ok=1,s=0;
    for(int i=0;i<a.size();i++)
    {
        s=s*10+a[i];
        if(!ok||s>=x)
        {
            ok=0;
            c.push_back(s/x);
        }
        s=s%x;    
    }
    return c;
}
vector<int> max_2(vector<int> a,vector<int> b)
{
    if(a.size()!=b.size())
    return a.size()>b.size()?a:b;
    return a>b?a:b;
}
void out(vector<int> ans)
{
    for(int i=0;i<ans.size();i++)
    {
        cout<<ans[i];
     } 
     cout<<"\n";
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<=n;i++)
    {
        cin>>a[i].l>>a[i].r;
    }
    sort(a+1,a+1+n,cmp);
    now.push_back(1);
    for(int i=1;i<=n;i++)
    {
        now=mul(now,a[i-1].l);
        ans=max_2(ans,dis(now,a[i].r));
    }
    out(ans);
    return 0;
 } 

 

 

Guess you like

Origin www.cnblogs.com/hh13579/p/11288985.html