Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:原来最好的算法,就是使用分治思想的经典第k元素算法

Posted by lengxiang at 2010-08-09 11:15:18 on Problem 2104
In Reply To:Re:原来最好的算法,就是使用分治思想的经典第k元素算法 Posted by:accept123 at 2009-12-14 23:43:10
对于啊,我就是这个原因超时了,附代码……
#include<stdio.h>
#include<stdlib.h>
#define MAX 100005
int partition(int *a ,int p,int r)
{
     int i,t;
     i=p-1;
     for(int j=p;j<=r-1;j++)
     {
         if(a[j]<a[r])
         {
            i++;
            t=a[i];
            a[i]=a[j];
            a[j]=t;
         }
     }
     t=a[i+1];
     a[i+1]=a[r];
     a[r]=t;
     return i+1;
}
int qsort(int *a,int p,int r,int k)
{
    int t;
     if(p<=r)
     {
     int q=partition(a,p,r);
     if(k==q-p+1)return a[q];
     else if(k<q-p+1)
     {  
        t=qsort(a,p,q-1,k);
        return t;
     }
     else 
     {  
        t=qsort(a,q+1,r,k-(q-p+1));
        return t;
     }
     }
     return -1;
}

int main()
{
    int n,i,a[MAX],b[MAX],k,m,zz,z; 
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
       scanf("%d",a+i);
    for(i=0;i<m;i++)
    {
    scanf("%d%d%d",&z,&zz,&k);
    for(int j=z;j<=zz;j++)
      b[j]=a[j];
    printf("%d\n",qsort(b,z,zz,k));
    }
   // for(i=1;i<=n;i++)
  //     printf("%d",a[i]);
    system("pause"); 
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator