COJ978 WZJ的数据结构(负二十二)
2015-07-03 11:09
274 查看
试题描述 |
输入两个正整数N、K,以及N个整数Ai,求第K小数。 |
输入 |
第一行为两个正整数N、K。 第二行为N个正整数Ai。 |
输出 |
输出第K小数。 |
输入示例 |
5 4 1 2 3 3 5 |
输出示例 |
3 |
其他说明 |
1<=K<=N<=200000 -10^9<=Ai<=10^9 |
如在A[i]--A
中查找k小,则先nth_element(A+1,A+k,A+n+1),再输出A[k]即可,时间复杂度为O(N)。
#include<cstdio> #include<cctype> #include<queue> #include<cstring> #include<algorithm> #define rep(s,t) for(int i=s;i<=t;i++) #define ren for(int i=first[x];i!=-1;i=next[i]) using namespace std; inline int read() { int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c=='-') f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f; } const int maxn=2000010; int A[maxn]; int main() { int n=read(),k=read(); rep(1,n) A[i]=read(); nth_element(A+1,A+k,A+n+1); printf("%d\n",A[k]); return 0; }
View Code
相关文章推荐
- Codeforces Round #309 (Div. 1) D. Nudist Beach 数据结构
- Trie树学习--数据结构一发
- 数据结构:链表,栈堆,队列
- 数据结构基础温故-1.线性表(下)
- 数据结构之---C语言实现哈夫曼树和编码
- 离散数学Notes
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome (DP+字典树)
- 【数据结构】堆栈和队列
- 2015华为软赛(四)——数据结构设计
- 数据结构复习001
- Binder学习一——Binder基础数据结构
- 数据结构与算法之二叉树(一)-----建立二叉树以及反转二叉树
- COJ979 WZJ的数据结构(负二十一)
- 编程之久除了算法和数据结构 什么也不属于我们
- 数据机构与算法-数据结构的一些基本概念
- 数据机构与算法-数据结构的一些基本概念
- 基于C语言的通用数据结构和算法库
- 【数据结构】Java单链表的逆序
- JS数据结构之BinarySearchTree
- COJ980 WZJ的数据结构(负二十)