[Greed] [洛谷] P1080 国王游戏
都TM 8102年了
还在写大数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
string prefix[MAXN];
int aa[10010] = {0}, bb[10010] = {0}, ans[2 * 10010] = {0};
string multi(string a, string b)
{
memset(aa, 0, sizeof(aa));
memset(bb, 0, sizeof(bb));
memset(ans, 0, sizeof(ans));
if(b.length() > a.length())
{
string tmp = a;
a = b;
b = tmp;
}
int lena = a.length(), lenb = b.length();
for(int i = lena - 1, flag = 0; i >= 0 ; i--, flag++)
{
aa[flag] = a[i] - '0';
}
for(int i = lenb - 1, flag = 0; i >= 0 ; i--, flag++)
{
bb[flag] = b[i] - '0';
}
for(int i = 0; i < lenb ; i++ )
{
for(int j = 0 ; j < lena; j++)
{
ans[i + j] += aa[j] * bb[i];
}
}
int flag = 2 * 10010 ;
for(int i = 0 ; i < flag; i++) //进位
{
ans[i + 1] += ans[i] / 10;
ans[i] = ans[i] % 10;
}
flag = 2 * 10010 ;
for(; ans[flag] == 0;flag --);
string sans;
for(;flag >= 0;flag --)
sans = sans + char(ans[flag] + '0');
return sans;
}
string Except(string s,int x)
{
ll cmp = 0, ok = 0;
string ans = "";
for(int i = 0; i < s.length(); i++)
{
cmp = (cmp * 10 + s[i] - '0');
if(cmp >= x)
{
ok = 1;
ans += (cmp / x + '0');
cmp %= x;
}
else if(ok == 1)
ans += '0';
}
return ans.empty() ? "0" : ans;
}
struct hand
{
int left, right;
}arr[MAXN];
bool cmp(hand a, hand b)
{
return a.left * a.right < b.left * b.right;
}
string rans;
string trans(ll x)
{
string ret = "";
while(x)
{
ret = char(x % 10 + '0') + ret;
x /= 10;
}
return ret;
}
string judge(string a, string b)
{
if(a.length() == b.length())
{
if(a > b)
return a;
else
return b;
}
if(a.length() > b.length())
{
return a;
}
return b;
}
int main()
{
int n;
cin>>n;
for(int i = 0; i <= n; i++)
{
cin>>arr[i].left>>arr[i].right;
}
sort(arr + 1, arr + n + 1, cmp);
prefix[0] = trans(arr[0].left);
for(int i = 1; i <= n; i++)
{
prefix[i] = multi(trans(arr[i].left), prefix[i - 1]);
}
for(int i = 1; i <= n; i++)
{
rans = judge(rans, Except(prefix[i - 1],arr[i].right) );
}
cout<<rans<<endl;
return 0;
}