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. JKXY的视频内容下载工具类

    package cn.jsonlu.make.license; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONOb ...

  2. CI 笔记 easyui 结合后,左侧导航跳转问题

    1. 在进行时,还没有做完整个项目时,可能不是最终稿 2. 从数据库中nav表中,读出url地址,然后,从admin中,重写这些url跳转 3. 在admin的控制器中,跳转写的并不完美, publi ...

  3. ERROR ITMS-90167: "No .app bundles found in the package"

    http://stackoverflow.com/questions/37838487/error-itms-90167-no-app-bundles-found-in-the-package 简单说 ...

  4. cocos2d-x 2.2.0 如何在lua中注册回调函数给C++

    cocos2d-x内部使用tolua进行lua绑定,但是引擎并没有提供一个通用的接口让我们可以把一个lua函数注册给C++层面的回调事件.翻看引擎的lua绑定代码,我们可以仿照引擎中的方法来做.值得吐 ...

  5. tomcat上servlet程序的配置与处理servlet请求过程

    手动配置: tomcat服务器下web项目的基本目录结构 |-tomcat根目录 |-webapps |-WebRoot : web应用的根目录 |-静态资源(html+css+js+image+ve ...

  6. Android Broadcast管理

  7. Python核心编程2第三章课后练习

    1. 标识符.为什么Python 中不需要变量名和变量类型声明? Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程.每个变量在内存中创建,都包括变量的标识,名称和数据这些信息.每 ...

  8. maya 写UI 用到的工具

    import os cmds.window() scrollLayout = cmds.scrollLayout(w=150) cmds.gridLayout( numberOfColumns=30, ...

  9. Python: 设计模式 之 工厂模式例(2)(神奇的Python)

    #!/usr/bin/env python #coding=utf-8 # # 工厂模式第二例(神奇的Python) # 版权所有 2014 yao_yu (http://blog.csdn.net/ ...

  10. 从用python自动生成.h的头文件集合和类声明集合到用python读写文件

    最近在用python自动生成c++的类.因为这些类会根据需求不同产生不同的类,所以需要用python自动生成.由于会产生大量的类,而且这些类是变化的.所以如果是在某个.h中要用include来加载这些 ...