shadow__ @ 2017-10-20 14:47:35
#include<bits/stdc++.h>
using namespace std;
int a[100005],temp;
int N,K;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while ((ch<'0' || ch>'9')&&ch!='-') ch=getchar();
if (ch=='-')
{
f=0;
ch=getchar();
}
while (ch>='0' && ch<='9')
{
x=(x<<3)+(x<<1)+ch-48;
ch=getchar();
}
return f?x:-x;
}
inline int quickselect(int b,int e,int k)
{
int i=b,j=e,mid=a[(i+j)>>1];
while (i<=j)//注意,小于等于
{
while(a[i]<mid)i++;
while(a[j]>mid)j--;
if (i<=j)
{
swap(a[i],a[j]);
i++;
j--;
}
}
if (b<j&&k<=j)return quickselect(b,j,k);//分治
if (i<e&&k>=i)return quickselect(i,e,k);
return a[k];//如果不属于任何一方,就结束,返回
}
int main()
{
N=read();
for (int i=1; i<=N; i++)a[i]=read();
cout<<a[1]<<endl;
for(int i=3; i<=N; i+=2)cout<<quickselect(1,i,(i+1)/2)<<endl;
return 0;
}
by 青石巷 @ 2017-10-20 15:14:58
快速选择算法似乎是均摊O(n)的,你调用了n次,你觉得O(n^2)不会T吗……
by OuRiGe @ 2017-10-23 18:08:57
不就是初赛的题吗