hdu 4006 The kth great number(线段树 || 优先队列)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4006

求一个数字序列的第K大的值。先输入两个数字n,k,接着是n行输入,I表示加入新的数字,Q是询问第k大的数字。

Sample Input
  
  
8 3 I 1 I 2 I 3 Q I 5 Q I 4 Q
 

Sample Output
  
  
1 2 3
练习线段树遇到这道题的,听说方法还挺多的。
线段树:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e6+5; //',INF=0x3f3f3f3f;
int ans[maxn];
struct node{
    int l,r,count;
    int mid(){  return (l+r)/2; }
    int length(){  return (r-l+1); }
}tree[maxn<<2];
void build(int a,int b,int root){
    tree[root].l=a;
    tree[root].r=b;
    tree[root].count=0;
    if(a==b){
        return ;
    }
    int m=tree[root].mid();
    build(a,m,2*root);
    build(m+1,b,2*root+1);
}
void insert(int root,int number){
    if(tree[root].l==tree[root].r){
         tree[root].count++;
         return ;
    }
    if(number<=tree[root].mid()) insert(2*root,number);
    else insert(2*root+1,number); // number-tree[2*root].count is wrong
    tree[root].count=tree[2*root].count+tree[2*root+1].count;
}
int query(int root,int number){
    if(tree[root].l==tree[root].r){
         return tree[root].l;
    }
    int ans;
    if(number<=tree[2*root].count)ans=query(2*root,number);
    else ans=query(2*root+1,number-tree[2*root].count);  //we need "number-tree[2*root].count"
    return ans;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int n,k;
    while(cin>>n>>k){
        build(1,maxn,1);
        char str[3];
        int top=0;
        for(int i=0;i<n;i++){
            scanf("%s",str);
            if(str[0]=='I'){
                int a;
                scanf("%d",&a);
                insert(1,a);
                top++;
            }
            else {
                 printf("%d\n",query(1,top-k+1));
            }
        }
    }
    return 0;
}

优先队列:
第一次写好的代码怀着忐忑的心情交上去,没错,果然超时了:
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=1e6+5;
int number[maxn];
int main()
{
    //freopen("cin.txt","r",stdin);
    int n,k;
    while(cin>>n>>k){
        priority_queue<int> que;
        char s[3];
        for(int i=0;i<n;i++){
            scanf("%s",s);
            if(s[0]=='I'){
                int a;
                scanf("%d",&a);
                que.push(a);
            }
            else {
                int j;
                for(j=0;j<k;j++){
                    number[j]=que.top();
                    que.pop();
                }
                printf("%d\n",number[j-1]);
                for(j=0;j<k;j++){
                    que.push(number[j]);
                }
            }
        }
    }
    return 0;
}
想个办法把第二个循环去掉吧,抓住主要矛盾,忽略次要因素:
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
const int maxn=1e6+5;
int number[maxn];
int main()
{
    //freopen("cin.txt","r",stdin);
    int n,k;
    while(cin>>n>>k){
        priority_queue<int,vector<int>,greater<int> > que;  //从小到大
        char s[3];
        for(int i=0;i<n;i++){
            scanf("%s",s);
            if(s[0]=='I'){
                int a;
                scanf("%d",&a);
                que.push(a);
                if(que.size()>k)que.pop();
            }
            else {
                printf("%d\n",que.top());
            }
        }
    }
    return 0;
}

记住要用functional头文件,不然会Compilation Error。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值