Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
O(n)(排序O(nlogn))#include <cstdio> #include <algorithm> #define MAX_N 10100 typedef long long ll; using namespace std; ll N,loc[MAX_N],dp[MAX_N],sum[MAX_N]; int main(){ scanf("%d",&N); for(int i = 1;i <= N;++i){ scanf("%d",&loc[i]); } sort(loc + 1,loc + N + 1); dp[1] = 0;sum[1] = loc[1]; for(int i = 2;i <= N;++i){ dp[i] = dp[i - 1] + (i - 1) * loc[i] - sum[i - 1]; sum[i] = sum[i - 1] + loc[i]; } printf("%I64d\n",dp[N] * 2); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator