#include <stdio.h>
class CA
{
public:
enum{N=2000020};
void run();
int partion(int low,int high);
int ar[N],mn,mid;
};
int CA::partion(int low,int high)
{
int i,j,t,t1;
i=low;j=high;t=ar[(i+j)/2];
while(i<j)
{
while(i<j&&ar[i]<t) i++;
while(i<j&&ar[j]>t) j--;
if(i<j) {t1=ar[i];ar[i]=ar[j];ar[j]=t1;}
}
return i;
}
void CA::run()
{
mn=1;
int i,j,k,m,n;
scanf("%d",&m);
for(i=0;i<m;i++) scanf("%d",&ar[mn++]);
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&ar[mn++]);
mid=mn/2;
i=1;j=mn-1;
while(1)
{
k=partion(i,j);
if(k==mid)
{
printf("%d",ar[k]);
break;;
}
else if(k<mid)
{
i=k;
}
else if(k>mid)
{
j=k;
}
}
}
int main()
{
// freopen("test.in","r",stdin);
CA *a=new CA;
a->run();
return 0;
}
1029. Median (25)
最新推荐文章于 2019-03-19 15:03:21 发布