hdu 4006 The kth great number (优先队列)

优先队列头文件

#include <queue>

默认优先为从大到小优先。

自定义优先级

 1 struct cmpmin{                         //按从小到大
 2
 3 // 因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
 4     bool operator ()(int &a, int &b){
 5         return a>b;                       //所以规定小的元素的优先级大于大的元素。
 6     }
 7 };
 8
 9 struct cmpmax{                        //按从大到小
10     bool operator ()(int &a, int &b){
11         return a<b;
12     }
13 };
14
15
16 //多个元素的时候
17 struct node{
18     int x, y;
19     friend bool operator <(int a, int b){        //小的优先级小,所以按照x从大到小,注意默认是<来确定优先级。
20         return a.x < b.x;
21     }
22 }
23
24 struct node{
25     int x, y;
26     friend bool operator <(int a, int b){        //小的优先级大,所以按照x从小到大
27         return a.x > b.x;
28     }
29 }

本题要输出第K大的数,因为数据量大,不能每次排序,所以用优先队列来解决。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <queue>
 5 using namespace std;
 6 struct cmp{
 7     bool operator ()(int &a,int &b){
 8         return a>b;//最小值优先
 9     }
10 };
11 int n, k, val, cnt;
12 char c;
13 int main(){
14     while(~scanf("%d%d", &n, &k)){
15         cnt = 0;
16         priority_queue <int, vector<int>,cmp> q;
17         while(n--){
18             cin>>c;
19             if(c == ‘Q‘){
20                 printf("%d\n", q.top());
21             }
22             else if(c == ‘I‘){
23                 scanf("%d", &val);
24                 if(cnt < k){
25                     q.push(val);cnt++;
26                 }
27                 else{
28                     if(val > q.top()){
29                         q.pop();
30                         q.push(val);
31                     }
32                 }
33             }
34         }
35
36     }
37
38
39     return 0;
40 }
时间: 2024-09-29 11:28:13

hdu 4006 The kth great number (优先队列)的相关文章

hdu 4006 The kth great number (优先队列)

1 /********************************************************** 2 题目: The kth great number(HDU 4006) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=4006 4 算法: 优先队列 5 ************************************************************/ 6 #include<cstdio> 7 #

hdu 4006 The kth great number(优先队列)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6982    Accepted Submission(s): 2837 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 4006 The kth great number(优先队列&#183;第K大数)

题意  动态查询第K大的数 用小数在前优先队列维护K个数  每要插入一个数时 若这个数小于队首元素那么就不用插入了  否则队首元素出队  这个数入队  每次询问只用输出队首元素就行了 #include<cstdio> #include<queue> using namespace std; int main() { int n, a, k; char op[5]; while(~scanf("%d%d", &n, &k)) { priority_

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 4006 The kth great number(multiset(或者)优先队列)

题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相同的数没直接放在相同的现有的数据后面的 #include<cstdio> #include<cstring> #include<algorithm> #include<set> using namespace std; //#define IN freopen(

HDU 4006 The kth great number (基本算法-水题)

The kth great number 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 mu

HDU 4006 The kth great number AVL解法

给出动态更新数据,实时问第K个大的数值是什么? 利用AVL数据结构做的一个统计数,比较高级的数据结构内容了. 不知道题目给出的数据值是否有重复,因为我下面的程序是可以处理出现数据重复的情况的. 其中的奥妙是增加了repeat的信息,可以知道出现了当前数组多少次. 主要是知道如何维护这些数据和如何查询,维护数据的函数是pushUp,查询函数是selectKth. 其他就是一般的AVL操作.个人觉得我的AVL写的还算蛮清晰的,有需要的参考一下. #include <stdio.h> inline

HDU 4006: The kth great number

The kth great number ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; while (cin >> n >> k) { char ch; int t; multiset<int>s; while (

hdoj 4006 The kth great number(优先队列)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 9415    Accepted Submission(s): 3756 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round

hdu 4006 The kth great number,set,priority_queue

The kth great number 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 feel