题目描述
恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这 n 位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。
国王不希望某一个大臣获得特别多的奖赏,所以他想请你帮他重新安排一下队伍的顺序,使得获得奖赏最多的大臣,所获奖赏尽可能的少。注意,国王的位置始终在队伍的最前面。
输入
第一行包含一个整数 n,表示大臣的人数。
第二行包含两个整数 a 和 b,之间用一个空格隔开,分别表示国王左手和右手上的整数。(均小于 10000)
接下来 n 行,每行包含两个整数 a 和 b,之间用一个空格隔开,分别表示每个大臣左手和右手上的整数。(均小于 10000)
输出
输出一个整数,表示重新排列后的队伍中获奖赏最多的大臣所获得的金币数。
代码实现
BigInt
数据结构的实现,为了实现大整数运算- 涉及到的算法没有变化
- 注意理解代码中const的实现。
#include <iostream>
#include <cmath>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef pair<int, int> PII;
class BigInt : public vector<int> {
public:
BigInt(int x);
void operator*=(int x);
void operator/=(int x);
BigInt operator/(int x);
bool operator<(const BigInt &)const;
bool operator>(const BigInt &)const;
private:
void process_digit();
};
ostream &operator<<(ostream &out, const BigInt &a) {
for (int i = a.size() - 1; i >= 0; i--) {
out << a[i];
}
return out;
}
BigInt::BigInt(int x) {
push_back(x);
process_digit();
}
void BigInt::process_digit() {
for (int i = 0; i < size(); i++) {
if (at(i) < 10) continue;
if (i + 1 == size()) push_back(0);
at(i + 1) += at(i) / 10;
at(i) %= 10;
}
while (size() > 1 && at(size() - 1) == 0) pop_back();
return ;
}
void BigInt::operator*=(int x) {
for (int i = 0; i < size(); i++) at(i) *= x;
process_digit();
return ;
}
void BigInt::operator/=(int x) {
int y = 0;
for (int i = size() - 1; i >= 0; i--) {
y = y * 10 + at(i);
at(i) = y / x;
y %= x;
}
process_digit();
return ;
}
BigInt BigInt::operator/(int x) {
BigInt ret(*this);
ret /= x;
return ret;
}
bool BigInt::operator<(const BigInt &a) const {
if (size() - a.size()) return size() < a.size();
for (int i = size() - 1; i >= 0; i--) {
if (at(i) - a[i]) return at(i) < a[i];
}
return false;
}
bool BigInt::operator>(const BigInt &a) const {
return a < (*this);
}
int main(int argc, char** argv)
{
vector<PII> arr;
int n;
cin >> n;
for(int i = 0, a,b; i <= n; i++) {
cin >> a >> b;
arr.push_back(PII(a, b));
}
sort(arr.begin() + 1, arr.end(),
[](const PII &x, const PII &y) {
return x.first * x.second < y.first * y.second;
}
);
BigInt p = arr[0].first, ans = 0;
for (int i = 1; i <= n; i++) {
BigInt temp = p / arr[i].second;
ans = max(ans, temp);
p *= arr[i].first;
}
cout << ans << endl;
return 0;
}
后记
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)