CF903 D.Almost Difference
来源:互联网 发布:js清空input的value值 编辑:程序博客网 时间:2024/06/10 07:45
题目链接: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个数),
多算的部分就是ai = aj + 1的部分,少算的部分就是ai = aj - 1的部分,
j为1到i的任意一个数(i≠j),sum -= 比ai大1的数的个数
sum += 比ai小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;}
阅读全文
0 0
- CF903 D.Almost Difference
- CF903 D.Almost Difference
- Codeforces 903D Almost Difference
- codeforces 903D. Almost Difference
- codeforces 903D Almost Difference(递推式)
- codeforces Educational Round 34 D Almost Difference
- codeforces 903 D. Almost Difference 线段树
- [Codeforces 903 D. Almost Difference]树状数组+大数模板
- Educational Codeforces Round 34 (Rated for Div. 2) D. Almost Difference(高精度)
- D. Almost Identity Permutations(dp)
- codeforces 888D.Almost Identity Permutations
- CodeForces 888D Almost Identity Permutations
- codeforces 888D Almost Identity Permutations (组合数+错排)
- HDU-5936-Difference(杭州CCPC D题)-二分
- HDU 5936 && 2016CCPC杭州 D: Difference(折半枚举)
- Almost done.
- Linux bootloader squeue AND runlevel info(rc.d init.d daemons S K difference ) RUNLEVEL
- What's the difference between Quartz Core, Core Graphics and Quartz 2D?
- 实验二运算符的重载,体验深拷贝
- 不要欺负我不懂java的volatile
- java HashSet / TreeSet 学习
- SVM分类器实践,检测是否有篮球
- 爬取百度词条内链接
- CF903 D.Almost Difference
- Dev c++ 高精度加法(500位以内)
- Ubuntu设置局域网Windows共享文件Samba
- HEVC一阶指数哥伦布编码
- SSM注解配置 往Spring Boot 过渡
- Leetcode 647.Palindromic Substrings(算法分析week15)
- LeetCode 102. Binary Tree Level Order Traversal
- 用筛法求之N内的素数
- 第十三周项目1