NOIP2012 国王游戏 贪心

题目链接

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值