题目链接: 点这里
使用方法: 微扰
得到以上排序性质后, 编写排序方法, 对大臣进行排序即可
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
using namespace std;
struct Node {
int a, b;
};
int cmp(const Node &a, const Node &b) {
return a.a * a.b < b.a * b.a;
}
int main() {
int n;
cin >> n;
vector<Node> v;
for (int i = 0, a, b; i <= n; i++) {
cin >> a >> b;
v.push_back(Node {a, b});
}
sort(v.begin() + 1, v.end(), cmp);
int ans = 0, p = v[0].a;
for (int i = 1; i < v.size(); i++) {
int t = p / v[i].b;
if (t > ans) ans = t;
p *= v[i].a;
}
cout << ans << endl;
return 0;
}
由于可能数值比较大, 超过了long long的表示范围, 这里可以考虑使用大整数
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
using namespace std;
struct Node {
int a, b;
};
bool cmp(const Node &a, const Node &b) {
return a.a * a.b < b.a * b.b;
}
class BigInt : public vector<int> {
public:
BigInt(int x) {
push_back(x);
process_digit();
}
bool operator<(const BigInt &a) {
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;
}
void operator*=(int x) {
for (int i = 0; i < size(); i++) at(i) *= x;
process_digit();
return ;
}
void 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 operator/(int x) {
BigInt ret(*this);
ret /= x;
return ret;
}
private:
void 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;
}
};
ostream &operator<<(ostream &out, const BigInt &a) {
for (int i = a.size() - 1; i >= 0; i--) {
out << a[i];
}
return out;
}
int main() {
int n;
cin >> n;
vector<Node> v;
for (int i = 0, a, b; i <= n; i++) {
cin >> a >> b;
v.push_back(Node{a, b});
}
sort(v.begin() + 1, v.end(), cmp);
BigInt ans = 0, p = v[0].a;
for (int i = 1; i < v.size(); i++) {
BigInt t = p / v[i].b;
if (ans < t) ans = t;
p *= v[i].a;
}
cout << ans << endl;
return 0;
}