设F(i, j) 为以(0, 0)为左下角,以(i, j)为右上角的矩形面积,对于1 <= i <= n, 1 <= j <= m, 求F(i, j)的第k大的值

题目

#include
using namespace std;
#define int long long
const int maxn = 1e6 + 5, inf = 1e9;
int a[maxn];
struct Node{
    int a, b;
    bool operator < (const Node x)const{
        return a * b < x.a * x.b;
    }
};
map, bool> mp;
void solve(){
    int n;
    cin >> n;
    int m, k;
    cin >> m >> k;
    priority_queue pq;
    pq.push({n, m});
    mp[{n, m}] = 1;
    for(int i = 1; i <= k; i++){
        auto u = pq.top();
        if(i == k){
            cout << u.a * u.b << '\n';
            break;
        }
        pq.pop();
        //记得用map判重
        if(u.a >= 2 && !mp[{u.a - 1, u.b}]) pq.push({u.a - 1, u.b}), mp[{u.a - 1, u.b}] = 1;
        if(u.b >= 2 && !mp[{u.a, u.b - 1}]) pq.push({u.a, u.b - 1}), mp[{u.a, u.b - 1}] = 1;
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
//     cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

你可能感兴趣的:(XCPC补题,算法)