Calculate Sum-Of-Absolute-Differences

1000(ms)
65535(kb)
131 / 683
给你N个正整数,求两两之差的绝对值之和。 比如有4个数分别为 3,2,6,5,则答案为 |3-2| + |3-6| + |3-5| + |2-6| + |2-5| + |6-5| =14

输入

测试有很多组,对于每一组测试:
第一行为一个正整数 N( 2<= N <=20000)。
第二行有N正个整数,每个数不大于 1000000。

输出

每组测试输出一行。测试数据保证结果在int范围内。

样例输入

4
3 2 6 5
3
1 2 3

样例输出

14
4
 #include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
void result(int a[],int n);
int N;
while(scanf("%d",&N)!=EOF)
{
int *arr;
arr=(int *)malloc(sizeof(arr)*N);
int i;
for(i=; i<N; i++)
scanf("%d",&arr[i]);
result(arr,N);
free(arr);
}
return ;
}
void result(int a[],int n)
{
sort(a,a+n);
int sum=;
int i,j; for(i=; i<n; i++)
{
sum=sum+abs(a[i]-a[i+])*(n--i)*(i+); //经过排序之后每一个数字只需要与它后面所有数字相减就可以,
//    比如2356,(2-3)*3*1就相当于把后面的数字都减掉了一个2,因为5-2=3=(3-2)+(5-3);6-2=3-2+5-3+6-5=4;所以n-1-i就是后面数字的个数,i+1就是当前是第几位。
}
printf("%d\n",sum);
}
 

swust oj 237的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  5. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

  9. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

随机推荐

  1. [再寄小读者之数学篇](2015-06-24 Series)

    (AMM. Problems and Solutions. 2015. 03) Let $\sed{a_n}$ be a monotone decreasing sequence of real nu ...

  2. ntp 服务:Server dropped: Strata too high

    1.通过ntpdate -d 服务端IP,显示Server dropped: Strata too high vi /etc/ntp.conf 在ntpd服务端的配置中添加 server 127.12 ...

  3. docker创建Redis集群

    开始工作: yum install wegt ##安装下载工具 yum install net-tools ##安装网络工具 yum install tree ##安装tree命令(方便查看集群配置文 ...

  4. 【easy】202. Happy Number

    happy number Write an algorithm to determine if a number is "happy". A happy number is a n ...

  5. asp.net MVC路由配置总结

    URL构造 命名参数规范+匿名对象 routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}&qu ...

  6. (原创)cocos2dx-lua TableView官方demo分析

    本来是想看看网上的教程文章,结果看了好几篇,复制代码各种报错,有很多不存在的类和变量,根本用不了. 所以干脆自己去看官方demo,经过自己分析测试,已经大概会用了,顺便记录一下. 以下是代码,复制粘贴 ...

  7. linux软件管理

    People who cannot find time for recreation are obliged sooner or later to find time for illness.腾不出时 ...

  8. 基于BootStrap的initupload()实现Excel上传和获取excel中的数据

    简单说明:后边要做exl解析(还没做呢),所以先有一个excel的的上传以及获取excel中的数据,展示出来. 代码: //html代码 <div class="btn-group&q ...

  9. win10安装mysql5.7.20解压版

    mysql安装包可到官网下载,地址:https://dev.mysql.com/downloads/mysql 1.首先解压文件包,我这解压到E:\install_work\mysql目录下: 2.发 ...

  10. 饮冰三年-人工智能-Python-23 Python PyCharm 使用中常见的问题

    一:软件工具使用中遇到的问题 1:AttributeError: module 'pip' has no attribute 'main'问题 处理方法: a:找到JetBrains\PyCharm ...