swustojCalculate Sum-Of-Absolute-Differences(0237)

2024-03-02 11:08

本文主要是介绍swustojCalculate Sum-Of-Absolute-Differences(0237),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

给你N个正整数,求两两之差的绝对值之和。 
比如有4个数分别为 3,2,6,5,则答案为 
|3-2| + |3-6| + |3-5| + |2-6| + |2-5| + |6-5| =14
Description
测试有很多组,对于每一组测试: 
第一行为一个正整数 N( 2<= N <=20000)。 
第二行有N正个整数,每个数不大于 1000000。
Input
每组测试输出一行。测试数据保证结果在int范围内。
Output
1
2
3
4
5
4
3 2 6 5
3
1 2 3
Sample Input
1
2
3
14
4

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include<stack>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;int main()
{int n;int a[20005];while (cin >> n){int sum = 0;for (int i = 0; i < n; i++){scanf("%d", &a[i]);}sort(a, a + n);for (int i = 0; i < n-1; i++){sum += abs(a[i] - a[i + 1])*(n - 1 - i)*(i + 1);}cout << sum << endl;}return 0;
}


这篇关于swustojCalculate Sum-Of-Absolute-Differences(0237)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/765914

相关文章

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不

最大流=最小割=最小点权覆盖集=sum-最大点权独立集

二分图最小点覆盖和最大独立集都可以转化为最大匹配求解。 在这个基础上,把每个点赋予一个非负的权值,这两个问题就转化为:二分图最小点权覆盖和二分图最大点权独立集。   二分图最小点权覆盖     从x或者y集合中选取一些点,使这些点覆盖所有的边,并且选出来的点的权值尽可能小。 建模:     原二分图中的边(u,v)替换为容量为INF的有向边(u,v),设立源点s和汇点t

如何导入sun.misc.BASE64Encoder和sum.misc.BASE64Decoder

右击项目名--->Build Path--->Configure Build Path...--->java Build Path--->Access rules:1 rule defined,added to all librar...   --->Edit --->Add...

18. 4 Sum

题目: 解答: 与之前的三数之和的解法类似,也是先排序,然后不断剔除不可能的条件,最后两个参数,通过两头求和计算得出。 代码: class Solution {public:vector<vector<int>> fourSum(vector<int>& nums, int target) {vector<vector<int>> result;int len = nums.size

apt-get update更新源时,出现“Hash Sum mismatch”问题

转载自:apt-get update更新源时,出现“Hash Sum mismatch”问题 当使用apt-get update更新源时,出现下面“Hash Sum mismatch”的报错,具体如下: root@localhost:~# apt-get update ...... ...... W: Failed to fetch http://us.archive.ubuntu.com/ub

[LeetCode] 303. Range Sum Query - Immutable

题:https://leetcode.com/problems/range-sum-query-immutable/description/ 题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums

[LeetCode] 64. Minimum Path Sum

题:https://leetcode.com/problems/minimum-path-sum/description/ 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers

[LeetCode] 404. Sum of Left Leaves

题:https://leetcode.com/problems/sum-of-left-leaves/description/ 题目 Find the sum of all left leaves in a given binary tree. Example: 3/ \9 20/ \15 7There are two left leaves in the binary t

html table td 中 使用相对定位(relative)和绝对定位(absolute)的div

absolute绝对定位的div放在td中,他的定位是相对于table的左上角。 relative相对定位的div放在td总共,他的定位是相对于本td的的左上角。 当把td设置成相对或者绝对定位,所有定位都正常了,也就是说在内层使用定位,外层必须也要使用定位。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN