D. Almost Difference
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's denote a function

You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

Input

The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of the array.

Output

Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

Examples
input
5
1 2 3 1 3
output
4
input
4
6 6 5 5
output
0
input
4
6 6 4 4
output
-8
Note

In the first example:

  1. d(a1, a2) = 0;
  2. d(a1, a3) = 2;
  3. d(a1, a4) = 0;
  4. d(a1, a5) = 2;
  5. d(a2, a3) = 0;
  6. d(a2, a4) = 0;
  7. d(a2, a5) = 0;
  8. d(a3, a4) =  - 2;
  9. d(a3, a5) = 0;
  10. d(a4, a5) = 2.

题意很好理解,这里就不说了。

解题思路:由给出的公式可以得到d(a1,an)+d(a2,an)+d(a3,an)+...+d(ai,an)的答案是i*an-sum (sum是a1~ai中与an差的绝对值大于1的数之和,那么这样只需要遍历一遍数组,执行i*an-sum并减去a1~ai中与an差的绝对值小于等于1的数就行了。所以还要用map记录一下。

坑点:注意数据1e9,1e9-2,1e9-4...-1e9+4,-1e9+2,-1e9 (共有200000个 ,这种时候答案近似于100000*100000*1e9 数据会爆long long,所以要用long double

附ac代码:

 1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <iostream>
5 #include <algorithm>
6 #include <map>
7 #include <iomanip>
8 using namespace std;
9 typedef long double ld;
10 const int maxn = 200000+10;
11 ld nu[maxn];
12 map<ld,ld>mp;
13 int main()
14 {
15 int n;
16 scanf("%d",&n);
17 for(int i=0;i<n;++i)
18 cin>>nu[i];
19 ld s=0;
20 ld sum=0;
21 mp.clear();
22 for(int i=0;i<n;++i) //根据推出的公式可得,i从0开始比较简单
23 {
24 s+=i*nu[i]-sum+mp[nu[i]+1]-mp[nu[i]-1];
25 //s+=((i-mp[nu[i]-1]-mp[nu[i]+1]-mp[nu[i]])*nu[i]-(sum-mp[nu[i]+1]*(nu[i]+1)-mp[nu[i]-1]*(nu[i]-1)-mp[nu[i]]*[i]));
26 mp[nu[i]]++;
27 sum+=nu[i];
28 }
29 cout<<fixed<<setprecision(0)<<s<<endl;
30 return 0;
31 }

codeforces 903D的更多相关文章

  1. Codeforces 903D Almost Difference

    Codeforces 903D Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. 被集群节点负载不均所困扰?TKE 重磅推出全链路调度解决方案

    引言 在 K8s 集群运营过程中,常常会被节点 CPU 和内存的高使用率所困扰,既影响了节点上 Pod 的稳定运行,也会增加节点故障的几率.为了应对集群节点高负载的问题,平衡各个节点之间的资源使用率, ...

  2. luogu P1453 城市环路

    题目描述 整个城市可以看做一个N个点,N条边的单圈图(保证图连通),唯一的环便是绕城的环路.保证环上任意两点有且只有2条路径互通.图中的其它部分皆隶属城市郊区. 现在,有一位名叫Jim的同学想在B市开 ...

  3. ModelForm的基本用法:

    一.ModelForm的基本用法示例: from django import forms from app01 import models class BookModelForm(forms.Mode ...

  4. JavaScript中原型对象的应用!

    JavaScript中原型对象的应用! 扩展内置对象的方法 我以数组对象为例! // 原型对象的应用 扩展内置对象方法! Array.prototype.sum = function() { var ...

  5. Go GC: Latency Problem Solved

    https://talks.golang.org/2015/go-gc.pdf https://www.oschina.net/translate/go-gc-solving-the-latency- ...

  6. luoguP2657 [SCOI2009] windy 数

    目录 luoguP2657 [SCOI2009] windy 数 简述题意: Solution: luoguP2657 [SCOI2009] windy 数 简述题意: 不含前导零且相邻两个数字之差至 ...

  7. Scala中使用fastJson 解析json字符串

    Scala中使用fastJson 解析json字符串 添加依赖 2.解析json字符 2.1可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象 2.2然 ...

  8. Linux slabtop命令——显示内核片缓存信息

    Linux内核需要为临时对象如任务或者设备结构和节点分配内存,缓存分配器管理着这些类型对象的缓存.现代Linux内核部署了该缓存分配器以持有缓存,称之为片.不同类型的片缓存由片分配器维护. slabt ...

  9. 在线安装mysql

    http://www.cnblogs.com/wishwzp/p/7113403.html

  10. nginx教程<二>(高可用)

    1.nginx集群 对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求,这时候需要多台服务器对大量的请求进行分流处理,即负载均衡. 而如果实现负载均衡,必须在网站的入口部署服务器 ...