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

求助:I got WA 3x

Posted by F117_A at 2006-05-12 21:55:52 on Problem 2231
#include<stdio.h>
#include<stdlib.h>
struct cow{
	double position,pre,back;
};
int cmp(const void *x, const void *y)
{
	return (*(cow*)x).position - (*(cow*)y).position;
}
void main()
{
	int n;
	double sum=0;
	scanf("%d",&n);
	cow *cc=new cow[n];
	for(int i=0;i<n;i++){
		scanf("%lf",&cc[i].position);
		cc[i].pre=cc[i].back=0.0;
	}
	qsort(cc,n,sizeof(cow),cmp);
    for(i=1;i<n;i++)cc[0].back+=cc[i].position-cc[0].position;
    sum=cc[0].back;
	for(i=1;i<n;i++){
		cc[i].pre=cc[i-1].pre+i*(cc[i].position-cc[i-1].position);
		cc[i].back=cc[i-1].back-(n-i)*(cc[i].position-cc[i-1].position);
		sum+=cc[i].pre+cc[i].back;
	}		
	printf("%.0lf\n",sum);
}

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