zoukankan      html  css  js  c++  java
  • 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 }
    View Code

    本题要输出第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
    ", 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 }
  • 相关阅读:
    jquery easy ui 简单字段选择搜索实现
    (转)EasyUI 分页总结
    EasyUI 搜索框
    微信公众号开发简单介绍
    【POJ3740】Easy Finding DLX(Dancing Links)精确覆盖问题
    推断View是否显示在界面上
    菜鸟调错(八)—— Maven编译错误:不兼容的类型的解决方式
    js对table操作(添加删除交换上下TR)
    NBUT 1222 English Game(trie树+DP)
    Android 返回键的处理
  • 原文地址:https://www.cnblogs.com/titicia/p/4055575.html
Copyright © 2011-2022 走看看