/*用STL set 去维护集合的大小为k*/
Problem : 4006 ( The kth great number ) Judge Status : Accepted
RunId : 6313274 Language : C++ Author : CherryChou
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
RunId : 6313274 Language : C++ Author : CherryChou
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<iostream> #include<cstdio> #include<string.h> #include<set> #include<algorithm> int n,k; char ch; using namespace std; int main(){ while(cin>>n>>k){ int x; multiset<int> aa; while(n--){ cin>>ch; if(ch=='I'){ scanf("%d",&x); aa.insert(x); if(aa.size()>k) aa.erase(aa.begin());} else printf("%d\n",*aa.begin());}} return 0; }
本文介绍了一种利用C++ STL set的数据结构来解决寻找数组中第K大的元素的问题。通过插入和删除操作确保set的大小始终为K,从而在每次查询时都能快速获得当前第K大的元素。
&spm=1001.2101.3001.5002&articleId=8580283&d=1&t=3&u=2181405aabc64b43b9ad96c635324c15)
389

被折叠的 条评论
为什么被折叠?



