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 linjianyu at 2011-04-09 11:45:31 on Problem 2231
In Reply To:库里的abs是怎么写的啊,是用位运算么,我自己写的函数超时 Posted by:darkcaller at 2011-04-05 13:08:19
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

int a[10001];

int main()
{
  int n,m,k,l;
  while(cin>>n&&n)
  {
    __int64 sum=0,M[10000]={0};
    int i;
   for(i=0;i<n;i++)
     cin>>a[i];
   sort(a,a+n);
   //for(i=0;i<n;i++)
    //cout<<a[i]<<endl;
   for(i=0;i<n;i++)
     M[0]+=(a[i]-a[0]);//求出M[0]; 
   for(i=1;i<n;i++)
    {
     M[i]=M[i-1]-(a[i]-a[i-1])*(n-i)+(a[i]-a[i-1])*(i);//排好序,前面减,后面加; 
    }
   for(i=0;i<n;i++)
   sum+=M[i];
   printf("%I64d\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