求一串数字中第K大的数(STL+读入优化)
来源:互联网 发布:上海 数据恢复中心 编辑:程序博客网 时间:2024/04/24 19:21
#include <iostream>#include <stdio.h>#include <set>#include <algorithm>using namespace std;//读入优化函数int get_val(){ int ret=0; char c; while((c=getchar())!=' '&&c!='\n') ret=ret*10+c-'0'; return ret;}int main( ){ int a, n, k, size; multiset<int> mySet; multiset<int>::iterator iter; scanf("%d %d",&n,&k); size = 0; //保持set里面只有K个数,提高效率 do { a=get_val(); if(size++ < k) { mySet.insert(a); } else { iter = mySet.begin(); if(a > *iter) { mySet.erase(iter); mySet.insert(a); } } } while(--n ); iter = mySet.begin(); printf("%d\n",*iter); return 0;}
0 0
- 求一串数字中第K大的数(STL+读入优化)
- 【算法】输入一串数列,求数列中第K大的数
- STL求第k大的元素
- 求数组中第k大的数的位置
- 求数组中第k大的数
- 求排列中第k大的数--逆康托展开
- 求两个数组中第k大的数
- 求数组中第K大的数
- 求一个数组中第k大的数
- 求一个数组中第k大的数方法
- 求一个数组中第k大的数方法
- 求数组中第K大的数
- 求数组中第K大的数
- 求n个数中第k大的数、前K大的数、快速排序
- 求一个数组中前K大的数或者第K大的数
- 【数字之魅】寻找最大的K个数(求第k大的数)
- 求第K大的数~~
- 【算法】求第k大的数
- 程序员的学习和积累
- Lisp语言:散列表(hash table)
- Lisp语言:列表(List)
- 指针数组和数组指针
- shell script中的syntax error near unexpected token `then' 问题
- 求一串数字中第K大的数(STL+读入优化)
- Linux - vim安装 配置与使用
- 基于CentOS的pip本地源搭建方法
- Matlab实现Hough变换检测图像中的直线
- 10_android项目svn代码管理问题
- snidel 2014春夏新品 画册款切换材质连衣裙
- Visual Studio 2012设置Jquery/Javascript智能提示
- leetcode:Linked List Cycle II
- Oracle 忘记用户名和密码解决方法