Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
嗯。随机化与非随机化的比较!!!!!11352071 nblt 2388 Accepted 244K 0MS C++ 530B Code: /*随机化*/ #include<cstdio> #include<cstdlib> int n,a[10001],b[10001]; int find(int l,int r,int p) { if (l==r) return a[l]; int mid=rand()%(r-l)+l,x=a[mid],i=l-1,j=r+1; for (int k=l;k<=r;++k){ if (k==mid) continue; if (a[k]<x) b[++i]=a[k]; else b[--j]=a[k]; } b[++i]=x; for (int k=l;k<=r;++k) a[k]=b[k]; if (i==p) return x; if (i<p) return find(i+1,r,p); if (i>p) return find(l,i-1,p); } int main() { scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%d",&a[i]); printf("%d\n",find(1,n,(n+1)>>1)); } 11352057 nblt 2388 Accepted 244K 16MS C++ 489B Code1: /*非随机化*/ #include<cstdio> #include<cstdlib> int n,a[10001],b[10001]; int find(int l,int r,int p) { int mid=l,x=a[mid],i=l-1,j=r+1; for (int k=l;k<=r;++k){ if (k==mid) continue; if (a[k]<x) b[++i]=a[k]; else b[--j]=a[k]; } b[++i]=x; for (int k=l;k<=r;++k) a[k]=b[k]; if (i==p) return x; if (i<p) return find(i+1,r,p); if (i>p) return find(l,i-1,p); } int main() { scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%d",&a[i]); printf("%d\n",find(1,n,(n+1)>>1)); } 11352024 nblt 2388 Time Limit Exceeded C++ 491B Code2: #include<cstdio> int n,a[10001],b[10001]; int find(int l,int r,int p) { int mid=(l+r)>>1,x=a[mid],i=l-1,j=r+1; for (int k=l;k<=r;++k){ if (k==mid) continue; if (a[k]<x) b[++i]=a[k]; else b[--j]=a[k]; } b[++i]=x; for (int k=l;k<=r;++k) a[k]=b[k]; if (i-l+1==p) return x; if (i-l+1<p) return find(i+1,r,p-i); if (i-l+1>p) return find(l,i-1,p); } int main() { scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%d",&a[i]); printf("%d\n",find(1,n,(n+1)>>1)); } 注:此程序仅供参考 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator