Educational Codeforces Round 34 D. Almost Difference【模拟/stl-map/ long double】
2 seconds
256 megabytes
standard input
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.
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.
Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
5
1 2 3 1 3
4
4
6 6 5 5
0
4
6 6 4 4
-8
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.
【题意】:计算每个数贡献的值之和。贡献规则如上分段函数所示。
【分析】:假设第i位值是a,那么他的贡献tmp=a*(i-1)-presum。tmp是假设都计算为y-x,presum是前缀和。那最后的贡献就是tmp-cnt(a-1)+cnt(a+1),相邻的数去掉。因为相邻的数和本身总是相差1,符合分段函数归0部分。前面出现过多少次a-1,a+1,用map实现。
【代码】:
#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 ;
}
Educational Codeforces Round 34 D. Almost Difference【模拟/stl-map/ long double】的更多相关文章
- 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) D - Almost Difference(高精度)
D. Almost Difference Let's denote a function You are given an array a consisting of n integers. You ...
- Educational Codeforces Round 34 C. Boxes Packing【模拟/STL-map/俄罗斯套娃】
C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Educational Codeforces Round 34 B. The Modcrab【模拟/STL】
B. The Modcrab time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 2 A. Extract Numbers 模拟题
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- Educational Codeforces Round 11B. Seating On Bus 模拟
地址:http://codeforces.com/contest/660/problem/B 题目: B. Seating On Bus time limit per test 1 second me ...
- 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
F - Clear The Matrix 分析 题目问将所有星变成点的花费,限制了行数(只有4行),就可以往状压DP上去靠了. \(dp[i][j]\) 表示到第 \(i\) 列时状态为 \(j\) ...
随机推荐
- HDU:2594-Simpsons’ Hidden Talents
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- WPF触控程序开发(四)——MultiTouchVista_-_second_release_-_refresh_2的救赎
起源 Multitouch是一款可用于Win7模拟触摸屏幕的开源软件(关于它的使用介绍),最后一次更新是在11年5月份,我是13年初开始用的,当时开发了一款类似IPhone相册的图片展示触控程序,就是 ...
- MVC中Spring.net 对基类控制器无效 过滤器控制器无效
比如现在我又一个BaseController作为基类控制器,用于过滤权限.登录判断等作用,其它控制由原本的继承Controller,改为继承BaseController.然后BaseControlle ...
- 8 REST Framework 实现Web API 1
1 参考博客: http://blog.csdn.net/SVALBARDKSY/article/details/50548073 2 准备工作 1. 环境 Python: Python 3.5 D ...
- Mac OS 添加 MySQL 环境变量
1.打开终端,输入: cd ~ 会进入~文件夹 2.如果 ~ 文件夹中已经存在 .bash_profile 文件,则在 : 后面追加你想要添加的路径, 3. 否则输入:touch .bash_prof ...
- python 类中__init__,__new__,__class__的使用详解
1.python中所有类默认继承object类,而object类提供了很多原始的内置属性和方法,所有用户定义的类在python 中也会继承这些内置属性.我们可以通过dir()进行查看.虽然python ...
- Goole Search Auto Complete
这个项目就九章算法大数据课程的一个项目.主要分为两步: 第一步是 offline 建立 数据库 我们用两个map reduce 的data pipline 来实现. 第二步是 online显示把数据里 ...
- sqlserver不能创建数据库关系图
use [你的数据库名]EXEC sp_changedbowner 'sa'
- IE IE8 iframe的onload方法分析 IE浏览器onload事件失效
判断iframe是否加载完成的完美方法 IE 支持 iframe 的 onload 事件,不过是隐形的,需要通过 attachEvent 来注册. 第二种方法比第一种方法更完美(采用readystat ...
- jQuery动态显示和隐藏datagrid中的某一列的方法
在EasyUI中: 1)展示某列的方法: $('#jgrid').datagrid('showColumn', 'XXX'); -----其中 XXX 是隐藏列的 field 属性值 2) ...