POJ 1442 Black Box -优先队列
来源:互联网 发布:软件开发方法学 编辑:程序博客网 时间:2024/04/29 03:39
优先队列。。刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊。
超时代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <cstdlib>#include <string>#include <vector>#include <map>#include <queue>#include <functional>using namespace std;#define N 30003priority_queue<int,vector<int>,greater<int> > que;int A[N];vector<int> tmp;int main(){ int m,n,i,j,th,u,k,h; while(scanf("%d%d",&m,&n)!=EOF) { th = 0; for(i=0;i<m;i++) scanf("%d",&A[i]); j = 0; k = 0; for(i=0;i<n;i++) { scanf("%d",&u); while(j<u) { que.push(A[k++]); j++; } th++; for(h=1;h<th;h++) { tmp.push_back(que.top()); que.pop(); } printf("%d\n",que.top()); for(h=0;h<tmp.size();h++) que.push(tmp[h]); tmp.clear(); } } return 0;}
后来看了网上某位大牛的,说是用两个优先队列,一个从大到小的,一个从小到大的,元素在这两个中间中转,可以大大缩短时间。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <vector>#include <map>#include <queue>#include <functional>using namespace std;#define N 30003priority_queue<int,vector<int>,greater<int> > StoBig; //从小到大排,先出小的priority_queue<int,vector<int>,less<int> > BtoSmall; //从大到小排,先出大的int A[N];int main(){ int m,n,i,j,th,u,k,h; while(scanf("%d%d",&m,&n)!=EOF) { th = 0; for(i=0;i<m;i++) scanf("%d",&A[i]); j = 0; for(i=0;i<n;i++) { scanf("%d",&u); while(j<u) BtoSmall.push(A[j++]); th++; while(BtoSmall.size() >= th) //语句1 { StoBig.push(BtoSmall.top()); BtoSmall.pop(); } printf("%d\n",StoBig.top()); BtoSmall.push(StoBig.top()); //还给它,保证语句1能够成立 StoBig.pop(); } } return 0;}
0 0
- POJ 1442 Black Box (优先队列)
- POJ 1442 Black Box -优先队列
- POJ 1442 Black Box(优先队列)
- POJ 1442-Black Box(优先队列)
- POJ 1442 Black Box (优先队列)
- POJ 1442 Black Box(优先队列)
- POJ-1442-Black Box-优先队列
- POJ Black Box 1442(优先队列)
- Black Box poj 1442 优先队列
- POJ 1442Black Box(优先队列)
- [ACM] POJ 1442 Black Box (堆,优先队列)
- Poj 1442 Black Box(优先队列水题)
- POJ 1442 Black Box treap || 树状数组 || 优先队列
- 【优先队列】Black Box
- pku 1442 Black Box(优先队列)
- Black Box (优先队列)
- Black Box(优先队列)
- 优先队列模拟最大堆和最小堆,poj 1442 Black Box
- POJ 2823 Sliding Window 再探单调队列
- c++必懂的基本概念
- FZU1894 志愿者选拔 --单调队列
- POJ 3250 Bad Hair Day --单调栈(单调队列?)
- POJ 2263 Heavy Cargo 多种解法
- POJ 1442 Black Box -优先队列
- HDU 1896 Stones --优先队列+搜索
- Variant does not reference an auomation object
- UVA 12266 Stock prices --优先队列
- UVA 11235 Frequent Values ---RMQ
- UVA 11573 Ocean Currents --BFS+优先队列
- POJ 3264 Balanced Lineup -- RMQ或线段树
- UVA 439 Knight Moves --DFS or BFS
- 【转】ACM搜索算法总结 --By GreenHand