题目描述
恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这 n 位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。
国王不希望某一个大臣获得特别多的奖赏,所以他想请你帮他重新安排一下队伍的顺序,使得获得奖赏最多的大臣,所获奖赏尽可能的少。注意,国王的位置始终在队伍的最前面。
输入格式
第一行包含一个整数 n,表示大臣的人数。
第二行包含两个整数 a 和 b,之间用一个空格隔开,分别表示国王左手和右手上的整数。
接下来 n 行,每行包含两个整数 a 和 b,之间用一个空格隔开,分别表示每个大臣左手和右手上的整数。
输出格式
一个整数,表示重新排列后的队伍中获奖赏最多的大臣所获得的金币数
input
3
1 1
2 3
7 4
4 6
output
2
input
3
1 7
6 1
2 3
2 3
output
4
code
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
int N;
vector<pair<int, int>> a;
vector<int> pdt;
void multi(int x)
{
for(int i=0; i<pdt.size(); i++)
{
pdt[i] *= x;
}
for(int i=0; i<pdt.size(); i++)
{
if(pdt[i] > 9)
{
if(i == pdt.size() - 1) pdt.push_back(pdt[i] / 10);
else pdt[i+1] += pdt[i] / 10;
pdt[i] %= 10;
}
}
}
string div(int x)
{
string re;
vector<int> temp(pdt);
for(int i=temp.size()-1; i>=0; i--)
{
re += temp[i] / x + '0';
if(temp[i] % x)
{
if(i == 0) break;
else temp[i-1] += temp[i] % x * 10;
}
}
while(re.front() == '0') re.erase(re.begin());
return re;
}
string Max(string x, string y)
{
if(x.size() == y.size()) return (x > y ? x : y);
else return (x.size() > y.size() ? x : y);
}
int main()
{
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> N; N++;
for(int i=0; i<N; i++)
{
int x, y; cin >> x >> y;
a.push_back({x, y});
}
sort(a.begin() + 1, a.end(), [](pair<int, int> a, pair<int, int> b){return a.first * a.second < b.first * b.second;});
pdt.push_back(1);
string ans = "0";
for(int i=0; i<N; i++)
{
if(i!=0) ans = Max(ans, div(a[i].second));
multi(a[i].first);
}
cout << ans << endl;
return 0;
}