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 200630740316 at 2008-05-22 07:40:41 on Problem 1723
#include <iostream>
#include <math.h>
#define N 20000
using namespace std;


void sort(int a[],int m,int n)
{
	int i,k,j,t;
		for(i=m;i<n-1;i++)
		{
			k=i;
			for(j=i+1;j<n;j++)
				if(a[j]<a[k])
					k=j;
			t=a[k];a[k]=a[i];a[i]=t;
		}
}

int partition (int a[],int p,int r,int x)
{
    int i=p, j=r-1; 
    while(i<j)
	{
		while(i<j&&a[j]>=x)
			--j;
		a[i]=a[j];
		while(i<j&&a[i]<=x)
			++i;
		a[j]=a[i];
	}
	a[i]=x;
    return i;
}


int select (int a[N],int p,int r,int k)
{
	int i,t,j;
	int x;
	if(r-p<75)
	{
		sort(a,p,r);
		if(p+k<=0)
			return a[p+k];
		else
			return a[p+k];
	}
	for(i=0;i<=(r-p-4)/5;i++)
	{
		sort(a,p+5*i,p+5*i+4);
		t=a[p+i];
		a[p+i]=a[p+5*i+2];
		a[p+5*i+2]=t;
	}
	x=select(a,p,p+(r-p-4)/5,(r-p-4)/10);
	i=partition(a,p,r,x);
	j=i-p;
	if(k<=j)
		return select(a,p,i,k);
	else
		return select(a,i+1,r,k-j);

}
void input(int x[],int y[],int &n)
{
	int i;
	for(i=0;i<n;i++)
	{
		cin>>x[i];
		cin>>y[i];
	}
	
 }



int main()
{
	int n,steps,i,mid_y,mid_x,x[N],y[N],cx[N];
	cin>>n;
	steps=0;
	input(x,y,n);
	mid_y=select(y,0,n,n/2);
	for(i=0;i<n;i++)
		steps=steps+abs(y[i]-mid_y);
	sort(x,0,n);
	for(i=0;i<n;i++)
		cx[i]=x[i]-i;
	mid_x=cx[n/2];
	for(i=0;i<n;i++)
		steps=steps+abs(x[i]-mid_x-i);
	cout<<steps<<endl;
	return(0);
}

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