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-;
const int mod = 1e9+;
const int maxn = 4e5+;
const int maxm = 4e5+;
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 = ; i <= n; i++){
scanf("%lld", &a[i]);
}
sort(a+,a++n);
q.push({a[],});
int cnt = ; 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+],id+});
q.push({ans-a[id]+a[id+], id+});
}
cnt++;
if(cnt == k){
printf("%lld\n", ans);
break;
}
}
return ;
}
Codeforces Gym101234G Dreamoon and NightMarket(优先队列,子集和第k大)的更多相关文章
- Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验
Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shal ...
- Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解
Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...
- Codeforces I. Producing Snow(优先队列)
题目描述: C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input stand ...
- HDU 4352 区间的有多少个数字满足数字的每一位上的数组成的最长递增子序列为K(数位DP+LIS)
题目:区间的有多少个数字满足数字的每一位上的数组成的最长递增子序列为K 思路:用dp[i][state][j]表示到第i位状态为state,最长上升序列的长度为k的方案数.那么只要模拟nlogn写法的 ...
- G - Dreamoon and NightMarket Gym - 101234G 优先队列+思路
题目:题目链接 题意:给出n种食物,食物有自己的价格并且可以自由搭配,每天吃之前没吃过的花费最少的搭配,问第k天的花费. 思路:第k小我们考虑用优先队列处理,虽然n比较大,但由于1 ≤ K ≤ min ...
- codeforces 725D . Contest Balloons(贪心+优先队列)
题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- codeforces 476B.Dreamoon and WiFi 解题报告
题目链接:http://codeforces.com/problemset/problem/476/B 题目意思:给出两个字符串str1, str2,其中,str1 只由 '+' 和 '-' 组成,而 ...
- [ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列
题意: 给你一个有n个数的序列 取一个区间 这个区间内的数可以与区间外的值交换k次 问这样的区间最大值是多少 思路: 看数据是200 时间复杂度O(n*n) 应该可以暴力 顺便学习一下优先队列 枚举区 ...
随机推荐
- 解决阿里云ECS下kubeadm部署k8s无法指定公网IP
背景 一般情况下,"kubeadm"部署集群时指定"--apiserver-advertise-address=<public_ip>"参数,即可在 ...
- 基于GPS北斗卫星授时系统和NTP网络授时服务器的设计与开发
基于GPS北斗卫星授时系统和NTP网络授时服务器的设计与开发 安徽京准科技提供@请勿转载@@ 更多资料请参考——ahjzsz.com 天文观测设备对于控制系统的时间准确度有严格要求.为此,采用搭建高精 ...
- K8S中的ingress-nginx配置
编辑在有道云笔记中,链接如下: http://note.youdao.com/noteshare?id=8d9ae3a1643e39714b36b58f7507baa2&sub=61C8C20 ...
- 12.pyecharts详细使用教程
官方数据教程: 柱状图-Bar //导入柱状图-Bar from pyecharts import Bar //设置行名 columns = ["Jan", "Feb&q ...
- 如何制作地图故事使用esri story maps
博客作者原创 制作方法如下:http://url.cn/5dnsVQd
- 【转】JavaScript 3D图表
文章系本人原创,转载请保持完整性并注明出自<四火的唠叨> 在说3D图表以前,首先要明确两个概念,一个是数据的维度,一个是呈现数据载体的维度.对于数据的维度,一维的数据呈现,但是呈现的载体是 ...
- 使用Jenkins进行前端UVE项目部署
操作步骤 1.用 Jenkins 管理员账号下载 NodeJS Plugin 2.系统管理 ---> 全局工具配置 ---> NodeJS ---> 安装 ---> 自动安装 ...
- pinpoint实现告警推送至钉钉和微信群
前言 在前面的文章中,我们学习了如何通过java实现将消息发送到钉钉.和将消息发送到微信群聊. 基于上述基础,我们今天来接入pinpoint的告警,发送到钉钉群. 实操前准备 开始之前,推荐阅读一下, ...
- npm 安装出现 run `npm audit fix` to fix them, or `npm audit` for details 解决办法
1.npm audit fix 2. npm audit fix --force 3.npm audit 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链 ...
- C++ 链式继承下的虚函数列表
目录 1.虚函数列表的位置 2.虚函数列表的内容 3.链式继承中虚函数列表的内容 注: 虚函数列表 又称为虚表, vtbl , 指向它的指针称为vptr, vs2019中称为__vfptr 操作系 ...