题目意思:

有n种操作,I代表插入数据,q代表输出第k大的数。

区间的大数据的操作,基本是用树操作。

这到题目可以用三种方法来做。

(1) 线段树

考虑到题目可能有负数,给每个值加500000直接贴代码。

s[k].n代表在这个区间内的数的个数。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int n,k;
const int N=1500005;
struct node
{int l, r;int n;
}s[N*4+1100];void bulid(int l,int r,int k)
{if(l==r){s[k].l=l;s[k].r=r;s[k].n=0;return ;}s[k].l=l;s[k].r=r;s[k].n=0;int mid=(l+r)>>1;bulid(l,mid,k<<1);bulid(mid+1,r,k<<1|1);s[k].n=s[2*k].n+s[2*k+1].n;
}void Insert(int d,int k)
{if(s[k].l==s[k].r&&s[k].l==d){s[k].n++;return;}int mid=(s[k].l+s[k].r)/2;if(d<=mid) Insert(d,2*k);else  Insert(d,2*k+1);s[k].n=s[2*k].n+s[2*k+1].n;
}int query(int k,int a)
{if(s[k].l==s[k].r){return s[k].l;}int ans;if(a<=s[k<<1].n) ans=query(k<<1,a);else if(a>s[k<<1].n) ans=query(k<<1|1,a-s[k<<1].n);s[k].n=s[2*k].n+s[2*k+1].n;return ans;
}
int main()
{while(scanf("%d%d",&n,&k)!=EOF){bulid(1,N,1);getchar();int total=0;for(int i=1;i<=n;i++){char c;scanf("%c",&c);getchar();if(c=='I'){int tt;scanf("%d",&tt);getchar();Insert(tt+500000,1);total++;}else if(c=='Q'){printf("%d\n",query(1,total-k+1)-500000);}}}
}

第二种方法:

用优先队列。

优先队列内部维护一个堆。

优先队列中只要维护k个数就好。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
struct Node
{int a;friend bool operator < (const struct Node n1,const struct Node n2){return n1.a>n2.a;}
};
int main()
{int n,k;while(scanf("%d%d",&n,&k)!=EOF){getchar();priority_queue<Node>q;for(int i=1;i<=n;i++){char c;scanf("%c",&c);getchar();if(c=='I'){Node tt;scanf("%d",&tt.a);getchar();q.push(tt);while(q.size()>k){q.pop();}}else {printf("%d\n",q.top().a);}}}
}

第三种方法是用set.

set内部维护一颗红黑树。

参见:http://blog.csdn.net/cnh294141800/article/details/21657969

#include<stdio.h>
#include<iostream>
#include<set>
using namespace std;
int main()
{int n,i,j,m,k;while(scanf("%d %d",&n,&k)!=EOF){multiset<int>s;multiset<int>::iterator it;for(i=0;i<n;i++){char a[2];scanf("%s",a);if(a[0]=='I'){scanf("%d",&m);s.insert(m);if(s.size()>k)s.erase(s.begin());}else{printf("%d\n",*s.begin());}}}return 0;
}

hdu 4006 The kth great number 线段树/优先队列/set相关推荐

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

    优先队列头文件 #include <queue> 默认优先为从大到小优先. 自定义优先级 1 struct cmpmin{ //按从小到大 2 3 // 因为标准库默认使用元素类型的< ...

  2. 【HDU - 4217 】Data Structure? (线段树求第k小数)

    题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...

  3. HDU 6203 ping ping ping lca 线段树成段更新

    题目链接:HDU 6203 ping ping ping Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)

    链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...

  5. hdu 5493 Queue(逆序对,线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5493 解题思路: 一道线段树的题目,因为是前面或者后面有k个比自己高的人,所以我们应该按照由身高从小到 ...

  6. Minimum Inversion Number 线段树

    The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that s ...

  7. HDU - 3564 Another LIS(LIS+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意 给出1~n的插入顺序,要求每次插入之后的LIS 分析 首先用线段树还原出最终序列.因为插入的顺序是按 ...

  8. 题解报告:hdu 1754 I Hate It(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某 ...

  9. HDU - 6070 Dirt Ratio (二分 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题目大意:给定一个序列a,对于任何一个区间 [l,r],它的"Dirt Ratio&q ...

最新文章

  1. jupyter notebook中执行命令报错No module named ‘wordcloud‘
  2. 物联网基础之了解CCP协议,COAP协议,MTQQ协议等
  3. Lua的清屏快捷方式
  4. 大数据项目产品选型的五个建议
  5. Python之路(第三十一篇) 网络编程:简单的tcp套接字通信、粘包现象
  6. 【Apache】 配置 (http协议的) vhost
  7. 分析设计网上书店数据库,并画E-R图
  8. 结合 Mist 在本地测试网络上实现代币智能合约
  9. Django: OperationalError / no such table
  10. web端兼容性测试相关知识
  11. Futter基础第1篇: 实现输出Hello World【MaterialApp、Scaffold】
  12. Let's encrypt 通配域名(二级, 三级)
  13. CISSP考点拾遗——关于道德
  14. 嵌入式编程中的堆栈溢出检测
  15. 验证电话号码,支持手机座机可加国家代码和区号,座机支持分机
  16. turtle(海龟)知识点整理
  17. H5页面微信自动登录,和微信页面自定义分享样式
  18. 【小白必读】机器学习入门须知
  19. NBA GLOSSARY
  20. Word文档如何进行压缩文件?

热门文章

  1. 网络编程释疑之:TCP半开连接的处理
  2. 2_3 ProxyMode.cpp 代理模式
  3. python中的线程threading.Thread()使用
  4. 使用VMware VSphere WebService SDK进行开发 (七)——获取数据中心、集群、主机、虚拟机的目录结构
  5. 剑指offer--剪绳子
  6. LiveVideoStack调查问卷
  7. 毕业五年的音视频开发工程师过得怎么样了?
  8. 数据结构与算法之转圈打印矩阵和旋转正方形矩阵
  9. hls之m3u8、ts流格式详解
  10. 协调多个对象之间的交互——中介者模式