题目链接:点击查看
题目大意:恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这 n 位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。
国王不希望某一个大臣获得特别多的奖赏,所以他想请你帮他重新安排一下队伍的顺序,使得获得奖赏最多的大臣,所获奖赏尽可能的少。注意,国王的位置始终在队伍的最前面。
题目分析:
先说结论,按照每个大臣的a*b大小升序排序就能得到最优解
证明如下:
首先我们交换第i个位置和第i+1个位置的两个大臣,在此之前他们两人的奖励分别是:
和
交换之后变为
和
为了方便观察,我们约分约去
则我们只需要比较下面两个式子的大小关系:
和
两边同时乘上得:
和
因为A[i]和B[i]都为正整数,所以且
于是当时,左式<=右式,交换前更优,否则交换后更优,所以在任何局面下,减小逆序对数都不会造成整体结果变差,而增加逆序对数则不会使整体结果变好,证毕
代码:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int N=40100;//最大长度
const string ss="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int mp[150];
void postpoint(string& a)//移位处理
{
int len=a.size();
a.resize(N);
for(int i=0;i<len;i++)
{
a[N-i-1]=a[len-i-1];
a[len-i-1]=0;
}
}
string change(const string& a)//用于解除postpoint
{
string ans;
for(int i=0;i<N;i++)
if(a[i])
ans+=a[i];
return ans;
}
string plu(string a,string b,int base=10)
{
postpoint(a);
postpoint(b);
string temp;
temp.resize(N);
int f=0;
for(int i=N-1;a[i]||b[i]||f;i--)
{
a[i]==0?a[i]='0':0;
b[i]==0?b[i]='0':0;
temp[i]=(mp[a[i]]+mp[b[i]]+f)%base;
f=(mp[a[i]]+mp[b[i]]+f)/base;
temp[i]=ss[temp[i]];
}
return change(temp);
}
string mul(string a,int n,int base=10)
{
postpoint(a);
string temp;
temp.resize(N);
int f=0;
for(int i=N-1;a[i]||f;i--)
{
a[i]==0?a[i]='0':0;
temp[i]=(mp[a[i]]*n+f)%base;
f=(mp[a[i]]*n+f)/base;
temp[i]=ss[temp[i]];
}
return change(temp);
}
string Except(string s,long long x) //大数除以整形数
{
long long cmp=0;
bool ok=false;
string ans="";
for(int i=0;i<s.size();i++)
{
cmp=(cmp*10+s[i]-'0');
if(cmp>=x)
{
ok=true;
ans+=(cmp/x+'0');
cmp%=x;
}
else{
if(ok)
ans+='0'; //注意这里
}
}
return ans;
}
void init()
{
for(int i=0;i<ss.size();i++)
mp[ss[i]]=i;
}
struct Node
{
int a,b;
bool operator<(const Node& t)const
{
return a*b<t.a*t.b;
}
}a[10100];
int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);
init();
int n;
scanf("%d",&n);
for(int i=0;i<=n;i++)
scanf("%d%d",&a[i].a,&a[i].b);
sort(a+1,a+1+n);
string ans=to_string(a[0].a);
string mmax;
for(int i=1;i<=n;i++)
{
string temp=Except(ans,a[i].b);
if(temp.size()>mmax.size()||temp>mmax)
mmax=temp;
ans=mul(ans,a[i].a);
}
cout<<mmax<<endl;
return 0;
}