1. 程式人生 > 其它 >P1080 [NOIP2012 提高組] 國王遊戲

P1080 [NOIP2012 提高組] 國王遊戲

// Problem: P1080 [NOIP2012 提高組] 國王遊戲
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1080
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

template<class T>
void debugVector(const T &a) {
    cout << "[ ";
    for (size_t i = 0; i < a.size(); ++i) {
        cout << a[i] << (i == a.size() - 1 ? " " : ", ");
    }
    cout << "]" << endl;
}

template<class T1, class T2>
void debugVectorPair(const vector<pair<T1, T2>> &a) {
    cout << "{";
    for (size_t i = 0; i < a.size(); ++i) {
        cout << "[ " << a[i].first << ": " << a[i].second << (i == a.size() - 1 ? " ]" : " ], ");
    }
    cout << "}" << endl;
}


template<class T>
void debugMatrix2(const T &a) {
    for (size_t i = 0; i < a.size(); ++i) {
        debugVector(a[i]);
    }
}

template<class T>
using matrix2 = vector<vector<T>>;

template<class T>
vector<vector<T>> getMatrix2(size_t n, size_t m, T init = T()) {
    return vector<vector<T>>(n, vector<T>(m, init));
}

template<class T>
using matrix3 = vector<vector<vector<T>>>;

template<class T>
vector<vector<vector<T>>> getMatrix3(size_t x, size_t y, size_t z, T init = T()) {
    return vector<vector<vector<T>>>(x, vector<vector<T>>(y, vector<T>(z, init)));
}

vector<int> genBigInteger(const string &a) {
    vector<int> res;
    if (a.empty()) return res;
    for (int i = a.size() - 1; i >= 0; --i) {
        res.push_back(a[i] - '0');
    }
    return res;
}

ostream &printBigInteger(const vector<int> &a) {
    if (a.empty()) return cout;
    for (int i = a.size() - 1; i >= 0; --i) {
        cout << a[i];
    }
    return cout;
}

vector<int> maxBigInteger(const vector<int> &a, const vector<int> &b) {
    if (a.size() > b.size()) {
        return a;
    } else if (a.size() < b.size()) {
        return b;
    }
    for (int i = a.size() - 1; i >= 0; --i) {
        if (a[i] > b[i]) {
            return a;
        } else if (a[i] < b[i]) {
            return b;
        }
    }
    return a;
}

vector<int> addBigInteger(const vector<int> &a, const vector<int> &b) {
    vector<int> res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || i < b.size() || pre; ++i) {
        if (i < a.size()) pre += a[i];
        if (i < b.size()) pre += b[i];
        res.push_back(pre % 10);
        pre /= 10;
    }
    while (res.size() > 1 && res.back() == 0) {
        res.pop_back();
    }
    return res;
}

vector<int> mulBigInteger(const vector<int> &a, int b) {
    vector<int> res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || pre; ++i) {
        if (i < a.size()) pre += a[i] * b;
        res.push_back(pre % 10);
        pre /= 10;
    }
    while (res.size() > 1 && res.back() == 0) {
        res.pop_back();
    }
    return res;
}

vector<int> divBigInteger(const vector<int> &a, int b, int &r) {
    vector<int> res;
    for (int i = a.size() - 1; i >= 0; --i) {
        r = 10 * r + a[i];
        res.push_back(r / b);
        r %= b;
    }
    reverse(res.begin(), res.end());
    while (res.size() > 1 && res.back() == 0) {
        res.pop_back();
    }
    return res;
}

bool compare(const pair<int, int> p1, const pair<int, int> p2) {
    return p1.first * p1.second < p2.first * p2.second;
}

/*
    假設已經排了幾個人,他們左手上的數的乘積為S。
    對於之後的兩個人,哪個放在前面?
    第一個人左手數目a1,右手b1;第二個人左手a2,右手b2
    如果第一個人排在前面優於第二個人,則
    max(S / b1, S * a1 / b2) < max(S / b2, S * a2 / b2);
    
    由於 S * a1 / b2 >= S / b2
    假設 S * a1 / b2 >= S * a2 / b2,則顯然max() > max(),矛盾
    所以 S * a1 / b2 < S * a2 / b1
     => a1 * b1 < a2 * b2
*/
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    int a, b;
    cin >> a >> b;
    vector<pair<int, int>> info(n);
    for (int i = 0; i < n; ++i) {
        cin >> info[i].first >> info[i].second;
    }
    
    sort(info.begin(), info.end(), compare);
    vector<int> res;
    vector<int> mul(1, 1);
    mul = mulBigInteger(mul, a);
    for (int i = 0; i < info.size(); ++i) {
        int r = 0;
        vector<int> tmp = divBigInteger(mul, info[i].second, r);
        res = maxBigInteger(res, tmp);
        mul = mulBigInteger(mul, info[i].first);
    }
    printBigInteger(res) << endl;
    return 0;
}