Codeforces Gym101234G Dreamoon and NightMarket(优先队列,子集和第k大)

Posted wrjlinkkkkkk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Gym101234G Dreamoon and NightMarket(优先队列,子集和第k大)相关的知识,希望对你有一定的参考价值。

题意:

求子集和第k大,n,k<=1e6

思路:

优先队列经典题目,注意优先队列是默认按从大到小排的

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
    
#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) 

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;


const db eps = 1e-6;
const int mod = 1e9+7;
const int maxn = 4e5+100;
const int maxm = 4e5+100;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);

int n, k;
ll a[maxn];
priority_queue<pair<ll, int> ,vector<pair<ll,int>>, greater<pair<ll,int>> >q;

//priority_queue<pair<ll,int>, vector<pair<ll,int> >, greater<pair<ll,int> > >q;
int main(){
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; i++){
        scanf("%lld", &a[i]);
    }
    sort(a+1,a+1+n);
    q.push({a[1],1});
    int cnt = 0;
    
    while(cnt < k){
        auto tmp = q.top();
        q.pop();
        ll ans = tmp.fst;
        int id = tmp.sc;
        if(id < n){
            q.push({ans+a[id+1],id+1});
            q.push({ans-a[id]+a[id+1], id+1});
        }
        cnt++;
        if(cnt == k){
            printf("%lld
", ans);
            break;
        }
    }
    return 0;
}

 

以上是关于Codeforces Gym101234G Dreamoon and NightMarket(优先队列,子集和第k大)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces Gym 101063 C

codeforces Gym 100735 DEGHI

CodeForces Gym-101350M

Codeforces-gym-101020 problem C. Rectangles

2021-5-11 CSUST 周练 Codeforces Gym #102894

2021-5-11 CSUST 周练 Codeforces Gym #102894