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) 应该可以暴力 顺便学习一下优先队列 枚举区 ...
随机推荐
- Python 处理Excel内的数据(案例介绍*2)
(一)案例一介绍 现在有一匹电商产品跟当日销量的数据,如下,总共有上万笔的数据,现在需要统计每个品牌当日的销售量,比如美宝莲今天总共卖出了多少的商品,另外需要统计每个品牌下面的每个子品类当日销售量(品 ...
- DispatcherServlet的url-pattern尽量不要配置为"/*"
DispatcherServlet的url-pattern尽量不要配置为"/*" 原因 当Dispatcher的url配置为"/*"时,会把tomcat的web ...
- 【JavaScript学习笔记】数据类型
1.获取数据类型 typeof 用法: typeof variable or typeof(variable) JavaScript中总共有6种数据类型,string.number.boolean ...
- python检查是否是闰年
检查的依据: 闰年可以被4整除不能被100整除,或者可以被400整除. year = int(input("请输入年份:")) if year % 4 == 0 and year ...
- CTF实验吧——证明自己吧
题目地址:http://www.shiyanbar.com/ctf/28 没有壳 ,vc++ 写的 拖进OD观察观察,发现代码很短哟,先来看这俩个call 怀疑他们其中有正确的flag和我们输入的东西 ...
- 「 优质资源20190409 」Java最新精选优质资源!
资源导读 经过小编精心整理,java最新优质资源出炉 不想看书,可以看视频,比较生动有趣,好的视频教程是一个好老师! 资源来自于网络,请勿用于商业用途 资源目录 1.Java Spring 技术栈构建 ...
- 玩转Django2.0---Django笔记建站基础三(编写URL规则)
第三章 编写URL规则 URL(Uniform Resource Locator,统一资源定位符)是对可以从互联网上得到的资源位置和访问方法简洁的表示,是互联网上标准资源的地址. 在App里由于Dja ...
- SpringCloud学习之搭建eureka集群,手把手教学,新手教程
一.为什么需要集群 上一篇文章讲解了如何搭建单个节点的eureka,这篇讲解如何搭建eureka集群,这里的集群还是本地不同的端口执行三个eureka,因为条件不要允许,没有三台电脑,所以大家将就一下 ...
- Celery异步处理
1.Celery概述 1.1问题抛出 我们在做网站后端程序开发时,会碰到这样的需求:用户需要在我们的网站填写注册信息,我们发给用户一封注册激活邮件到用户邮箱,如果由于各种原因,这封邮件发送所需时间较长 ...
- Python 类中方法的内部变量,命名加'self.'变成 self.xxx 和不加直接 xxx 的区别
先看两个类的方法: >>> class nc(): def __init__(self): self.name ='tester' #name变量加self >>> ...