题目链接:http://codeforces.com/problemset/problem/903/D
题目大意:给你n个数a1,a2,…,an,然后在1到n范围内求函数d(x,y)的和。
这道题可以这么想,就是先不考虑x,y的大小关系,直接认为d(x,y)= y - x
记最后结果为sum,那么sum = ∑d(x,y)=a2 - a1 + a3 - a1 + a3 - a2 + … +an - an-1 ,
即sum = ∑d(x,y)=∑(2i - n - 1)ai,其中i = 1到n。
显然上面的部分很容易求,然后我们只需求出多加的部分和少减的部分。
仔细一想,如果x = a,那么只有y = a + 1 或 a - 1的时候才会对d(x,y)产生影响,因此我们记录ai出现的个数。每当记录下一个ai的个数时,去计算因为这个ai而多算的部分和少算的部分(ai与之前的i - 1个数),处理完的sum就是最后的答案。
另外要注意的是这道题数据范围大,会爆long long,只能用long double 了。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
double arr[maxn];
int main()
{map<double,double>m;int n;long double sum = 0;scanf("%d", &n);for(int i = 1; i <= n; i ++){scanf("%lf",&arr[i]);sum += (i - 1) * (arr[i]);sum -= (n - i) * (arr[i]);m[arr[i]]++;sum -= m[arr[i] - 1];sum += m[arr[i] + 1];}cout<<fixed<<setprecision(0)<<sum<<endl;return 0;
}

转载于:https://www.cnblogs.com/asakuras/p/8079012.html

CF903 D.Almost Difference相关推荐

  1. ADPRL - 近似动态规划和强化学习 - Note 10 - 蒙特卡洛法和时序差分学习及其实例 (Monte Carlo and Temporal Difference)

    Note 10 蒙特卡洛法和时序差分学习 Monte Carlo and Temporal Difference 蒙特卡洛法和时序差分学习 Note 10 蒙特卡洛法和时序差分学习 Monte Car ...

  2. Linux: what's the difference between /proc/self and /proc/$$?

    我在Stack Overflow上提了一个问题:linux - what's the difference between /proc/self and /proc/$$? - Stack Overf ...

  3. python 集合set 的三大方法intersection union difference来处理文氏图

    TODO - 练习:A或B,但不能同时包含 编写一个函数,将两个集合(set_a和set_b)作为输入,并返回一个新的集合,其中包含set_a或set_b中的元素,但不包含两者兼有的元素. 在上面的文 ...

  4. Difference between prop and attr in different version of jquery

    jQuery <1.9 $('#inputId').attr('readonly', true); jQuery 1.9+ $('#inputId').prop('readonly', true ...

  5. What’s the Difference between a Hue, Tint, Shade and Tone ?

    What's the Difference between a Hue, Tint, Shade and Tone ? Ask Yourself first- What's the differenc ...

  6. G面经prepare: Set Intersection Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  7. LeetCode——Find the Difference

    LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...

  8. python 并集union, 交集intersection, 差集difference, 对称差集symmetric_difference

    python的集合set和其他语言类似,是一个无序不重复元素集, 可用于消除重复元素. 支持union(联合), intersection(交), difference(差)和sysmmetric d ...

  9. 理解一条语句:SELECT difference(sum(value)) FROM mq_enqueue WHERE channel =~ /ActiveMQ_TEST/ AND $tim...

    最近使用grafana在查询InfluxDB中,用到了这一条语句 SELECT difference(sum("value")) FROM "mq_enqueue&quo ...

最新文章

  1. .NET Core IdentityServer4实战-开篇介绍与规划
  2. Jan 09 - Number of 1 Bits; Bit Operation;
  3. 1.20 正则表达式详解
  4. python zen_Python的宗旨(Zen of Python)
  5. 阿里员工发帖吐槽人不如驴:你不能一边抽我,一边问我爱不爱你
  6. javascript 框架_克服JavaScript框架疲劳
  7. ros多机通讯的办法
  8. 免费素材大荟萃:免费图标和UI设计
  9. vue实现单页面多标签页
  10. [NLP]OpenNLP语言检测器的使用
  11. chromedriver下载链接以及对应版本
  12. 信号与系统_冲激函数匹配法
  13. 基于LCD1602的多功能万年历,温湿度计,非RTC时钟芯片单片机技术
  14. 鸿蒙智联 HarmonyOS Connect 设备小艺语音的接入
  15. RabbitMq(二)一文彻底弄懂RabbitMq的四种交换机原理及springboot实战应用
  16. 语义分析(Semantic Parsing)调研
  17. 井盖识别yolov5
  18. 多元统计分析——数据降维——因子分析(FA)
  19. 用于定义图像热区的html标记是,html 图像热区链接
  20. 代码整洁之道-读书笔记之整洁的代码

热门文章

  1. 国能93量产背后:恒大的理想与许家印的决心
  2. 批量删除过期文件的方法
  3. 作为PA AE工程师需要了解的知识
  4. PTA 7-18 花生换核桃
  5. C++:strcat、strcpy、strcmp、strupr、strlwr
  6. StandardMultipartHttpServletRequest$StandardMultipartFile cannot be cast to org.springframework.web
  7. 一台服务器插两个虚拟化显卡,显卡虚拟化解决方案大爆料
  8. Word怎么转换成Excel?教你个轻松转换的方法
  9. 阿里云服务器内网ip有什么用
  10. 如何给pdf文件添加水印实用方法