题目链接
http://noi-test.zzstep.com/contest/0x00%E3%80%8C%E5%9F%BA%E6%9C%AC%E7%AE%97%E6%B3%95%E3%80%8D%E4%BE%8B%E9%A2%98/0701%20%E5%9B%BD%E7%8E%8B%E6%B8%B8%E6%88%8F
分析
贪心,考虑相邻两位大臣,分别列出他们站位靠前的答案,比较可知,令 a × b a \times b a×b 小的大臣站得靠前答案更优。
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int read() {
int num = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9')
num = num * 10 + c - '0', c = getchar();
return num;
}
const int maxn = 1e3 + 5, maxb = 5e3 + 5;
struct People {
int l, r;
bool operator < (const People& rhs) const {
return l * r < rhs.l * rhs.r;
}
} p[maxn];
struct BigInteger {
int num[maxb], len;
BigInteger(int x = 0) {
memset(num, 0, sizeof(num));
len = 1;
while (x) num[len++] = x % 10, x /= 10;
if (len > 1) --len;
}
bool operator < (const BigInteger& rhs) const {
if (len == rhs.len) for (int i = len; i >= 1; --i)
if (num[i] != rhs.num[i]) return num[i] < rhs.num[i];
return len < rhs.len;
}
BigInteger operator * (const int& rhs) const {
BigInteger ans = *this;
for (int i = 1; i <= ans.len; ++i) ans.num[i] *= rhs;
for (int i = 1; i <= ans.len; ++i)
for (int j = i; ans.num[j] > 9; ++j)
ans.num[j + 1] += ans.num[j] / 10, ans.num[j] %= 10;
ans.len += 10;
while (!ans.num[ans.len] && ans.len > 1) --ans.len;
return ans;
}
BigInteger operator / (const int& rhs) const {
BigInteger ans = *this;
for (int i = ans.len; i >= 1; --i) {
ans.num[i - 1] += ans.num[i] % rhs * 10;
ans.num[i] /= rhs;
}
while (!ans.num[ans.len] && ans.len > 1) --ans.len;
return ans;
}
void print() {
for (int i = len; i >= 1; --i) printf("%d", num[i]);
}
};
int main() {
int n = read();
for (int i = 0; i <= n; ++i) p[i].l = read(), p[i].r = read();
sort(p + 1, p + n + 1);
BigInteger now(p[0].l), ans;
for (int i = 1; i <= n; ++i) {
if (ans < now / p[i].r) ans = now / p[i].r;
now = now * p[i].l;
}
ans.print();
return 0;
}