114.国王游戏

114.国王游戏
考察:贪心+高精度(乘、除)
贪心证明:https://www.acwing.com/solution/content/1062/

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
typedef pair<int,int> PII;
const int N = 1010;
PII p[N];

vector<int> mul(vector<int> &a, int b){
    vector<int> c;
    int t = 0;
    for(int i = 0; i < a.size(); i++){
        t += a[i]*b;
        c.push_back(t%10);
        t /= 10;
    }
    while(t){
        c.push_back(t%10);
        t /= 10;
    }
    return c;
}

vector<int> div(vector<int> &a, int b){
    vector<int> c;
    bool is_first = true;
    for(int i = a.size()-1, t = 0; i >= 0; i--){
        t = t*10 + a[i];
        int x = t / b;
        if(!is_first || x){
            is_first = false;
            c.push_back(x);
        }
        t %= b;
    }
    reverse(c.begin(),c.end());
    return c;
}

vector<int> max_vec(vector<int> a, vector<int> b){
    if(a.size() > b.size()) return a;
    if(b.size() > a.size()) return b;
    if(vector<int>(a.rbegin(),a.rend()) > vector<int>(b.rbegin(),b.rend())) return a;
    return b;
}

int main(){
    cin.tie(0)->sync_with_stdio(false),cout.tie(0);
    int n; cin>>n;
    for(int i = 0; i <= n; i++){
        int a,b; cin>>a>>b;
        p[i] = {a*b,a};
    }
    
    sort(p+1,p+n+1);

    vector<int> product(1,1);
    vector<int> res(1,0);
    for(int i = 0; i <= n; i++){
        if(i) res = max_vec(res,div(product,p[i].first/p[i].second));
        product = mul(product, p[i].second);        
    }
    for(int i = res.size()-1; i >= 0; i--) cout<<res[i];
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值