codeforces 903D
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.
题意很好理解,这里就不说了。
解题思路:由给出的公式可以得到d(a1,an)+d(a2,an)+d(a3,an)+...+d(ai,an)的答案是i*an-sum (sum是a1~ai中与an差的绝对值大于1的数之和,那么这样只需要遍历一遍数组,执行i*an-sum并减去a1~ai中与an差的绝对值小于等于1的数就行了。所以还要用map记录一下。
坑点:注意数据1e9,1e9-2,1e9-4...-1e9+4,-1e9+2,-1e9 (共有200000个 ,这种时候答案近似于100000*100000*1e9 数据会爆long long,所以要用long double
附ac代码:
1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <iostream>
5 #include <algorithm>
6 #include <map>
7 #include <iomanip>
8 using namespace std;
9 typedef long double ld;
10 const int maxn = 200000+10;
11 ld nu[maxn];
12 map<ld,ld>mp;
13 int main()
14 {
15 int n;
16 scanf("%d",&n);
17 for(int i=0;i<n;++i)
18 cin>>nu[i];
19 ld s=0;
20 ld sum=0;
21 mp.clear();
22 for(int i=0;i<n;++i) //根据推出的公式可得,i从0开始比较简单
23 {
24 s+=i*nu[i]-sum+mp[nu[i]+1]-mp[nu[i]-1];
25 //s+=((i-mp[nu[i]-1]-mp[nu[i]+1]-mp[nu[i]])*nu[i]-(sum-mp[nu[i]+1]*(nu[i]+1)-mp[nu[i]-1]*(nu[i]-1)-mp[nu[i]]*[i]));
26 mp[nu[i]]++;
27 sum+=nu[i];
28 }
29 cout<<fixed<<setprecision(0)<<s<<endl;
30 return 0;
31 }
codeforces 903D的更多相关文章
- Codeforces 903D Almost Difference
Codeforces 903D Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途
注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途 介绍: 如果你想将在SpringBoot项目中的配置类进行排序,那么用到spring-boot-au ...
- 三十二:WEB漏洞-文件操作之文件下载读取全解
文件下载读取 原路,检测,利用,修复 利用 数据库配置文件下载或者读取后续 接口密钥信息文件下载或者读取后续 文件名,参数值,目录符号 read.xxx?filename= down.xxx?file ...
- 初次使用Open Live Writer
关于下载和配置 建议大家不要在官网下载,会出不来.华军软件园(或其他下载站)也提供Open Live Writer最新版的下载. 创建账户时千万不要写错地址,错一个就失败. 体验 体验还是很好的,美中 ...
- 两种方式,花五分钟就能构建一个 Spring Boot 应用
前言 Spring Boot 的好处自然不必多说,对于想要从事 Java 工作的朋友们来说,可谓是必学的技能. 在我看来,它的优势就是多快好省. 功能多,很多常用的能力都有集成: 接入快,简单的几行代 ...
- C++ /Python 将视频中的片段转为图片
配置OpenCV :项目名称->右击->属性 VC++目录 包含目录 放 ...\build\include ...\build\include\opencv ...\build\ ...
- (15)-Python3之--configparser模块
1.模块简介 configparser模块是python用来读取配置文件的模块,置文件的格式跟windows下的ini或conf配置文件相似,可以包含一个或多个节(section), 每个节可以有多个 ...
- MapReduce过程源码分析
MapReduce过程源码分析 Mapper 首先mapper完成映射,将word映射成(word,1)的形式. MapReduce进程,Map阶段也叫MapTask,在MapTask中会通过 ...
- 分布式缓存 — redis
redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存,事件发布或订阅,高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串,哈希,列表,队列,集合结构直接存取, ...
- (二)基于Netty的高性能Websocket服务器(netty-websocket-spring-boot)
@toc Netty是一款基于NIO(Nonblocking I/O,非阻塞IO)开发的网络通信框架,对比于BIO(Blocking I/O,阻塞IO),他的并发性能得到了很大提高. 1.Netty为 ...
- 数理统计5:指数分布的参数估计,Gamma分布,Gamma分布与其他分布的联系
今天的主角是指数分布,由此导出\(\Gamma\)分布,同样,读者应尝试一边阅读,一边独立推导出本文的结论.由于本系列为我独自完成的,缺少审阅,如果有任何错误,欢迎在评论区中指出,谢谢! 目录 Par ...