max-Queue(堆)
2016-04-21 14:32
549 查看
【题目描述】
Use a max-Heap to design a priority queue with integer value. Each element stores an integer. The larger the integer is, the higher the priority of the element is.
【题目输入】
The first line contains the total number of test cases.
For each test case, the first line has 2 numbers. The first one is the initial size of the queue, n. The second is the number of operation, m. The second line contains n integers, which are the initial elements. You should store them in the priority queue.
In the next m lines, each line represent an operation. 'I' indicates an insertion with an integer. 'P' denotes removing top element of the queue. We assume that the queue will never be empty.
1 <= n <= 100
【题目输出】
Output the top element after each operation.
【我的程序】
Use a max-Heap to design a priority queue with integer value. Each element stores an integer. The larger the integer is, the higher the priority of the element is.
【题目输入】
The first line contains the total number of test cases.
For each test case, the first line has 2 numbers. The first one is the initial size of the queue, n. The second is the number of operation, m. The second line contains n integers, which are the initial elements. You should store them in the priority queue.
In the next m lines, each line represent an operation. 'I' indicates an insertion with an integer. 'P' denotes removing top element of the queue. We assume that the queue will never be empty.
1 <= n <= 100
2 3 4 2 1 3 I 4 I 5 P P 4 4 10 8 9 12 I 1 I 20 P P
【题目输出】
Output the top element after each operation.
4 5 4 3 12 20 12 10
【我的程序】
#include <iostream> #define maxSize 150 using namespace std; int num, a[maxSize]; void swapOp(int x, int y) { int temp=a[x]; a[x]=a[y]; a[y]=temp; } void downOp(int x) { while (x*2<=num) { x*=2; if (x+1<=num && a[x+1]>a[x]) x++; if (a[x]>a[x/2]) swapOp(x,x/2); else break; } } void upOp(int x) { while (x/2>=1){ if (a[x]>a[x/2]) swapOp(x,x/2); else break; x/=2; } } int main() { int n,m,cas; cin>> cas; for (int j=0; j<cas; j++) { cin>> n>> m; num=n; for (int i=1;i<=n;i++) cin>>a[i]; for (int i=n/2; i>=1; i--) downOp(i); char ch; for (int i=0; i<m; i++) { cin>> ch; if (ch=='P'){ swapOp(1,num); num--; downOp(1); cout<< a[1]<< endl;} else{ cin>>a[++num]; upOp(num); cout<< a[1]<< endl; } } } return 0; }
相关文章推荐
- android studio build cpu 99%运行问题
- I.MX6 Ethernet UI patch failed
- Range Sum Query - Mutable
- Delegation Token can be issued only with kerberos or web authentication
- UIView层次管理(sendSubviewToBack,bringSubviewToFront)
- ERROR OGG-00446 No valid log files for current redo sequence 20415, thread 1, error retrieving re
- Handler,Message,MessageQueue,Looper之间的相爱相杀
- iOS-UITableView 设置分割线长度和颜色
- POJ 2299 Ultra-QuickSort 树状数组
- java web笔记——request&response
- [IOS 开发] NSOperationQueue常用方法
- Android学习笔记(40):SharedPreferences存储key-value对
- Android UI基础——ListView控件及Adapter适配器
- GradleUserGuide中文版 1)介绍 2)概述 3)教程 4)安装 5)故障 6)构建基础
- FZU 1894 志愿者选拔【单调队列】【monotone decreasing queue】
- [BS-03] 统一设置UITabBarController管理的所有VC的tabBarItem图标文字的颜色大小等属性
- UIImageView 点击放大缩小
- HDU 2604 Queuing(矩阵快速幂)
- HDU 2604 Queuing(矩阵快速幂)
- [置顶] UI设计资源收集