Luogu P1080 [NOIP2012 improvement group] king game (greedy, high precision)

[Title Description]
Coincides H H H national day, invited by the king n n n ministers come 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 this n n n ministers lined up, and the king stood 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 n n n. Indicates the number of ministers.
The second line contains two integers a a a and b b b. Separated by a space, it represents the integer on the king's left hand and right hand respectively.
next n n n rows, each containing two integers a a a and b b b. Separated by a space, it represents 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.

[data range]
1 ≤ n ≤ 1 , 000 , 0 < a , b < 10000 1≤n≤1,000,0<a,b<10000 1≤n≤1,000,0<a,b<10000

[input example]

3
1 1
2 3
7 4
4 6

[output example]

2

[analysis]

according to a ∗ b a*b Sorting a * b from small to large is the optimal solution, and high-precision calculation is required for multiplication and division.

[Code]

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

typedef pair<int, int> PII;
const int N = 1010;
vector<int> sum, res;
PII item[N];
int n;

//High precision sum / x
vector<int> eval_div(vector<int> sum, int x)
{
	vector<int> res;
	int r = 0;
	for (int i = sum.size() - 1; i >= 0; i--)
	{
		r = r * 10 + sum[i];
		res.push_back(r / x);
		r %= x;
	}
	reverse(res.begin(), res.end());
	while (res.size() > 1 && res.back() == 0) res.pop_back();//To leading 0
	return res;
}

//High precision sum * x
vector<int> eval_mul(vector<int> sum, int x)
{
	vector<int> res;
	int t = 0;
	for (int i = 0; i < sum.size() || t; i++)
	{
		if(i < sum.size()) t += sum[i] * x;
		res.push_back(t % 10);
		t /= 10;
	}
	while (res.size() > 1 && res.back() == 0) res.pop_back();//To leading 0
	return res;
}

int main()
{
	cin >> n;
	string a, b;
	cin >> a >> b;
	for (int i = a.size() - 1; i >= 0; i--)
		sum.push_back(a[i] - '0');
	for (int i = 0; i < n; i++)
	{
		cin >> item[i].first >> item[i].second;
		item[i].first *= item[i].second;
	}
	sort(item, item + n);//Sort by a * b from small to large
	for (int i = 0; i < n; i++)
	{
		int right = item[i].second, left = item[i].first / right;
		auto tmp = eval_div(sum, right);
		if (tmp.size() > res.size()) res = tmp;
		if (tmp.size() == res.size())
			for (int i = res.size() - 1; i >= 0; i--)
				if (tmp[i] > res[i]) res = tmp;
		sum = eval_mul(sum, left);
	}
	for (int i = res.size() - 1; i >= 0; i--) cout << res[i];
	return 0;
}

Tags: C++ Algorithm greedy algorithm

Posted on Thu, 21 Oct 2021 21:06:40 -0400 by janm2009