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 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.
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:
- d(a1, a2) = 0;
- d(a1, a3) = 2;
- d(a1, a4) = 0;
- d(a1, a5) = 2;
- d(a2, a3) = 0;
- d(a2, a4) = 0;
- d(a2, a5) = 0;
- d(a3, a4) = - 2;
- d(a3, a5) = 0;
- d(a4, a5) = 2.
哇,好不容易写到第四题,突然弹出消息说这题爆long long,然后就懵逼了,看了下状态AC的全是Python。赛后发现Hacker在疯狂Hack C++,血赚场?
怎么全世界都会long double,不过瞄到qls也被Hack了,窝q(小纠结.JPG)
#include <bits/stdc++.h>
using namespace std;
map<double,double>a;
int main()
{
int n;
scanf("%d",&n);
long double ans=;
for (int i=;i<n;i++)
{
double x;scanf("%lf",&x);
a[x]++;
ans= ans+ a[x+]-a[x-]+x*(i+-n+i);
}
cout << fixed << setprecision() << ans << endl;
return ;
}
q巨赛后补题代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=;
const ll BASE=1000000000000000000LL;
int a[MAXN];
int main()
{
int n;
scanf("%d",&n);
ll high=,low=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
low+=1LL*(*(i-)-(n-))*a[i];
while(low>=BASE)low-=BASE,high++;
while(low<)low+=BASE,high--;
}
map<int,int> mp;
for(int i=;i<=n;i++)
{
low-=mp[a[i]-],low+=mp[a[i]+];
while(low>=BASE)low-=BASE,high++;
while(low<)low+=BASE,high--;
mp[a[i]]++;
}
if(high>=- && high<=)printf("%lld\n",high*BASE+low);
else if(high>)printf("%lld%018lld\n",high,low);
else printf("%lld%018lld\n",high+(low>),(low>)*BASE-low);
return ;
}
Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)的更多相关文章
- Educational Codeforces Round 34 (Rated for Div. 2) A B C D
Educational Codeforces Round 34 (Rated for Div. 2) A Hungry Student Problem 题目链接: http://codeforces. ...
- Educational Codeforces Round 34 (Rated for Div. 2) C. Boxes Packing
C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Educational Codeforces Round 34 (Rated for Div. 2)
A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 34 (Rated for Div. 2) B题【打怪模拟】
B. The Modcrab Vova is again playing some computer game, now an RPG. In the game Vova's character re ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
随机推荐
- 原生JS获取url汇总
在WEB开发中,许多开发者都比较喜欢使用javascript来获取当前url网址,本文就此为大家总结一下比较常用获取URL的javascript实现代码 URL即统一资源定位符 (Uniform Re ...
- python字典练习
#!/bin/python3.4 # coding=utf-8 class lexicon(object): def __init__(self): print "define a clas ...
- dir/
dos窗口输入dir命令是显示磁盘目录命令: addslashes()使用反斜线转义字符串: exec($command,$output,$return)执行一个外部程序 $command:要执行的命 ...
- js点击添加
1.点击变色 <div id="dd" style="width:100px;height: 100px;background-color: #ccc"& ...
- Mint UI 之 Swipe 组件
#为什么不显示内容? 一定要指定 mt-swipe 元素的宽和高. <mt-swipe :auto="4000" class="swipe"> &l ...
- APP自动化测试怎么测?
一般来说工具还有技术都是千篇一律 测试测到最后还是业务能力...
- centos6.5 设置ssh无密码登录
:关闭防火墙 vim /etc/selinux/config 把SELINUX=enforcing修改为SELINUX=disabled A机器root连接B机器root用户 (root用户登录) ...
- 硬件GPIO,UART,I2C,SPI电路图
- 557. Reverse Words in a String III
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- Android中px, ppi, dpi, dp, dip, sp概念解析
Android中px, ppi, dpi, dp, dip, sp概念解析