CodeForces 251A Points on Line(二分)
来源:互联网 发布:电信网络电视节目表 编辑:程序博客网 时间:2024/06/04 18:53
题目连接:CodeForces 251A Points on Line
题目大意:具体讲什么真心没看懂,不过最下面有对测试数据的解释,所以就大概懂了,就是给出n和k,然后给出一个大小为n的集合,问从集合中取出3个数,最大数与最小数的差值小于k的方式有多少种。
解题思路:很明显的二分啊,对于num[i], 假设num[i]必取,有多少种情况,令c = num[i] + k, 找到j,使得num[j] > c && num[j - 1] <= c, 这样(i, j)就为可选区间,注意是选取两个,记得用组合数的公式。
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;const int N = 100005;int n, dis, d[N];long long sum;long long count (long long ans) {if (ans < 2) return 0;return ans * (ans - 1) / 2;}int search(int c) {int l = -1, r = n;while (l < r) {int mid = (l + r) / 2;if (d[mid] < c) l = mid;else if (d[mid] > c) r = mid;else return mid + 1;if (l == r - 1) return r;}}void solve() {sum = 0;for (int i = 0; i < n; i ++)sum += count(search(d[i] + dis) - i - 1);}int main () {scanf("%d%d", &n, &dis);for (int i = 0; i < n; i++)scanf("%d", &d[i]);sort(d, d + n);solve();cout << sum << endl ;return 0;}
- CodeForces 251A Points on Line(二分)
- codeforces 251A Points on Line 二分 or 单调队列
- cf 251 A Points on Line 二分
- CodeForces - 251A Points on Line
- CodeForces 251A-Points on Line
- CodeForces 251A. Points on Line(数学 lower_bound )
- (leetcode)Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points On a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- max points on a line
- sicily 1176.Two ends
- Auto-Sync tips
- 理解车削 3dmax
- 三层中的工厂接口架构分析
- Ultraedit文件大概不是dos格式
- CodeForces 251A Points on Line(二分)
- C#数据类型
- 实战cacti配置
- python Tkinter 的image问题
- MonetDB用户指南4——(资源)内存使用Memory footprint
- 高效合并两个有序数组
- IOS UILabel
- 利用稀疏矩阵的“三元组表”存储结构,实现两个矩阵的相加。
- 根据Econometrics in R一书,将回归方法总结一下