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\) ...
随机推荐
- A1065 A+B and C (64bit) (20)(20 分)
A1065 A+B and C (64bit) (20)(20 分) Given three integers A, B and C in [-2^63^, 2^63^], you are suppo ...
- HDU - 6514 Monitor(二维差分)
题意 给定一个\(n×m\)的矩阵.(\(n×m <= 1e7\)). \(p\)次操作,每次可以在这个矩阵中覆盖一个矩形. \(q\)次询问,每次问一个矩形区域中,是否所有的点都被覆盖. 解析 ...
- 51NOD:1639-绑鞋带
传送门:https://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=475129 1639 绑鞋带 基准时间限制:1 秒 空间限制:131 ...
- SSH无密码登录及远程拷贝命令SCP的使用
SSH无密码登录 1.生成密钥对(公钥和私钥) $ cd /home/cen/.ssh $ ssh-keygen -t rsa #生成密钥,使用rsa方式进行加密,四个回车 $ ssh-copy-id ...
- logback mybatis 打印sql语句
logbac.xml 文件的基础配置参考的园友的 http://www.cnblogs.com/yuanermen/archive/2012/02/13/2349609.html 然后hibernat ...
- 想进BAT?这些面试题助你一臂之力
1 软性热身题 这种题目,考的就是你的软性能力,比如表达能力,理解能力,协调能力,一个词概括就是套路.这类题目会在面试开始热身的时候,问一道两题,不会多,但是如果你能回答的有条不紊,清晰达意,那么就会 ...
- Java学习6之泛型
1.泛型定义 1.泛型类: public class Generic <T>{ public void get(T t){ System.out.println(t); } } examp ...
- Python面向对象之什么是类(1)
1.C#.Java :只能用面向对象编程 Ruby.Python :函数编程+ 面向对象 面向对象编程不是在所有地方都比函数式编程方便的,类是为了封装,下面是简单的使用方法 在创建类的时候要用clas ...
- MQ、JMS以及ActiveMQ的了解和认识
新加入的公司中,架构用到了activeMq,对于以前只了解nginx.tomcat的我有点懵逼,所以在网上找点资料看看,了解下什么是MQ,activemq.具体作用是什么 MQ MQ简介: MQ全称为 ...
- c++ STL sort struct comp
详细解说 STL 排序(Sort) http://www.cppblog.com/mzty/archive/2005/12/15/1770.html 详细解说 STL 排序(Sort) 作者Winte ...