CF251A Points on Line 题解 二分

题目链接:http://codeforces.com/problemset/problem/251/A

题目描述

一条直线上有 \(n\) 个点,他们的坐标分别是 \(x_1,x_2, \cdots ,x_n\) 。没有两个点的坐标相同。
你可以从这些点中挑选出三个点,使得三个点的最大距离不超过 \(d\) 。
请求出满足条件的三个点的方案数。

输入格式

输入的第一行包含两个整数 \(n\) 和 \(d\) ( \(1 \le n \le 10^5; 1 \le d \le 10^9\) )。
接下来的一行包含 \(n\) 个整数 \(x_1,x_2, \cdots ,x_n\) ,分别表示 \(n\) 个点的坐标,坐标的绝对值不超过 \(10^9\) 。
输入保证坐标是按照递增顺序给你的。

输出格式

输出你选择三个点并且这三个点的最大距离不超过 \(d\) 的方案总数。

样例输入1

4 3
1 2 3 4

样例输出1

4

样例输入2

4 2
-3 -2 -1 0

样例输出2

2

样例输入3

5 19
1 10 20 30 50

样例输出3

1

【样例解释】
样例1中,任意三个点的组合都满足条件;
样例2中,只有两个组合 {-3, -2, -1} 和 {-2, -1, 0} 满足条件;
样例3中,只有一个组合 {1, 10, 20} 满足条件。
【样例分析】
这道题目涉及的算法:二分。
首先我们遍历 \(i\) ,我们设 \(x_i\) 是我们挑选的组合中的第1个元素,
那么我们可以用二分快速找出 \(x\) 中最大的那个满足 \(xj-xi \ge d\) 的坐标 \(j\) ,
那么 \(x_i\) 到 \(x_j\) 中,除了 \(x_i\) ,我们需要找出挑选任意两个数的组合,就是 \((j-i) \times (j-i-1)/2\) ,将其加到答案中。
最终循环一遍之后输出答案即可。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;

int n;
long long ans, d, a[maxn];

int main() {
    cin >> n >> d;
    for (int i = 0; i < n; i ++) cin >> a[i];
    for (int i = 0; i < n; i ++) {
        int j = upper_bound(a, a+n, a[i]+d) - a - 1;
        long long m = j - i;
        ans += m * (m-1) / 2LL;
    }
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/quanjun/p/12208882.html

时间: 2024-08-30 10:26:37

CF251A Points on Line 题解 二分的相关文章

codeforces 251A Points on Line(二分or单调队列)

Description Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't e

LeetCode-Max Points On a Line题解

一. 题意描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 二. 题目分析 看到这道题想到的第一种方法是暴力枚举法,时间复杂度为O(n3),显然是不够好的. 于是我们需要进行一些优化,注意到对于一条直线上的任意两点,他们连线的斜率是相同的.于是我们可以算出指定点到其他所有点的斜率,先使得包含指定点的直线上点数最多,这个过程中可以建立斜率和直线

CodeForces 251A. Points on Line(数学 lower_bound )

题目链接:http://codeforces.com/problemset/problem/251/A Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance be

codeforces251A. Points on Line

Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d. Not

【日常学习】【模拟双向链表】【疑问】Uva12657 - Boxes in a Line题解

这道题目我做的不对.事实上,我按书上的标程抄的,几乎一模一样,我认为他没有什么错误,可我就是不知道为什么我在代码仓库下的刘汝佳写的程序就AC,我写的就WA.跳了一下午,两程序样例输出完全一样(奇怪的是和书上答案不一样)一个字一个字的比对,就是找不出哪里不一样.我觉得极少不一样的地方应该没有影响,哪位大神愿意给看看? 这是一道双向链表,同样没有用指针,而是用两个数组模拟,道理和上面的那道非指针单向链表题目一样 刘汝佳的代码 // UVa12657 Boxes in a Line // Rujia

【Henu ACM Round#19 D】 Points on Line

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑l..r这个区间. 且r是满足a[r]-a[l]<=d的最大的r 如果是第一个找到的区间,则直接累加C(r-l+1,3); 然后l++ 然后考虑这个区间之后的下一个区间[l+1,R],这里R是满足a[R]-a[l+1]<=d的最大的R 如果R==r的话,l=l+1,continue; 否则. 如果l+1>r的话累加C(R-(l+1)+1,3); 如果l+1<=r的话 显然l..r和l+1..R之间有一个公共的部

[ 题解 ] [ 二分+模拟 ] E. Convention

http://codeforces.com/group/NVaJtLaLjS/contest/238203/problem/E 题意: 农夫的N只牛会在N[i]时间到达机场,农夫准备了M辆大巴去接.每辆大巴能载C只牛.(MC≥N) 问对于一只牛最小的最长等待时间是多少. 示例: Input: 6 3 2 1 1 10 14 4 3 Output: 4 你要明确这里要搜索什么.既然题目问的是等待时间,我们就搜索这个等待时间:不要想着怎样给这N个数据套M个区间,太复杂了. 搜索用二分,109不是开玩

CF600B Queries about less or equal elements 题解 二分

题目链接:http://codeforces.com/problemset/problem/600/B 题目大意: 给你一个长度为 \(n\) 的数组 \(a[]\) 和一个长度为 \(m\) 的数组 \(b[]\) . 对于数组 \(b[]\) 中的每一个元素 \(b_j\) ,你需要计算出 \(a[]\) 中有多少元素 \(a_i\) 是满足 \(a_i \le b_j\) 的. 解题思路: 本题涉及算法:二分. 需要先将数组 \(a[]\) 排序,然后对于每一个 \(b_j\) ,二分查找

Lightoj 1088 - Points in Segments 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个: 思路:寻找这些点中对于每条线段的上下界即可. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include