P1080 Kings game (greedy + high-precision multiply and divide large numbers and compare)

Copyright: private individuals do question summed up ~ https://blog.csdn.net/tb_youth/article/details/89578721

https://www.luogu.org/problemnew/show/P1080
subject description
coincides with H 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: the product of the row on the left hand in front of all the ministers of the number divided by the number on his right hand, the result is then rounded down integer obtained.

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 Output Format
Input format:
The first line contains an integer n, the number of minister.

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:
An integer representing the team re-arrangement after winning the most gold coins reward minister obtained.

Input Output Sample
Input Sample # 1:

3 
1 1 
2 3 
7 4 
4 6 

Output Sample # 1:

2

DESCRIPTION
[O] Sample Description

Chancellor Press 1,2,3 arranged such teams rewarded largest number of obtained coins is 2;

Chancellor Press 1,3,2 arranged such teams rewarded largest number of obtained coins is 2;

Chancellor Press 2,1,3 arranged such teams rewarded largest number of obtained coins is 2;

Press 2,3, 1 arranged such team, the largest number of coins obtained by the Minister of reward 9;

Such teams are arranged by 3,1, 2, rewarded the most number of coins is obtained by Chancellor 2;

3,2,1 minister by this arrangement team, rewarded the most number of gold coins obtained 9.

Therefore, the most rewarding Minister least get two gold coins, the answer output 2.

【data range】

For 20% of the data, there 1≤ n≤ 10,0 <a, b <8;

For 40% of the data, there 1≤ n≤20,0 <a, b <8;

For 60% of the data, there 1≤ n≤100;

For 60% of the data, to ensure that the answer does not exceed 10 ^ 9;

To 100% of the data, there 1≤n≤1,000,0 <a, b <10000.

Improve the group NOIP 2012 the first day of the second question
Ac_code:
/ *
For chestnuts:

3
1 1
2 100
3 50
4 1

Chestnuts ... plus the product of reasoning should draw little standing in the front, plus the product of the maximum value of nearly 10 ^ 4000
so even high-precision computing '!
* /

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
char aim[4500],temp[4500];
char c1[4500],c2[4500],ans[4500];
struct person
{
    int r;
    int l;
    bool operator<(const person &x)const
    {
        return l*r < x.l*x.r;//乘积小的排在前面
    }
};
void multi(char tmp[],int x)//乘法
{
    int len  = strlen(tmp),t,y;
    int k = 1,s = 0;
    memset(aim,'\0',sizeof(aim));
    while(x)
    {
        t = x%10;
        y = 0;
        k = s;
        for(int i = len-1; i>=0; i--)
        {
            int p = (tmp[i]-'0')*t + y;
            if(aim[k]=='\0') aim[k] = '0';
            y = (aim[k]-'0' + p)/10;
            aim[k] = (aim[k]-'0' + p)%10+'0';
            k++;
        }
        if(y) aim[k++] = y+'0';
        ++s;
        x /= 10;
    }
    for(int i = k-1,j = 0; i >= 0; i--)
    {
        tmp[j++] = aim[i];
    }
}
void divs(char *tmp,int x)//除法
{
    int len = strlen(tmp);
    memset(c1,'\0',sizeof(c1));
    int k = 0;
    int num = 0;
    for(int i = 0; i < len; i++)
    {
        num = num*10+(tmp[i]-'0');
        if(num >= x)
        {
            c1[k] = num / x+'0';
            num = num % x;
        }
        else
        {
            c1[k] = '0';
        }
        k++;
    }
    int s = 0;
    while(c1[s]=='0')
        s++;
    memset(c2,'\0',sizeof(c2));
    if(s==k)
    {
        c2[0] = '0';
    }
    else
    {
        strcpy(c2,c1+s);
    }
}
bool cmp(char *a,char *b)//大数比较
{
    int lena = strlen(a),lenb = strlen(b);
    if(lena == lenb)
    {
        return strcmp(a,b)>0;
    }
    return lena > lenb;
}
int main()
{
    //std::ios::sync_with_stdio(0);
    //cin.tie(0);
    int n;
    cin>>n;
    person a[n+2];
    for(int i = 0; i <= n; i++)
    {
        cin>>a[i].l>>a[i].r;
    }
    sort(a+1,a+n+1);
    temp[0] = '1';
    ans[0] = '0';
    for(int i = 1; i <= n; i++)
    {
        multi(temp ,a[i-1].l);
        divs(temp,a[i].r);
        if(!cmp(ans,c2))
        {
            strcpy(ans,c2);
        }
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/tb_youth/article/details/89578721