Codeforces Gym101234G Dreamoon and NightMarket(优先队列,子集和第k大)
2024-09-06 18:46:29
题意:
求子集和第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 ;
}
最新文章
- eclipse maven maven-archetype-webapp 创建失败
- Linux 系统中僵尸进程
- SharePoint Server 2013开发之旅(一):新的开发平台和典型开发场景介绍
- sublime Text3使用笔记
- 因用了NeatUpload大文件上传控件而导致Nonfile portion >; 4194304 bytes错误的解决方法
- 第2章 Posix IPC
- (转载)delphi 常用函数(数学)
- [RxJS] Error Handling in RxJS
- meta标签的少许语法,慢慢收集中...
- iOS英语—》中国本土化,如调用专辑,摄像头的变化“cancel”,“photos”至“撤消”,“摄像头”
- IDEA内存异常问题
- Python实现Mysql数据库连接池
- CentOS7.4使用KVM
- 使用.net core efcore根据数据库结构自动生成实体类
- SpringMvc的基本流程
- 第四十一课 KMP子串查找算法
- Leaflet.draw 无法编辑multipolygon类型多边形 解决方法
- .NetCore使用Swagger进行单版本或多版本控制处理
- I/O模型(同步、非同步、阻塞、非阻塞)总结
- 允许root远程登录Solaris
热门文章
- Go指针,如此轻松掌握,希望有收获
- OpenStack Identity API v3 extensions (CURRENT)
- (2)MongoDB副本集自动故障转移原理
- DP-直线分割递推
- <;a>;标签的href和onclick属性【转】
- [bzoj4569] [loj#2014] [Scoi2016] 萌萌哒
- sense8影评摘抄
- python 获取网页图片 十月底的 一弹
- 生成URL(而不是链接) Generating URLs (and Not Links) | 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
- GCC编译Win图形程序不显示控制台方法