Codeforces 903D 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.

分析:刚拿到这题,先想到的是:归并排序,仔细构思了一下感觉细节很多,不大好写;转而去想线段树(归并排序能解决的线段树都行),绝对值差在1及以内的直接不考虑就行了,那么对于一个值x,要考虑的区间范围是[1,x-2]U[x+2,+inf],这样先离散化,然后线段树维护离散化后的下标,维护的信息有两个:区间和与区间计数(sum和cnt),因为对于x的查询结果sum和cnt,ans=cnt*x-sum,然后就是基本的单点更新,区间查询了。这道题要用高精度,我偷懒没写。主要原因是这道题其实用不着线段树,只要set或者map维护一下集合,然后维护前缀和即可。。这样写主程序代码长度不超过50行,比我100行的线段树代码清爽多了。

代码

Codeforces 903D Almost Difference的更多相关文章

  1. codeforces 903D

    D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces 1476G - Minimum Difference(带修莫队+根号平衡)

    Codeforces 题目传送门 & 洛谷题目传送门 震惊!我竟然独立切掉了这道 *3100 的题! 虽然此题难度的确虚高,感觉真实评分也就 2800~2900 罢.但感觉还是挺有成就感的( ...

  3. codeforces 1244E Minimizing Difference (贪心)

    (点击此处查看原题) 题意分析 给出n个数,a1,a2...an,现在可以进行最多k次操作,每次操纵可以使得任意一个数自增或者自减,问经过最多k次操作后,n个数中的最大值-最小值最小为多少? 解题思路 ...

  4. Codeforces 1244E. Minimizing Difference

    传送门 首先减的顺序是无关紧要的,那么有一个显然的贪心 每次减都减最大或者最小的,因为如果不这样操作,最大的差值不会变小 那么直接把序列排序一下然后模拟一下操作过程即可,别一次只减 $1$ 就好 #i ...

  5. codeforces A. Difference Row

    link:http://codeforces.com/contest/347/problem/A 开始看起来很复杂的样子,但是刚写下样例,就发现因为中间的都消去了,其实起作用的就是最大值和最小值=_= ...

  6. codeforces A. Difference Row 解题报告

    题目链接:http://codeforces.com/problemset/problem/347/A 题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2 ...

  7. Codeforces gym101612 L.Little Difference(枚举+二分)

    传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...

  8. Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)

    D. Almost Difference Let's denote a function You are given an array a consisting of n integers. You ...

  9. Educational Codeforces Round 34 D. Almost Difference【模拟/stl-map/ long double】

    D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. Apache反向代理的配置

    Apache反向代理的配置 一: Mac系统自带apache服务器 1. 查看apache版本命令如下:   sudo apachectl -v 2. 启动apache   sudo apachect ...

  2. nginx日常维护常用命令

    http://www.jb51.net/article/47750.htm 一.简明nginx常用命令 1. 启动 Nginx poechant@ubuntu:sudo ./sbin/nginx 2. ...

  3. python每天进步一点点

    1. apply, 可以调用函数和参数,如apply(Fun,(xxx,xxx)) 2. Format, 很好处理字符串的对齐命名,其他进制,打印等. 3. input(),接受输入 4. str() ...

  4. 简单介绍什么是协程及其在ES6中的实现方式

    协程,英文名coroutine,是一种执行过程可以被暂停和恢复的方法.各个协程之间相互协作完成一个任务. 让我们来看一个关于发挥协程作用的例子.假定我们有一个生产者和消费者的关系,生产者创建物品并将物 ...

  5. VS源码编译QuaZip(Windows下)

    最近写个Qt demo,想要使用压缩和解压多个文件的功能,并不使用额外进程.网上参考了很多资料,发现只有QuaZip比较适合我的需求.但是QuaZip只提供源码,因此需要自己来编译. QuaZip简介 ...

  6. 《Linux系统编程手册》读书笔记——第2章基本概念

    操作系统的核心--内核 内核的职责 进程调度:Linux属于抢占式多任务操作系统,多个进程可同时驻留于内存,且每个进程都能获得对CPU的使用权.哪些进程获得对CPU的使用,以及每个进程能使用多长时间 ...

  7. Nginx (一) 认识Nginx并进行安装

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...

  8. java之自动过滤提交文本中的html代码script代码

    public class test { public static String Html2Text(String inputString) { String htmlStr = inputStrin ...

  9. 冒烟测试与BVT测试

    冒烟测试,它和回归测试的性质一样--只是一个测试活动,并不是一个测试阶段.冒烟测试贯穿于测试的任何一个阶段,单元测试.集成测试.系统测试里都有冒烟测试. 冒烟测试和其他所有的测试活动的目的不一样,它不 ...

  10. 自动生成getter,setter方法的插件lombok

    1.在InteiliJ IDEA上安装lombok插件,并重启 . 2.在pom.xml文件中添加依赖 <dependency>    <groupId>org.project ...