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

所有数据都通过还是WA

Posted by xiaotang_ at 2012-01-12 17:32:58 on Problem 1723
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define N 10000
int n;
struct position
{
	int px;
	int py;
};

int cmp(const void *a,const void *b)
{
	return *((int *)a)-*((int *)b);
}

int median(int s[N],int size)
{
	
	if(size%2==1) {	return s[size/2];	}
	else
	{
		return (s[size/2]+s[size/2-1])/2;

	}
}
int _abs(int number)
{
if(number<0)
   return number*-1;
else
   return number;
}

int main()
{
	struct position soldier[N];
	int i;
	int midx,midy;
	int killer[N],distx[N];
	int result=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>soldier[i].px>>soldier[i].py;
	}
	for(i=0;i<n;i++)
	{
		killer[i]=soldier[i].py;
	}
	qsort(killer,n,sizeof(int),cmp);
	midy=median(killer,n);
	
	for(i=0;i<n;i++)
	{
		result+=_abs(midy-killer[i]);
	}

	for(i=0;i<n;i++)
	{
		killer[i]=soldier[i].px;
	}
	
	qsort(killer,n,sizeof(int),cmp);
	
	midx=median(killer,n);
	for(i=0;i<n;i++)
	{
		distx[i]=midx-(n/2-i);
	}
	for(i=0;i<n;i++)
	{
		result+=_abs(distx[i]-killer[i]);
	}
	cout<<result<<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