您的位置:首页 > 其它

#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

//快排分治,选择一半排序 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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: