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 優先佇列 用法佇列
- 堆與優先佇列佇列
- 淺談優先佇列佇列
- 優先佇列和堆排序佇列排序
- 01揹包優先佇列優化佇列優化
- 棧,佇列,優先順序佇列簡單介面使用佇列
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- leetcode621——優先佇列的思路LeetCode佇列
- STL優先佇列最小堆最大堆佇列
- 手擼優先佇列——二叉堆佇列
- C++ STL 優先佇列 (priority_queue)C++佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- 二叉堆實現優先佇列佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列
- 演算法面試(三) 優先佇列演算法面試佇列
- 【堆】【優先佇列】[NOIP2004]合併果子佇列
- Facebook的分散式優先順序佇列FOQS分散式佇列
- 最詳細版圖解優先佇列(堆)圖解佇列
- 堆、堆排序和優先佇列的那些事排序佇列
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- 【Dijkstra演算法】未優化版+優先佇列優化版演算法優化佇列
- codeforces round 974(div.3)E(優先佇列實現dijstra演算法,devc++的優先佇列用greater報錯)佇列JS演算法devC++
- CodeForces - 960B:Minimize the error(優先佇列+貪心)Error佇列
- Sunscreen POJ - 3614(防曬油) 貪心-優先佇列佇列
- [USACO 2009 Feb Gold] Fair Shuttle (貪心+優先佇列)GoAI佇列
- Python3 執行緒優先順序佇列( Queue)Python執行緒佇列
- MaxHeap 最大堆 MinHeap 最小堆 PriorityQueue 優先佇列實現佇列
- CSP之公共鑰匙盒(模擬、排序、優先佇列)排序佇列
- RMQ——支援合併和優先順序的訊息佇列MQ佇列
- 資料結構之PHP(最大堆)實現優先佇列資料結構PHP佇列
- Python 列表推導及優先順序佇列的實現Python佇列
- 個推基於 Apache Pulsar 的優先順序佇列方案Apache佇列
- 1284 海港 普及組 NOIP2016 佇列基礎 簡單列舉 簡單模擬 優先佇列(priority_queue)佇列
- 演算法與資料結構番外(1):優先佇列演算法資料結構佇列