国王的游戏(贪心,排序)
思路:
对于A,B两个大臣,他们前面的人的左手上的整数乘积为S,
对于 A:LA,RA; B:LB,RB.
有两种顺序,AB或者BA
按照上面的结论,我们把左右手乘积更大的排在后面就行
但是这是一道高精度,采用高精度乘高精,高精除以低精度即可。
好像高精度除以高精度会超时
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e4+10;
int A[maxn];
int ej = 1;
int ans[maxn] = {0};
struct P
{
int a;
int b;
}p[maxn];
struct Rule
{
bool operator()(const P &p1,const P &p2)
{
return p1.a*p1.b < p2.a*p2.b;
}
};
void chengfa(int x)
{
// printf("x=%d\n",x);
int B[maxn];
int C[maxn];
memset(B,0,sizeof(B));
memset(C,0,sizeof(C));
while(x)
{
B[++B[0]] = (x%10);
x/=10;
}
for(int i=1;i<=B[0];i++)
{
int tmp = 0;
for(int j=1;j<=ej;j++)
{
int t = i+j-1;
C[t] += A[j]*B[i] + tmp;//这次乘积 + 上次乘积 + 进位
tmp = C[t]/10;//取进位
C[t]%=10;
}
C[i+ej] = tmp;
}
ej = B[0]+ej+1;
while(C[ej]==0) ej--;
for(int i=ej;i>0;i--)
{
A[i] = C[i];
//cout<<A[i];
}
//cout<<endl;
A[0] = ej;
}
int cmp(int A2[maxn],int B[maxn])
{
if(A2[0]>B[0]) return 1;
else if(A2[0]<B[0]) return -1;
else
{
for(int i=A2[0];i>=1;i--)
{
if(A2[i]>B[i]) return 1;
else if(A2[i]<B[i]) return -1;
}
return 0;
}
}
void chufa(int x)
{
int ans2[maxn];
memset(ans2,0,sizeof(ans2));
int b = 0;
for(int i=A[0];i>0;i--)//每一位计算
{
ans2[i]=( A[i] + b*10)/x;
b = (A[i] + b*10)%x;
}
ans2[0] = A[0];
while(ans2[ans2[0]]==0&&ans2[0]>0) ans2[0]--;
if(cmp(ans2,ans)>0)
{
for(int i=ans2[0];i>0;i--)
{
ans[i] = ans2[i];
// cout<<ans[i];
}
ans[0] = ans2[0];
// cout<<endl;
}
}
int main()
{
std::ios::sync_with_stdio(false);
int n;
int ka;
int kb;
cin>>n;
cin>>ka>>kb;
// FILE *fp;
// if((fp=fopen("D:\\tmp\\mytest.txt","r"))==NULL)
// if((fp=fopen("D:\\tmp\\in.txt","r"))==NULL)
// {
//printf("打开失败\n");
// }
for(int i=0;i<n;i++)
{
cin>>p[i].a>>p[i].b;
//fscanf(fp,"%d %d\n",&p[i].a,&p[i].b);
}
//fclose(fp);
sort(p,p+n,Rule());
/* for(int i=0;i<n;i++)
{
cout<<p[i].a<<" "<<p[i].b<<endl;
}*/
while(ka)
{
A[ej++]=(ka%10);
ka/=10;
}
ej--;
A[0] = ej;
for(int i=0;i<n;i++)
{
chufa(p[i].b);
chengfa(p[i].a);
}
for(int i=ans[0];i>0;i--)
{
cout<<ans[i];
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话