1. 程式人生 > 實用技巧 >國王遊戲

國王遊戲

國王真會玩

\[king~~l_0~~r_0\\ 1~~l_1~~r_1\\ 2~~l_2~~r_2\\ l_1:l_0/r1或者(l_0*l_2)/r1\\ l_2:(l_0*l_1)/r_2或者l_0/r_2\\ ans = max(\frac{l_0}{r_1},\frac{l_0*l_1}{r_2})或max(\frac{l_0*l_2}{r_1},\frac{l_0}{r_2})\\ 顯然l_0*l_2\ge l_0\\ 如果ans1>ans2~~\frac{l_0*l_1}{r_2}>\frac{l_0*l_2}{r_1}\\ l_1*r_1>l_2*r_2 \]

按此排序即可,giao精

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxe 10001
using namespace std;
int n,lens = 1,lenm = 1,lena = 1;
int sum[maxe] = {0,1},maxn[maxe] = {0,1},ans[maxe];
struct tmp{
	int l,r;
	bool operator < (const tmp x){//
	return l * r < x.l * x.r;
	}
}coin[maxe];
inline void muti(int x){
	int tmp = 0;
	for(int i = 1; i <= lens; i++) sum[i] *= x;
	for(int i = 1; i <= lens; i++){
		tmp += sum[i];
		sum[i] = tmp %10;
		tmp /= 10;
	}
	while(tmp != 0){
		lens++;
		sum[lens] = tmp % 10;
		tmp /= 10;
	}
}
void cut(long long x)
{
	memset(ans, 0, sizeof(ans));
	lena = lens;
	int tmp = 0;
	for(int i = lena; i >= 1; i--)
	{
		tmp *= 10;
		tmp += sum[i];
		if(tmp >= x){
			ans[i] = tmp / x;
			tmp %= x;
		}
	}
	while(ans[lena] == 0){
		if(lena == 1)
			break;
		lena--;
	}
}
void max(){
	if(lena > lenm){
		for(int i = 1; i <= lena; i++)
			maxn[i] = ans[i];
		lenm = lena;
	}
	else if(lena == lenm){
		for(int i = lena; i >= 1; i--)
			if(maxn[i] < ans[i]){
				for(int j = 1; j <= lena; j++)
					maxn[j] = ans[j];
				lenm = lena;
				break;
			}
	}
}

int main(){
	cin >> n;
	cin >> coin[0].l >> coin[0].r;
	for(int i = 1; i <= n; i++)
		scanf("%d %d", &coin[i].l, & coin[i].r);
	sort(coin + 1, coin + n + 1);
	for(int i = 1; i <= n; i++)
	{
		muti(coin[i - 1].l);
		cut(coin[i].r);
		max();
	}
	for(int i = lenm; i >= 1; i--)
		cout << maxn[i];
}