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大的数,用线段树超时了,至今不知道为什么超时,最后用优先队列过的。
优先队列:
  1. #include<cstdio>
  2. #include<queue>
  3. #include<vector>
  4. using namespace std;
  5. struct cmp
  6. {
  7. bool operator()(int x, int y)
  8. {
  9. return x > y;
  10. }
  11. };
  12. priority_queue<int, vector<int>, cmp>q;
  13. int main(int argc, char const *argv[])
  14. {
  15. int n, k, temp;
  16. char str[];
  17. //freopen("in.c", "r", stdin);
  18. while(~scanf("%d%d", &n, &k))
  19. {
  20. while(!q.empty())
  21. q.pop();
  22. for(int i = ;i < n;i ++)
  23. {
  24. scanf("%s", str);
  25. if(str[] == 'I')
  26. {
  27. scanf("%d", &temp);
  28. q.push(temp);
  29. if(q.size() > k)
  30. q.pop();
  31. }
  32. else
  33. printf("%d\n", q.top());
  34. }
  35. }
  36. return ;
  37. }

线段树:

  1. #include<stdio.h>
  2. #define MAX 1000005
  3. typedef struct
  4. {
  5. int left, right, mid, num, l_cnt, r_cnt;
  6. }NodeTree;
  7. NodeTree node[*MAX];
  8. void BuildTree(int k, int l, int r)
  9. {
  10. node[k].left = l;
  11. node[k].right = r;
  12. node[k].l_cnt = node[k].r_cnt = ;
  13. node[k].mid = (l + r) >> ;
  14. if(l == r)
  15. return;
  16. int mid = (l + r) >> ;
  17. BuildTree(k << , l, mid);
  18. BuildTree(k << |, mid+, r);
  19. }
  20.  
  21. void UpdateTree(int k, int num)
  22. {
  23. if(node[k].left == node[k].right)
  24. {
  25. node[k].num = num;
  26. return ;
  27. }
  28. if(node[k].mid < num)
  29. {
  30. node[k].r_cnt ++;
  31. UpdateTree(k << |, num);
  32. }
  33. else
  34. {
  35. node[k].l_cnt ++;
  36. UpdateTree(k << , num);
  37. }
  38. }
  39.  
  40. int GetRusult(int k, int pos)
  41. {
  42. if(node[k].left == node[k].right)
  43. return node[k].num;
  44. if(node[k].r_cnt >= pos)
  45. GetRusult(k << |, pos);
  46. else
  47. GetRusult(k << , pos - node[k].r_cnt);
  48. }
  49.  
  50. int main(int argc, char const *argv[])
  51. {
  52. int n, k, a, i;
  53. char str[];
  54. //freopen("in.c", "r", stdin);
  55. while(~scanf("%d%d", &n, &k))
  56. {
  57. BuildTree(, , n);
  58. for(i = ;i < n;i ++)
  59. {
  60. scanf("%s", str);
  61. if(str[] == 'I')
  62. {
  63. scanf("%d%d", &a);
  64. UpdateTree(, a);
  65. }
  66. else
  67. {
  68. printf("%d\n", GetRusult(, k));
  69. }
  70. }
  71. }
  72. return ;
  73. }

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. 学习使用Et采集的过程和分析

  2. [时间操作] C#TimeHelper时间格式化帮助类 (转载)

    点击下载 TimeHelper.rar 主要功能如下 .将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 .将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 . ...

  3. Oracle 11g 新特性(一)-- 虚拟列

    数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Oracle11g 增加了虚拟列的新特性, 具体说明如 ...

  4. ie8中parseInt字符型数值转换数值型问题

    今天在ie8中测试项目发现一个奇怪的问题,"08" "09" 强转竟然变成了: 后来发现ie8把"08" "09" 默认 ...

  5. 接触.net5年了,感觉自己的知识面很狭隘。

    08年毕业找工作期间开始接触网页开发,由于在学校了混了4年时间,我只能从html标记语言开始学习,后来应聘到一个网站建设公司,开始学习ps.Dreamweaver和asp.由于基础薄弱,一个月后离开了 ...

  6. 【elasticsearch】(1)centos7 使用yum安装elasticsearch 2.X

    前言 elasticsearch(下面称为ES)是一个基于Lucene的搜索服务器(By 百度百科:查看).所以他需要java的环境即jdk,这里提供懒人一键安装方式 # yum install ja ...

  7. Forward reference vs. forward declaration

    Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forwa ...

  8. www.nygwkt.com

    南京宁阳制冷设备维修有限公司是专业从事厨房空调,http://www.nygwkt.com岗位空调制冷设备设计.制造.安装.改造.维修.保养的专业化公司.在南京享有很高的客户评论. 我们对南京宁阳制冷 ...

  9. bzoj 2734: [HNOI2012]集合选数 状压DP

    2734: [HNOI2012]集合选数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 560  Solved: 321[Submit][Status ...

  10. 你真的了解 MySQL 数据库的运行状况吗?

    2015年第三方市场调查机构 Evans 数据公司最近公布的一系列客户调查数据显示,在过去两年里,MySQL 在所有开发者使用的数据库中获得了25%的市场份额,Evans 公司的本次调查显示,数据库的 ...