1007(優先佇列)
Problem Description
Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling
giddy. Now, try to help Xiao Bao.
Input
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming
will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
Output
The output consists of one integer representing the largest number of islands that all lie on one line.
Sample Input
8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q
Sample Output
1
2
3
題目大概:
要求我們求出所給數列的第k大數。
思路:
剛開始,我是用樹狀陣列做的,再加上二分,但是超時了。
因為用二分必須要有序,所以我用了排序,我感覺就是排序超時,但沒找到解決的好辦法,就用了這個優先佇列,畢竟看到這個題,很少有人會想到樹狀陣列。
程式碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct poin
{
int v;
friend bool operator<(poin a,poin b)
{
return a.v>b.v;
}
};
int main()
{
int n,k;int l;
poin s;
while(~scanf("%d%d",&n,&k))
{
priority_queue<poin>q;
char p[2];
while(n--)
{
scanf("%s",p);
if(p[0]=='I')
{
scanf("%d",&l);
s.v=l;
q.push(s);
int t=q.size();
if(t>k)q.pop();
}
else
{ poin su=q.top();
printf("%d\n",su.v);
}
}
}
return 0;
}
相關文章
- PHP優先佇列PHP佇列
- 堆--優先佇列佇列
- 優先佇列 (轉)佇列
- 淺談優先佇列佇列
- STL 優先佇列 用法佇列
- 堆與優先佇列佇列
- 堆和優先佇列佇列
- 優先佇列和堆排序佇列排序
- 堆排序與優先佇列排序佇列
- Java優先佇列(PriorityQueue)示例Java佇列
- 01揹包優先佇列優化佇列優化
- 棧,佇列,優先順序佇列簡單介面使用佇列
- Redis實現任務佇列、優先順序佇列Redis佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- 二叉堆優先佇列佇列
- POJ 3253 Fence Repair 優先佇列AI佇列
- 堆——神奇的優先佇列(上)佇列
- 優先佇列的效能測試佇列
- hdu5040 優先佇列+bfs佇列
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 演算法面試(三) 優先佇列演算法面試佇列
- STL優先佇列最小堆最大堆佇列
- STL醜數(set+優先佇列)佇列
- 【圖論】拓撲排序+優先佇列圖論排序佇列
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列
- 二叉堆實現優先佇列佇列
- STL(二十)priority_queue優先佇列容器佇列
- 找最小的k個數(優先佇列)佇列
- 優先佇列中過載運算子>和佇列
- C++ STL 優先佇列 (priority_queue)C++佇列
- 手擼優先佇列——二叉堆佇列