BUPT Summer Journey #test9 A
来源:互联网 发布:mac地址绑定错误 编辑:程序博客网 时间:2024/06/05 11:53
A. diffsum 2014新生暑假个人排位赛09
时间限制 1000 ms 内存限制 65536 KB
题目描述
You are givin an array of integers, and you are to figure out the sum of differences between each pair of integers belonging to the array. SEE THE HINT FOR MORE INFORMATION.
输入格式
There are multiple test cases. The first line contains an integer n(n<=1e5), size of the array. The second line contains n integers, ai(|ai|<=100000), the array.
输出格式
For each case, output one line, the answer.
输入样例
41 1 2 2
输出样例
4hintfor the test case the answer is abs(1-1)+abs(1-2)+abs(1-2)+abs(1-2)+abs(1-2)+abs(2-2)=4.
思路:先排序使其具有大小关系。然后进行一轮O(n)的递推即可。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#define maxn 200005//#define LOCALusing namespace std;int n,a[maxn];long long ans;int main(){ #ifdef LOCAL freopen("input.txt","r",stdin); #endif // LOCAL while(scanf("%d",&n)==1) { long long sum=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]);sum+=a[i]; } sort(a+1,a+1+n); sum=sum-(long long)n*(long long)a[1]; ans=sum; //long long ans=sum; //printf("%I64d\n",ans); int j=1; for(int i=n-1;i>=2;i--) { sum=sum+(long long)i*(long long)(a[j]-a[j+1]); ans+=sum; j++; } printf("%lld\n",ans); } return 0;}
0 0
- BUPT Summer Journey #test9 A
- BUPT Summer Journey #test9 B
- BUPT Summer Journey #test9 C
- BUPT Summer Journey #test9 D
- BUPT Summer Journey #test1 A
- BUPT Summer Journey #test2 A
- BUPT Summer Journey #test3 A
- BUPT Summer Journey #test4 A
- BUPT Summer Journey #test5 A
- BUPT Summer Journey #test6 A
- BUPT Summer Journey #test7 A
- BUPT Summer Journey #test8 A
- BUPT Summer Journey #test10 A
- BUPT Summer Journey #test11 A
- BUPT Summer Journey #test1 B
- BUPT Summer Journey #test1 C
- BUPT Summer Journey #test1 D
- BUPT Summer Journey #test1 E
- 一个fork问题的思考
- php学习要点
- mac系统如何显示和隐藏文件 和 删除SVN隐藏文件
- 【水输入】#52 A. Bar
- poj2499
- BUPT Summer Journey #test9 A
- UVALive 5025 Arranging Your Team dfs
- 使用MJRefresh上下拉刷新时的问题
- 两种方法求单链表逆序
- 使用Zipalign优化Android应用
- Creating a ClickOnce application
- BUPT Summer Journey #test9 B
- Fijkstra算法C语言版本和MATLAB版本
- 最长公共临时文档7