Gym - 101234G Dreamoon and NightMarket

题目链接点这里

这题感觉和图上k短路很类似,,用优先队列一直弹弹弹直到弹满k个就好了


/*%%%%%%%%%%%%%lcy%%%%%%%%%%%%%%%%%%%%*/
#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<string>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin);
#define mem(x,y) memset(x,y,sizeof(x));
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<x<<endl;
const int  MX=2e5+5;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<LL,int> PII;
const double eps=1e-9;
int w[MX],n,k;
int main()
{
    FIN;
    while(cin>>n>>k)
    {
        for(int i=0; i<n; i++)scanf("%d",&w[i]);
        sort(w,w+n);
        priority_queue<PII,vector<PII>,greater<PII> >Q;
        LL ans;
        int cnt=0;
        Q.push(PII(w[0],0));
        while(!Q.empty())
        {
            PII u=Q.top();
            Q.pop();
            //cout<<u.first<<endl;
            cnt++;
            if(cnt==k)
            {
                ans=u.first;
                break;
            }
            if(u.second+1<n)
            {
                Q.push(PII(u.first+w[u.second+1],u.second+1));
                Q.push(PII(u.first+w[u.second+1]-w[u.second],u.second+1));
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值