[YBT high-efficiency advanced] 1 basic algorithm/2 greedy algorithm/4 king game&[Lokgu] P1080 king game

[YBT high-efficiency advanced] 1 basic algorithm/2 greedy algorithm/4 king game&[Lokgu] P1080 king game

Title description

It coincided with the National Day of H, and the king invited n ministers to play a game with prizes. 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 n ministers line up in a row, with the king standing at the forefront 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 numbers on the left hand of all the ministers in front of the line divided by the number on his own right hand. Then round down the result.

The king does not want a certain minister to receive particularly many rewards, so he would like to ask you to help him rearrange the order of the team so that the minister who receives the most rewards will receive as few rewards as possible. Note that the position of the king is always at the forefront of the team.

Input format

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

The second line contains two integers, a and b, separated by a space, representing the integers in the king's left and right hands.

In the next n lines, each line contains two integers a and b, separated by a space, representing the integers on the left and right hands of each minister.

Output format

An integer representing the number of gold coins obtained by the minister with the most prizes in the rearranged team.

Sample input and output

Enter #1 to copy

3
1 1
2 3
7 4
4 6

Output #1 copy

2

Instructions/tips

[Instructions of input and output samples]

Arrange the teams according to 123, and the number of gold coins won by the minister with the most reward is 2;

Arrange the teams according to 132, and the number of gold coins won by the minister with the most reward is 2;

Arrange the teams according to 213, and the number of gold coins won by the minister with the most reward is 2;

Arrange the teams according to 231, and the minister with the most rewards will get 9 gold coins;

Arrange the teams as 312, and the number of gold coins won by the minister with the most reward is 2;

Arrange the teams in 321, and the minister with the most rewards will get 9 gold coins.

Therefore, the minister with the most rewards will get at least 2 gold coins, and the answer will be 2.

【data range】

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

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

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

For 60% of the data, ensure that the answer does not exceed 10 9
For 100% of the data, 1≤n≤1000, 0<a, b<10000.

Ideas

Sort the product of a i *b i from small to large.
Find the biggest reward in this order.

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int ans[10010],answer[10010],len=1;
struct jgt
{
    
    
	int l,r,w;
}a[10010];
bool cmp(jgt t1,jgt t2)
{
    
    
	return t1.w<t2.w;
}
void mul(int x)//高精乘 
{
    
    
	int g=0,i;
	for(i=1;i<=len;i++)
	{
    
    
		ans[i]=ans[i]*x+g;
		g=ans[i]/10;
		ans[i]=ans[i]%10;
	}
	for(;g;g/=10)
		ans[++len]=g%10;
	return;
}
void divb(int x)//高精除 
{
    
    
	int i,g=0;
	for(i=len;i>=1;i--)
	{
    
    
		answer[i]=(g*10+ans[i])/x;
		g=(g*10+ans[i])%x;
	}
	for(i=len;i>=1;i--)
		if(answer[i]!=0)
		{
    
    
			len=i;
			return;
		}
	printf("1");
	len=0;
	return;
}
void output()
{
    
    
	int i;
	for(i=len;i>=1;i--)
		printf("%d",answer[i]);
	return;
}
int main()
{
    
    
	int n,i,j;
	ans[1]=1;
	scanf("%d",&n);
	for(i=0;i<=n;i++)
		scanf("%d%d",&a[i].l,&a[i].r),a[i].w=a[i].l*a[i].r;
	sort(a+1,a+1+n,cmp);//将左右手的积从小到大排序 
	for(i=0;i<n;i++)
		mul(a[i].l);
	divb(a[n].r);
	output();
	return 0;
}```

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/113048847