国王游戏

解题思路

先将所有大臣 a i ∗ b i a_i * b_i aibi 从小到大排序
贪心求答案

AC代码

#include<cstdio>
#include<algorithm>
using namespace std;
int n,tot,tot2,b[100005],answer[100005];
struct node
{
   
	int l,r;
}a[1005];
bool cmp(node x,node y)
{
   
	return x.l*x.r<y.l*y.r;
}
void gjd(int o)
{
   
	int c[100005]={
   },k=0,tot1=0;
	for(int i=tot;i>=1;i--)
	{
   
		k=k*10+b[i];
		while(k>=a[o].r)
		{
   c[i]++;k-=a[o].r;}
		if(c[i]&&!tot1)tot1=i;	
	} 
	if(tot1>=tot2)
	{
   
		int ok=0;
		if(tot1>tot2)ok=1;
		else for(int i=tot1;i>=1;i--)
		{
   
			if(c[i]>answer[i])
			{
   
				ok=1;
				break;
			}
			if(c[i]<answer[i])
			{
   
				ok=-1;
				break;
			}
		}
		if(ok)
		{
   
			for(int i=1;i<=tot1;i++)
			 answer[i]=c[i];
			tot2=tot1;
		}
		int k=0,j=tot+5;
		for(int i=1;i<=j;i++)
		{
   
			k+=b[i]*a[o].l;
			b[i]=k%10;
			k/=10;
			if(b[i])tot=i;
		}
	}
}
int main()
{
   
	scanf("%d",&n);
	for(int i=0;i<=n;i++)
	 scanf("%d%d",&a[i].l,&a[i].r);
	sort(a+1,a+n+1,cmp);//排序
	while(a[0].l)//初值
	{
   
		b[++tot]=a[0].l%10;
		a[0].l/=10;
	}
	for(int i=1;i<=n;i++)//高精度
	 gjd(i); 
	for(int i=tot2;i>=1;i--)
	 printf("%d",answer[i]);
	return 0;
}

谢谢