Luo Gu p1080 Kings game (precision + greedy)

Title Description

Coincides with  H H National Day, the king invited n 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  the n- 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 Format

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

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

Next,  n- n-rows, each row comprising two integers A A and  B B, separated by a space between, each represent an integer minister on each left and right.

Output Format

An integer representing the team after winning re-arranged tours in the most number of gold coins minister obtained.

Sample input and output

Entry
3 
1 1 
2 3 
7 4 
4 6 
Export 
2 

greedy proof: Let the king is king, Minister for p1, p2.
Countable r0 of King L0 L0 r0 of King

   P1 P2 L1 L2 R1 R2
   P2 L1 L2 R1 R2 P1

is k1 = l0 / r1, k2 = l0 * l1 / r2, k3 = l0 / r2, k4 = l0 * l2 / r1.
ans1 = max (k1, k2) , ans2 = max (k3, k4);
apparently k2> k3, k4> k1, so if ans1> ans2, the k2> k4, i.e. l1 * r1> i2 * r2;
therefore when the sort press a * b is the priority can be.

(As precision op board, no more explanation)
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <vector>

#define ll long long
using namespace std;

int n, lens = 1, lenm = 1, lena = 1;
int sum[10010] = {0, 1}, MAXN [ 10010 ] = { 0 , . 1 }, ANS [ 10010 ]; 

struct of { 
    LL L, R & lt; 
    BOOL  operator <( const of X) const 
    { 
        return L * R & lt <XL * XR;      // press coins left and right product of prioritizing 
    } 
} COIN [ 1001 ]; 

void Times (X LL)     // precision multiplication 
{
     int T = 0 ;
     for ( int I = . 1 ; I <= Lens; I ++ ) 
        SUM [I] * = x;
    for(int i = 1; i <= lens; i++)
    {
        t += sum[i];
        sum[i] = t % 10;
        t /= 10;
    }
    while(t != 0)
    {
        lens++;
        sum[lens] = t % 10;
        t /= 10;
    }
}

void div(ll x)         //高精度除法
{
    memset(ans, 0, sizeof(ans));
    lena = lens;
    int t = 0;
    for(int i = lens; i >= 1; i--)
    {
        t *= 10;
        t += sum[i];
        if(t >= x)
        {
            ans[i] = t /x;
            t %= x;
        }
    }
    while(ans[lena] == 0)
    {
        if(lena == 1)
            break;
        lena--;
    }
}

void max()        //高精度比较
{
    if(lena > lenm)
    {
        for(int i = 1; i <= lena; i++)
            maxn[i] = ans[i];
        lenm = lena;
    }
    else if(lena == lenm)
    {
        for(int i =lena; i >= 1; i--)
            if(maxn[i] < ans[i])
            {
                for(int j = 1; j <= lena; j++)
                    maxn[j] = ans[j];
                lenm = lena;
                break;
            }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    cin >> coin[0].l >> coin[0].r;
    for(int i = 1; i <= n; i++)
        cin >> coin[i].l >> coin[i].r;
    sort(coin + 1, coin + n + 1);
    for(int i = 1; i <=n; i++)
    {
        times(coin[i - 1].l);
        div(coin[i].r);
        max();
    }
    for(int i = lenm; i >= 1; i--)
        cout << maxn[i];
    return 0;
}

 



Guess you like

Origin www.cnblogs.com/zxybdnb/p/11443962.html