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

这样也会超时呀,郁闷!!

Posted by ecjtubaowp at 2006-12-23 10:40:22 on Problem 2104
#include<stdio.h>
int a[100001],b[100001],c[100001];
__int64 num;
void merge(int *a,int p,int q,int r)
{
   int sa,ea,sb,eb,j=p,i;
   sa=p;ea=q-1;sb=q;eb=r;
   
   while(sa<=ea&&sb<=eb)
   {
      if(a[sa]<=a[sb]){b[j++]=a[sa];sa++;}
      else{b[j++]=a[sb];sb++;num+=(q-sa);}
       
     // printf("%d \n",num);   
   }
   while(sa<=ea) b[j++]=a[sa++];
   while(sb<=eb) b[j++]=a[sb++];
   for(i=p;i<=r;i++) a[i]=b[i];
}
void msort(int*a,int n)
{
  int i,j,k,m,n0,seg,lastseg,s0;
    n0=n;seg=1;lastseg=1;
    while(n0>=2)
    {m=n0;
     n0/=2;s0=seg*2;
     if(m%2==0)
     {
     for(i=1,j=0;i<=n0;j+=s0,i++)
     {
      if(i<n0)merge(a,j,j+seg,j+s0-1);
     else merge(a,j,j+seg,n-1);
      }//for   
      lastseg=seg+lastseg;
      seg=s0;
     }//if   
     else{ 
      for(i=1,j=0;i<=n0;j+=s0,i++)
      {
         merge(a,j,j+seg,j+s0-1);
         
      }
      j=n-lastseg-s0;
      merge(a,j,n-lastseg,n-1);
      lastseg+=s0;
      seg=s0;    
              
    }
}
}
int main()
{
 int i,j,k,m,n,l,i0;
 scanf("%d%d",&n,&m);
 for(l=1;l<=n;l++)
 scanf("%d",&a[l]);
 for(i=1;i<=n;i++)c[i]=a[i];
 while(m--)
  {
   scanf("%d%d%d",&i,&j,&k);
   //for(i0=i;i0<=j;i0++)c[i0]=a[i0];
   msort(a+i,j-i+1);
   printf("%d\n",a[k+i-1]);
   for(l=i;l<=j;l++)a[l]=c[l];
  }
 
}
/*
7 63
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output


5
6
3
*/

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