博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)
阅读量:4489 次
发布时间:2019-06-08

本文共 1852 字,大约阅读时间需要 6 分钟。

D. Almost Difference

 

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.

 

哇,好不容易写到第四题,突然弹出消息说这题爆long long,然后就懵逼了,看了下状态AC的全是Python。赛后发现Hacker在疯狂Hack C++,血赚场?

怎么全世界都会long double,不过瞄到qls也被Hack了,窝q(小纠结.JPG)

#include 
using namespace std;map
a; int main(){ int n; scanf("%d",&n); long double ans=0; for (int i=0;i

q巨赛后补题代码:

#include
using namespace std;typedef long long ll;const int MAXN=200005;const ll BASE=1000000000000000000LL;int a[MAXN];int main(){ int n; scanf("%d",&n); ll high=0,low=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); low+=1LL*(2*(i-1)-(n-1))*a[i]; while(low>=BASE)low-=BASE,high++; while(low<0)low+=BASE,high--; } map
mp; for(int i=1;i<=n;i++) { low-=mp[a[i]-1],low+=mp[a[i]+1]; while(low>=BASE)low-=BASE,high++; while(low<0)low+=BASE,high--; mp[a[i]]++; } if(high>=-1 && high<=0)printf("%lld\n",high*BASE+low); else if(high>0)printf("%lld%018lld\n",high,low); else printf("%lld%018lld\n",high+(low>0),(low>0)*BASE-low); return 0;}

 

 

 

 

转载于:https://www.cnblogs.com/weimeiyuer/p/8053364.html

你可能感兴趣的文章
document
查看>>
Hadoop下大矩阵乘法Version2
查看>>
iPhone内存溢出——黑白苹果
查看>>
Struts2学习笔记(十二) 类型转换(Type Conversion)(下)
查看>>
tcpdump学习
查看>>
局域网内传输文件速度慢
查看>>
Linux的核心版本(摘抄)
查看>>
CASE表达式
查看>>
zkw线段树
查看>>
作业1226
查看>>
mainline.js主线
查看>>
fseek()
查看>>
Python学习笔记——PyQt控件中文字居中显示
查看>>
JAVA环境下利用solrj二次开发SOlR搜索的环境部署常见错误
查看>>
Beta阶段敏捷冲刺前准备
查看>>
mini web框架-3-替换模板
查看>>
Siamese Network简介
查看>>
svg学习(三)rect
查看>>
博客园博文生成章节目录
查看>>
ruby 模块 的引入
查看>>