Luogu P1080 king game

Posted by iwarlord on Mon, 03 Jan 2022 22:50:00 +0100

Title Description

On the national day of HH, the king invited NN ministers to play a prize game. First, he asked each minister to write an integer on his left and right hands, and the king himself wrote an integer on his left and right hands. Then let the}nn ministers line up and the king stand at the front of the line. After lining up, all ministers will receive several gold coins rewarded by the king. The number of gold coins received by each minister is: the product of the number on the left hand of everyone in front of the minister divided by the number on his own right hand, and then rounded down.

The king doesn't want a minister to get a lot of rewards, so he wants you to help him rearrange the order of the team, so that the minister who gets the most rewards will get as few rewards as possible. Note that the king is always at the front of the team.

Input format

The first line contains an integer nn representing the number of ministers.

The second line contains two integers, {aa and} bb, separated by a space, representing the integers on the king's left hand and right hand, respectively.

Next, the ^ nn line contains two integers aa ^ and ^ bb, separated by a space, representing the integers on each minister's left hand and right hand respectively.

Output format

An integer indicating the number of gold coins won by the minister who won the most reward in the rearranged team.

Input and output samples

Enter #1 copy

3 
1 1 
2 3 
7 4 
4 6 

Output #1 copy

2

Description / tips

[description of input and output examples]

According to the arrangement of 11, 22 and 33 , the minister who won the most reward received the number of gold coins is , 22;

Rank according to , 11,33,22 , and the minister who gets the most reward will get 22 gold coins;

Rank according to , 22,11,33 , and the minister who gets the most reward will get , 22 gold coins;

According to the ranks of 22, 33 and 11, the minister who won the most reward received 99 gold coins;

Rank according to , 33, 11 and 22. The minister who gets the most reward will get , 22 gold coins;

According to the arrangement of 33, 22 and 11 , the minister who won the most reward received 99 , gold coins.

Therefore, the minister with the most reward received at least 22 gold coins, and the answer was 22.

[data range]

For 20% of the data, there are 1 ≤ n ≤ 10,0 < A, B < 81 ≤ n ≤ 10,0 < A, B < 8;

For 40% of the data, there are 1 ≤ n ≤ 20,0 < A, B < 81 ≤ n ≤ 20,0 < A, B < 8;

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

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

For 100% data, there are 1 ≤ n ≤ 1000,0 < A, B < 100001 ≤ n ≤ 1000,0 < A, B < 10000.

Second question of the first day of NOIP 2012 improvement group

Upper Code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int n, lens = 1, lenm = 1, lena = 1;
int sum[10010] = {0, 1}, maxn[10010] = {0, 1}, ans[10010];

struct tmp
{
	long long l, r;
	bool operator < (const tmp x) const 
	{
			return l * r < x.l * x.r;
	}
}coin[1001];

void muti(long long x)
{
	int tmp = 0;
	for(int i = 1; i <= lens; i++)
		sum[i] *= x;
	for(int i = 1; i <= lens; i++)
	{
		tmp += sum[i];
		sum[i] = tmp %10;
		tmp /= 10;
	}
	while(tmp != 0)
	{
		lens++;
		sum[lens] = tmp % 10;
		tmp /= 10;
	}
}

void cut(long long x)
{
	memset(ans, 0, sizeof(ans));
	lena = lens;
	int tmp = 0;
	for(int i = lena; i >= 1; i--)
	{
		tmp *= 10;
		tmp += sum[i];
		if(tmp >= x)
		{
			ans[i] = tmp / x;
			tmp %= 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()
{
// 	freopen("game.in", "r", stdin);
// 	freopen("game.out", "w", stdout);
	cin >> n;
	cin >> coin[0].l >> coin[0].r;
	for(int i = 1; i <= n; i++)
		scanf("%d %d", &coin[i].l, & coin[i].r);
	sort(coin + 1, coin + n + 1);
	for(int i = 1; i <= n; i++)
	{
		muti(coin[i - 1].l);
		cut(coin[i].r);
		max();
	}
	for(int i = lenm; i >= 1; i--)
		cout << maxn[i];
}

Topics: C++