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. CSS画三角形引发的一些思考

      今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...

  2. Chart图形 [功能帮助类] Assistant创建显示图像的标签和文件 (转载)

    点击下载 Assistant.zip /// <summary> /// 类说明:Assistant /// 联系方式:361983679 /// 更新网站:[url=http://www ...

  3. javascript-设置div隐藏

    html code: <div class="title"> <ul id="col02_left_title"> <li> ...

  4. oracle sql语句

    一.ORACLE的启动和关闭1.在单机环境下要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下su - oracle a.启动ORACLE系统oracle>svrmgrlSVRM ...

  5. RESTful 架构风格概述

    http://blog.igevin.info/posts/restful-architecture-in-general/(非常好) http://blog.igevin.info/posts/re ...

  6. 数据库(学习整理)----4--Oracle数据查询(基础点1)

    其他: 计算机中的内存是线性的,一维. length('')计算字符的个数,而不是字节的个数 Oracle中的日期类型和数值类型的数据可以做运算符(>,=,<,<>)比较 如果 ...

  7. 查看当前linux系统位数

    linux系统也有位数之分,所以在linux上安装一些软件,比如jdk之类的就需要注意下版本. 查看linux系统位数最简单的命令(这里以redhat为例,不同版本linux命令也许不同) 命令1:g ...

  8. html5新增操作类名方式 classList

    如果一个元素有多个类名,要如何删除呢,jqeury提供了removeClass()这个api,如果不用插件,自己封装,可以这样 function removeClass(elm,removeClass ...

  9. HTML 5结构

    进行总体布局时候,具体可以用的方法. 1.大纲:文档中各内容区块的结构编排. 内容区块可以使用标题元素来展示各级内容区块的标题. 关于内容区块的编排可以分为“显示编排”和“隐式编排”. 显示编排:明确 ...

  10. centos 7.0防火墙导致vagrant端口映射失败

    在vagrant上部署了centos7.0后,Vagrantfile端口转发设置后,宿主机访问客户机站点还是无法访问,问题出在:centos7.0以上版本默认会安装firewalld防火墙, fire ...