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. CodeMonkey少儿编程第2章 turnTo对象

    目标 了解对象的概念 了解方法与对象的关系 掌握turnTo指令的用法 在开始本章的学习之前,我们先来复习一下上一章的知识点. 在第1章中,我们学会了在这个游戏中最简单的两个指令. step x 其中 ...

  2. EFCore 5 新特性 —— Savepoints

    EFCore 5 中的 Savepoints Intro EFCore 5中引入了一个新特性,叫做 Savepoints,主要是事务中使用,个人感觉有点类似于 Windows 上的系统还原点,如果事务 ...

  3. 安装jdk-windows系统

    1. 明确需要安装的jdk版本,注意jdk存在小版本号,例如jdk1.7_51,如果不清楚小版本号的话建议安装最新版本的jdk: 2. 打开cmd命令窗口,输入java -version查看本机是否安 ...

  4. scrapy框架的中间件

    中间件的使用 作用:拦截所有的请求和响应 拦截请求:process_request拦截正常的请求,process_exception拦截异常的请求 篡改请求的头信息 def process_reque ...

  5. vue+element-ui:table表格中的slot 、formatter属性

    slot 插槽,table中表示该行内容以自定义方式展示 :formatter 方法,用来格式化内容 Function(row, column, cellValue, index) html < ...

  6. 阿里云 CentOS7中搭建FTP服务器

    1配置 vsftpd-3.0.2-27.el7.x86_64 阿里云 centos 7.0 2 ftp工作模式 2.1 ftp通道 ftp工作会启动两个通道: 控制通道,数据通道 在ftp协议中,控制 ...

  7. Linux sudo权限提升漏洞整改方法

    一.漏洞概述 1月26日,Sudo发布安全通告,修复了一个类Unix操作系统在命令参数中转义反斜杠时存在基于堆的缓冲区溢出漏洞.当sudo通过-s或-i命令行选项在shell模式下运行命令时,它将在命 ...

  8. 如何实现new,call,apply,bind的底层原理。

    new做了什么? new是用来实例化对象的,当new了一个对象后 1.创建一个新对象 2.将构造函数的作用域赋值给新对象(this指向新对象) 3.执行构造函数里面的代码(为这个新对象添加属性) 4. ...

  9. __del__ PyPy和CPython的不同点 动态编译(注意不是解释) 析构函数被调用的次数

    小结 1.cpy的垃圾回收会对调用__del__多次:pypy仅仅一次: https://www.liaoxuefeng.com/wiki/1016959663602400/1016966024263 ...

  10. 20200927gryz校赛心得

    今天gyh学长给我们办了一场校内模拟赛,特地跑来记录一下心得 昨天晚上问了一下lkp学长,听说题目不卡常,不毒瘤,因此我在考试前20分钟仍在若无其事的练习着刚学的强连通分量,丝毫不慌 结果虽然rank ...