The kth great number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6014    Accepted Submission(s): 2434

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<cstdio>
#include<queue>
#include<vector>
using namespace std;
struct cmp
{
bool operator()(int x, int y)
{
return x > y;
}
};
priority_queue<int, vector<int>, cmp>q;
int main(int argc, char const *argv[])
{
int n, k, temp;
char str[];
//freopen("in.c", "r", stdin);
while(~scanf("%d%d", &n, &k))
{
while(!q.empty())
q.pop();
for(int i = ;i < n;i ++)
{
scanf("%s", str);
if(str[] == 'I')
{
scanf("%d", &temp);
q.push(temp);
if(q.size() > k)
q.pop();
}
else
printf("%d\n", q.top());
}
}
return ;
}

线段树:

 #include<stdio.h>
#define MAX 1000005
typedef struct
{
int left, right, mid, num, l_cnt, r_cnt;
}NodeTree;
NodeTree node[*MAX];
void BuildTree(int k, int l, int r)
{
node[k].left = l;
node[k].right = r;
node[k].l_cnt = node[k].r_cnt = ;
node[k].mid = (l + r) >> ;
if(l == r)
return;
int mid = (l + r) >> ;
BuildTree(k << , l, mid);
BuildTree(k << |, mid+, r);
} void UpdateTree(int k, int num)
{
if(node[k].left == node[k].right)
{
node[k].num = num;
return ;
}
if(node[k].mid < num)
{
node[k].r_cnt ++;
UpdateTree(k << |, num);
}
else
{
node[k].l_cnt ++;
UpdateTree(k << , num);
}
} int GetRusult(int k, int pos)
{
if(node[k].left == node[k].right)
return node[k].num;
if(node[k].r_cnt >= pos)
GetRusult(k << |, pos);
else
GetRusult(k << , pos - node[k].r_cnt);
} int main(int argc, char const *argv[])
{
int n, k, a, i;
char str[];
//freopen("in.c", "r", stdin);
while(~scanf("%d%d", &n, &k))
{
BuildTree(, , n);
for(i = ;i < n;i ++)
{
scanf("%s", str);
if(str[] == 'I')
{
scanf("%d%d", &a);
UpdateTree(, a);
}
else
{
printf("%d\n", GetRusult(, k));
}
}
}
return ;
}

HDU --- 4006的更多相关文章

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

    /********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 题目大意: 第一行 输入 n k,后有 n 行,对于每一行有两种状态 ,①“I x” : 插入 ...

  3. hdu 4006 The kth great number

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 思路:利用优先队列的性质,将数据存入后会自动对数据进行排序 #include<stdlib ...

  4. hdu 4006/AvlTree

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 这道题以前用c语言写的Avltree水过了.. 现在接触了c++重写一遍... 由于没有删除操作 ...

  5. HDU 4006 优先队列

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  6. hdu 4006 优先队列 2011大连赛区网络赛F **

    签到题都要想一会 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...

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

    题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相 ...

  8. HDU 4006 The kth great number【优先队列】

    题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数 用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了 另外这个维护队列只有k个元素的时候需要注意一下 ...

  9. HDU 4006 The kth great number AVL解

    提供动态更新数据.第实时QK大量的值什么? 使用AVL统计数据结构做,比较先进的数据结构的内容. 不知道给出的数据为准值是否有反复.下面的程序是因为我能够处理重复数据出现的情况下,. 了repeat的 ...

随机推荐

  1. 经典关于多态的demo

    class Foo { public int a; public Foo() { a = 3; } public int addFive() { a += 5; return a; } public ...

  2. [日历] C#修改CNDate日历帮助类 (转载)

    点击下载 CNDate.rar 主要功能如下 .传回公历y年m月的总天数 .根据日期值获得周一的日期 .获取农历 #region 私有方法 private static long[] lunarInf ...

  3. 微软未公开的 SP

    一些用在SQL 2000的企业管理GUI中,并且不打算用于其他的流程.微软已预计将其中的一些存储过程从未来的SQL Server版本中删除(或已经删除了).虽然这些存储过程可能很有用并为你节省了很多时 ...

  4. OC - 13.数据解析(JSON与XML)

    ##数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需 ...

  5. IOS pop使用代理传值

    假如oneViewController页面push到OtherViewController页面,然后你想从OtherViewController页面pop到oneViewController页面的时候 ...

  6. 学习protobuf

    一.认识Protobuf ref:http://blog.csdn.net/program_think/article/details/4229773摘要:1. protobuf是一个开源项目.2. ...

  7. C++primer(第五版)读书笔记&习题解答---CHAPTER 3

    C++标准库类型包括:string,vector和迭代器,其中string是可变长的字符序列,vector存放的是某种给定类型对象的可变长序列,迭代器是string和vector的配套类型,常被用于访 ...

  8. CentOS如何查看端口是被哪个应用/进程占用

    有时启动应用时会发现端口已经被占用,或者是感觉有些端口自己没有使用却发现是打开的.这时我们希望知道是哪个应用/进程在使用该端口. CentOS下可以用netstat或者lsof查看,Windows下也 ...

  9. 学渣也要搞 laravel(2)—— HTTP路由[1]篇

    前几天忙了,然后快两个星期没有发博客.今天正式回归.哈哈 1. 路由 说到路由当时学的时候给我疑惑了几天..没有仔细看文档.然后一脸蒙蔽的去用 postman[谷歌插件] 测试路由方法.然后就很奇怪 ...

  10. 一些static_cast const_cast

    static_cast:干杂活的,那三个都有各自的专有用途,那三个不做的都由这个转型符来做,只要它能做的,用C语法的强制类型转换运算符也一定能够完成:但话又说回来了,C强制类型转换能做的,它可不一定都 ...