优先级队列
来源:互联网 发布:东莞cnc编程学徒招聘 编辑:程序博客网 时间:2024/05/20 13:19
坑点:某些区间,out优先级队列会为空,这是不能出现out.top(),否则会出错
https://www.bnuoj.com/v3/problem_show.php?pid=35125
用优先级队列,实现每次都是in中的最小值和out中的最大值比较,而且交换后in马上重新排序,out:pop后不用push,因为push的数肯定不要再和in中的交换
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<queue>#define INF 0x3f3f3f3fusing namespace std;const int maxn=2e2+5;int a[maxn]; typedef long long ll;struct cmp1{bool operator()(int& a,int& b){return a>b;}};struct cmp2{bool operator()(int& a,int& b){return a<b;}};int Swap(int n,int k,int l,int r){priority_queue<int,vector<int>,cmp1> in;priority_queue<int,vector<int>,cmp2> out;int sum=0;for(int i=0;i<n;i++){if(i>=l&&i<=r){in.push(a[i]); sum+=a[i];}else{out.push(a[i]);}}while(k--){if(out.size()==0)break;if(out.top()>in.top()){sum=sum-in.top()+out.top();int big=out.top();int small=in.top();in.pop();out.pop();in.push(big);}else{break;}}return sum;}int main(){int n,k;scanf("%d%d",&n,&k);for(int i=0;i<n;i++){scanf("%d",&a[i]);}int max=-INF;int now;for(int i=0;i<n;i++){for(int p=i;p<n;p++){now=Swap(n,k,i,p);if(now>max){max=now;}}}printf("%d\n",max);return 0;}10 210 -1 2 2 2 2 2 2 -1 10
0 0
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- 优先级队列
- JavaSE-位操作
- 一些音视频通话方案架构
- Python自定义豆瓣电影种类,排行,点评的爬取与存储(高阶上)
- C语言学习笔记 lesson15&16 函数2指针的偏移
- 嵌入式系统学习(八)-通过sysfs(/sys/class/gpio)操作GPIO
- 优先级队列
- 去掉Sublime Text 3烦人的更新新版本提醒
- codeforces 361C
- Leetcode 120. Triangle
- android 打造Dialog式的Activity
- svn常用操作
- 课后作业银行ATM系统模拟参考答案
- js几种继承的fa
- PixHawk飞控和Mission Planner地面站安装调试