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. js--小结⑤

    js中的for循环,while循环,do...while循环和C语言的一模一样 有几个问题要提醒一下的是 1.  null是对象,即object       undefined是undefined d ...

  2. (转)Java爬虫,信息抓取的实现

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23272657 今天公司有个需求,需要做一些指定网站查询后的数据的抓取,于是花了点 ...

  3. [Session] SessionHelper---C#操作Session的帮助类 (转载)

    点击下载 SessionHelper.rar 下面是代码大家看一下 这个类主要是关于Session的基本操作比如:1.获取Session值2.设置一个Session的值3.清空所有的Session4. ...

  4. javascript-设置div隐藏

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

  5. ios专题 - openSSL

    1  概述 1.1  产生背景 基 于万维网的电子商务和网上银行等新兴应用,极大地方便了人们的日常生活,受到人们的青睐.由于这些应用都需要在网络上进行在线交易,它们对网络通信的安全 性提出了更高的要求 ...

  6. POJ_1088 滑雪(记忆型DP+DFS)

    Description Michael喜欢滑雪,这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  7. SVM(支持向量机)算法

    第一步.初步了解SVM 1.0.什么是支持向量机SVM 要明白什么是SVM,便得从分类说起. 分类作为数据挖掘领域中一项非常重要的任务,它的目的是学会一个分类函数或分类模型(或者叫做分类器),而支持向 ...

  8. 正则过滤html标签

    var html = "<p>好好学习,<br>天天向上</p>"; var re=/<[^>]+>/g; var text ...

  9. CSS_Bootstrap

    ①BS学习的基础 第一个例子 <!DOCTYPE html> <html lang="en"> <head> <title>Boot ...

  10. "System.Web" 中不存在类型或命名空间

    System.Web”中不存在类型或命名空间名称script  /找不到System.Web.Extensions.dll引用 添加引用就行了...“添加引用→.Net→System.Web.Ente ...