[Ybtoj high-efficiency advanced 1.2] [greedy] [Luogu P1080] King's Game

[Ybtoj high-efficiency advanced 1.2] [greedy] [Luogu P1080] King's Game

topic

Insert picture description here
Insert picture description here


Problem-solving ideas

n==2
Insert picture description here

Insert picture description here
ans1=max (l0 / r1,l0 * l1 / r2)
ans2=max (l0 / r2,l0 * l2 / r1)
can be compared to get
l0 / r1 <l0 * l2 / r1
l0 / r2 <l0 * l1 / r2
Assuming that ans1<ans2,
then l0 * l1 / r2 <l0 * l2 / r1
uses the eighth grade fractional equation solution method to obtain
l1 * r1 <l2 * r2
. After fast sorting, the answer
should be high-precision.


Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct lzf{
    
    
	int l,r;
}a[1020];
int sum[10000],d[10000],ans[10000];
int n,c,t,l;
bool cmp(lzf l,lzf y)  //快排
{
    
    
	 return l.l*l.r<y.l*y.r;
}
void xc(int x)  //高精乘
{
    
    
	 int w=0;
	 for (int i=1;i<=l;i++)
	 {
    
    
	 	 sum[i]=sum[i]*x+w;
	 	 w=sum[i]/10;
	 	 sum[i]=sum[i]%10;
	 } 
	 while (w)
	 {
    
    
	 	   sum[++l]=w%10;
	 	   w=w/10;
	 } 
}
void div(int x)  //高精除
{
    
    
	 int w=0;
	 c=1;
	 memset(d,0,sizeof(d));
	 for (int i=l;i>0;i--)
	 {
    
    
	 	 w=w*10+sum[i];
	 	 d[i]=w/x;
	 	 if (d[i]!=0&&c==1) c=i;
	 	 w=w%x;
	 } 
}
bool pd()  //当前大臣拿到的金币是否比答案大
{
    
    
	 if (t>c) return 0;
	 if (c>t) 
	 {
    
    
	    t=c;
		return 1;
	 }
	 for (int i=1;i<=t;i++)
	     if (ans[i]<d[i])
	        return 1;
	 return 0;
}
void jh()  //更新答案
{
    
    
	 for (int i=1;i<=t;i++)
	     ans[i]=d[i];
}
int main()
{
    
    
	scanf("%d",&n);
	l=1;
	sum[1]=1;
	for (int i=0;i<=n;i++)
	    scanf("%d%d",&a[i].l,&a[i].r);
	sort(a+1,a+n+1,cmp);
	for (int i=1;i<=n;i++)
	{
    
     
		xc(a[i-1].l); 
		div(a[i].r);
		if (pd())
		   jh();
	}
	for (int i=t;i>0;i--)
	    printf("%d",ans[i]);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/111722652
Recommended