hihocoder1269 优化延迟(二分&优先队列)
来源:互联网 发布:监控计算机的软件 编辑:程序博客网 时间:2024/04/27 05:25
数组 方便快速访问
链表 适合元素移动
使用sqrt等函数时include<math.h>
使用sort等函数时#include<algorithm>
分配动态数组
int len;cin>>len;int *a = new int[len];
声明超大整型变量(例如a<10^13)
typedef long long ll;ll a;
二分法的写法也是很值得注意的!!!
优先队列!以前竟然都不知道啊这么好的东西!!!(以下为转载)
队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部。
优先队列(priority_queue)特别之处在于,允许用户为队列中存储的元素设置优先级。这种队列不是直接将新元素放置在队列尾部,而是放在比它优先级低的元素前面。标准库默认使用<操作符来确定对象之间的优先级关系,所以如果要使用自定义对象,需要重载 < 操作符。
优先队列有两种,一种是最大优先队列;一种是最小优先队列;每次取自队列的第一个元素分别是优先级最大和优先级最小的元素。
1)定义时要包含头文件”queue.h”, “functional.h”
2)三种定义的方式:
.使用具有默认优先级的已有数据结构。
.在定义优先队列的时候传入自定义的优先级比较对象。
.使用自定义对象(数据结构),但是必须重载好< 操作符。
3)常用操作
q.empty() //如果队列为空,则返回true,否则返回falseq.size() //返回队列中元素的个数q.pop() //删除队首元素,但不返回其值q.top() //返回具有最高优先级的元素值,但不删除该元素q.push(item) //在基于优先级的适当位置插入新元素
4)q.top()为查找操作,在最小优先队列中搜索优先权最小的元素,在最大优先队列中搜索优先权最大的元素。q.pop()为删除该元素。优先队列插入和删除元素的复杂度都是O(lgn),所以很快。
5)另外,在优先队列中,元素可以具有相同的优先权。
6)包含所有基本操作的优先队列C语言示例代码见最后
hihocoder1269
#include <iostream>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef long long ll;ll calculate_sp(int k, int* pn, int n){ ll sp = 0; priority_queue<int> buffer; int cou = 0; for(int i=0; i<n; i++){ buffer.push(pn[i]); while(buffer.size()>=k){ cou++; int temp = buffer.top(); buffer.pop(); sp += cou* temp; } } while(!buffer.empty()){ cou++; sp += cou*buffer.top(); buffer.pop(); } return sp;}int main(void){ int n; ll Q; cin>>n; cin>>Q; ll sp; int k; int *pn = new int[n]; for(int i=0; i<n; i++){ cin>>pn[i]; } sp = 0; int left=1; int right = n; while(left<=right){ k = (left+right)/2; sp = calculate_sp(k, pn, n); if(sp<=Q) right = k-1; else left = k+1; } if(left>n) left=-1; cout<<left; return 0;}
包含所有基本操作的优先队列C语言示例代码(转载)
#include<iostream>#include<functional>#include<queue>#include<vector>using namespace std;//定义比较结构struct cmp1{ bool operator ()(int &a,int &b){ return a>b;//最小值优先 }};struct cmp2{ bool operator ()(int &a,int &b){ return a<b;//最大值优先 }};//自定义数据结构struct number1{ int x; bool operator < (const number1 &a) const { return x>a.x;//最小值优先 }};struct number2{ int x; bool operator < (const number2 &a) const { return x<a.x;//最大值优先 }};int a[]={14,10,56,7,83,22,36,91,3,47,72,0};number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};int main(){ priority_queue<int>que;//采用默认优先级构造队列 priority_queue<int,vector<int>,cmp1>que1;//最小值优先 priority_queue<int,vector<int>,cmp2>que2;//最大值优先 priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误, priority_queue<int,vector<int>,less<int> >que4;////最大值优先 priority_queue<number1>que5; //最小优先级队列 priority_queue<number2>que6; //最大优先级队列 int i; for(i=0;a[i];i++){ que.push(a[i]); que1.push(a[i]); que2.push(a[i]); que3.push(a[i]); que4.push(a[i]); } for(i=0;num1[i].x;i++) que5.push(num1[i]); for(i=0;num2[i].x;i++) que6.push(num2[i]); printf("采用默认优先关系:/n(priority_queue<int>que;)/n"); printf("Queue 0:/n"); while(!que.empty()){ printf("%3d",que.top()); que.pop(); } puts(""); puts(""); printf("采用结构体自定义优先级方式一:/n(priority_queue<int,vector<int>,cmp>que;)/n"); printf("Queue 1:/n"); while(!que1.empty()){ printf("%3d",que1.top()); que1.pop(); } puts(""); printf("Queue 2:/n"); while(!que2.empty()){ printf("%3d",que2.top()); que2.pop(); } puts(""); puts(""); printf("采用头文件/"functional/"内定义优先级:/n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n"); printf("Queue 3:/n"); while(!que3.empty()){ printf("%3d",que3.top()); que3.pop(); } puts(""); printf("Queue 4:/n"); while(!que4.empty()){ printf("%3d",que4.top()); que4.pop(); } puts(""); puts(""); printf("采用结构体自定义优先级方式二:/n(priority_queue<number>que)/n"); printf("Queue 5:/n"); while(!que5.empty()){ printf("%3d",que5.top()); que5.pop(); } puts(""); printf("Queue 6:/n"); while(!que6.empty()){ printf("%3d",que6.top()); que6.pop(); } puts(""); return 0;}/*运行结果 :采用默认优先关系:(priority_queue<int>que;)Queue 0:91 83 72 56 47 36 22 14 10 7 3采用结构体自定义优先级方式一:(priority_queue<int,vector<int>,cmp>que;)Queue 1:3 7 10 14 22 36 47 56 72 83 91Queue 2:91 83 72 56 47 36 22 14 10 7 3采用头文件"functional"内定义优先级:(priority_queue<int,vector<int>,greater<int>/less<int> >que;)Queue 3:3 7 10 14 22 36 47 56 72 83 91Queue 4:91 83 72 56 47 36 22 14 10 7 3采用结构体自定义优先级方式二:(priority_queue<number>que)Queue 5:3 7 10 14 22 36 47 56 72 83 91Queue 6:91 83 72 56 47 36 22 14 10 7 3*/
- hihocoder1269 优化延迟(二分&优先队列)
- hihocoder1269优化延迟
- HihoCoder 1269 优化延迟(二分、优先队列)
- hiho 1269 优化延迟 二分 优先队列
- hihoCoder #1269 : 优化延迟 【二分 + 优先队列】
- hihoCoder--1469 优化延迟(二分+优先队列)
- hihocoder 136 #1269 优化延迟 二分+优先队列
- hiho一下 第136周 #1269 : 优化延迟 【二分+堆排序|优先队列】
- uva 1422 - Processor(二分+优先队列)
- UVA-1422(贪心+二分+优先队列)
- Sort 二分 + (队列 + sort 代替优先队列)
- Huffman树(使用优先队列优化)
- 堆(优先队列)优化dijkstra(邻接矩阵)
- dijkstra算法模板(优先队列优化)
- 贪心+优先队列(需优化) POJ2010
- poj 2387(Dijkstra优先队列优化)
- poj1729(bfs+优先队列优化)
- dijstra+优先队列优化
- virtualbox创建com对象失败(解决方法)
- Oracle数据类型之number
- Bean named '*' must be of type [*], but was actually of type [*] with root case
- 2.bean的命名和实例化、依赖注入方式
- Android Application Architecture
- hihocoder1269 优化延迟(二分&优先队列)
- 在windows下codeblocks中配置pthread的库
- UIScrollView频道滑动(授人以鱼,不如授人以渔)
- webview加载html代码,其中代码中有图片
- 第一篇文章--学习小感欢迎交流
- iOS开发 ☞ UIImage部分拉伸——stretchableImageWithLeftCapWidth的使用
- 利用sqoop导出hive数据到 oracle
- Integer的大小比较
- opencv IplImage转CvMat