#1133 : 二分·二分查找之k小数 ( 快速排序, 分治 OR nth_element() 函数)
2015-04-13 21:35
537 查看
#1133 : 二分·二分查找之k小数
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
在上一回里我们知道Nettle在玩《艦これ》,Nettle的镇守府有很多船位,但船位再多也是有限的。Nettle通过捞船又出了一艘稀有的船,但是已有的N(1≤N≤1,000,000)个船位都已经有船了。所以Nettle不得不把其中一艘船拆掉来让位给新的船。Nettle思考了很久,决定随机选择一个k,然后拆掉稀有度第k小的船。 已知每一艘船都有自己的稀有度,Nettle现在把所有船的稀有度值告诉你,希望你能帮他找出目标船。
提示:非有序数组的二分查找之二
输入
第1行:2个整数N,k。N表示数组长度,
第2行:N个整数,表示a[1..N],保证不会出现重复的数,1≤a[i]≤2,000,000,000。
输出
第1行:一个整数t,表示t在数组中是第k小的数,若K不在数组中,输出-1。
样例输入
10 4
1732 4176 2602 6176 1303 6207 3125 1 1011 6600
样例输出
1732
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
在上一回里我们知道Nettle在玩《艦これ》,Nettle的镇守府有很多船位,但船位再多也是有限的。Nettle通过捞船又出了一艘稀有的船,但是已有的N(1≤N≤1,000,000)个船位都已经有船了。所以Nettle不得不把其中一艘船拆掉来让位给新的船。Nettle思考了很久,决定随机选择一个k,然后拆掉稀有度第k小的船。 已知每一艘船都有自己的稀有度,Nettle现在把所有船的稀有度值告诉你,希望你能帮他找出目标船。
提示:非有序数组的二分查找之二
输入
第1行:2个整数N,k。N表示数组长度,
第2行:N个整数,表示a[1..N],保证不会出现重复的数,1≤a[i]≤2,000,000,000。
输出
第1行:一个整数t,表示t在数组中是第k小的数,若K不在数组中,输出-1。
样例输入
10 4
1732 4176 2602 6176 1303 6207 3125 1 1011 6600
样例输出
1732
//快排分治,选择一半排序 31ms #include<cstdio> #include<algorithm> //#include<bits/stdc++.h> using namespace std; template<class T>inline T read(T&x) { char c; while((c=getchar())<=32)if(c==EOF)return 0; bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return 1; } template<class T> inline T read_(T&x,T&y) { return read(x)&&read(y); } template<class T> inline T read__(T&x,T&y,T&z) { return read(x)&&read(y)&&read(z); } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------ZCC IO template------ const int maxn=1000011; const double inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,t,n) for(int i=(t);i<(n);i++) typedef long long LL; typedef double DB; typedef pair<int,int> P; #define bug printf("---\n"); #define mod 10007 LL a[maxn]; LL k; void my_sort(LL left,LL right) { // for(int i=left;i<right;i++) // printf("%d ",a[i] ); // printf("\n"); if(left<right) { LL key=a[left]; LL low=left; LL high=right; while(low<high) { while(low<high&&a[high]>=key) high--; if(low<high)swap(a[high],a[low]); while(low<high&&a[low]<=key) low++; if(low<high)swap(a[high],a[low]); } // printf("[k==%lld , left =%lld right=%lld ]\n",k,low,high); if(low==k)return ;//选择一半进行排序即可 if(k<low) my_sort(left,low-1); else my_sort(low+1,right); } } int main() { //#ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); //#endif // ONLINE_JUDGE LL n,m,i,j,t; while(read_(n,k)) { For(i,1,n+1) { read(a[i]); } // nth_element(a,a+k-1,a+n); my_sort(1,n); if(k<=0||k>n)puts("-1");else writeln(a[k]); } return 0; } <pre name="code" class="cpp">//直接调用C++ STL函数 nth_element(start,start+k,end); 33ms #include<cstdio> #include<algorithm> //#include<bits/stdc++.h> using namespace std; template<class T>inline T read(T&x) { char c; while((c=getchar())<=32)if(c==EOF)return 0; bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return 1; } template<class T> inline T read_(T&x,T&y) { return read(x)&&read(y); } template<class T> inline T read__(T&x,T&y,T&z) { return read(x)&&read(y)&&read(z); } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------ZCC IO template------ const int maxn=1000011; const double inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,t,n) for(int i=(t);i<(n);i++) typedef long long LL; typedef double DB; typedef pair<int,int> P; #define bug printf("---\n"); #define mod 10007 LL a[maxn]; LL k; int main() { LL n,m,i,j,t; while(read_(n,k)) { For(i,0,n) { read(a[i]); } nth_element(a,a+k-1,a+n); if(k<0||k>n)puts("-1");else writeln(a[k-1]); } return 0; }
相关文章推荐
- #1133 : 二分·二分查找之k小数(优先队列或SBT或排序)
- hihoCoder 1133 二分·二分查找之k小数
- 快速排序qsort()函数和二分查找bsearch()函数
- #1128 : 二分·二分查找&ѭ : 二分·二分查找之k小数
- 【模版】快速排序,二分查找及其求其上下界函数
- 二分·二分查找之k小数
- #1128 : 二分·二分查找 ( 两种方法 先排序在二分O(nlogN) + 直接二分+快排思想O(2N) )
- 给大家分享下:快速排序、冒泡排序、二分查找
- 顺序表的增删查改、二分查找、冒泡和快速排序
- 插入,冒泡,选择,快速排序,二分查找
- leetcode 378. Kth Smallest Element in a Sorted Matrix有序矩阵寻找第K小数+二分查找
- #笔记#圣思园 JavaSE 第35讲——冒泡排序、交换排序、快速排序、二分查找
- 题目1 : 二分·二分查找
- 数组 获取最值(最大值 最小值)选择排序 冒泡排序 快速排序 半查找(二分查找) 十进制转其他进制法
- #1133 : 二分·二分查找之k小数
- c语言 快速排序 和 二分 查找 模型
- 快速排序和二分查找时间复杂度详解
- hiho一下 第三十七周二分·二分查找之k小数
- 分治算法的利用例子(查找最大最小数,快速排序,查看第k小的数字)
- 归并排序 && 快速排序 ——分治思想