题目链接:

https://vjudge.net/problem/35188/origin

题目大意:

要求你找到一个 i < j < k时有 a[k]-a[i] <= d的组的个数。

这道题的思路就是每输入一个a[i]就是找到那个第一个大于或等于a[i]-d的值!,设a[i]-d = mn, 则用lower_bound()函数可以得到这个第一个大于或者等于mn的值的下标。

关于lower_bound()函数的详细用法可以看这个博客:

https://blog.csdn.net/qq_40160605/article/details/80150252

然后得到下标后,就观察规律,发现这个长度是呈等差数列形式的,其中,a[1] = 1, a[n]  = i-idex-1

所以有其中三元组的个数为:(i-idex-1)((i-idex-1)+1)/2;

下面是AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long using namespace std;
const int MX = 1e5+;
ll a[MX]; int main()
{
ll ans = ;
ll n, d;
scanf("%lld%lld", &n, &d);
for(ll i = ; i < n; ++i)
{
scanf("%lld", &a[i]);
ll mn = a[i]-d;
ll idx = lower_bound(a, a+i, mn)-a;
ans += (i-idx-)*((i-idx-)+)/;
}
printf("%lld", ans);
}

如有疑问,欢迎评论指出!

二分三元组 CodeForces - 251A的更多相关文章

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

    Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...

  2. 【二分】Codeforces 706B Interesting drink

    题目链接: http://codeforces.com/problemset/problem/706/B 题目大意: n (1 ≤ n ≤ 100 000)个商店卖一个东西,每个商店的价格Ai,你有m ...

  3. 【贪心+二分】codeforces D. Magazine Ad

    codeforces.com/contest/803/problem/D [题意] 给定一个字符串,字符串里可能有空格和连字符‘-’,空格和连字符的意义是一样的,都表示:能在那个位置把字符串分成两部分 ...

  4. 【贪心+二分】codeforces C. Sagheer and Nubian Market

    http://codeforces.com/contest/812/problem/C [题意] 如何花最少的钱买最多的纪念品?首要满足纪念品尽可能多,纪念品数量一样花钱要最少,输出纪念品数量以及最少 ...

  5. 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

    题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...

  6. Codeforces 1104 D. Game with modulo-交互题-二分-woshizhizhang(Codeforces Round #534 (Div. 2))

    D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string

    题意:交互题:存在一个至少有一个0和一个1的长度为n的二进制串,你可以进行最多15次询问,每次给出一个长度为n的二进制串,系统返回你此串和原串的海明距离(两串不同的位数).最后要你找到任意一个0的位置 ...

  8. 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

  9. 【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale

    当m>=n时,显然答案是n: 若m<n,在第m天之后,每天粮仓减少的量会形成等差数列,只需要二分到底在第几天,粮仓第一次下降到0即可. 若直接解不等式,可能会有误差,需要在答案旁边扫一下. ...

随机推荐

  1. 删除链表倒数第n个节点

    题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链 ...

  2. busybox编译

    sync.c:(.text.sync_main+0x7c): undefined reference to `syncfs'Coreutils—>sync选项去掉 nsenter.c:(.tex ...

  3. Mybatis的原理与JVM内存结构(面试题)

    Mybatis的原理 1.Mapper 接口在初始SQL SessionFactory注册的 2.Mapper 接口注册在名为MapperRegistry类的 HasMap中 key=Mapper c ...

  4. 分布式监控系统开发【day37】:服务端生成配置数据(四)

    一.目录结构 二.引子与代码 1.客户端获取服务列表接口 1.解决了什么问题 客户端要给我获取服务列表的的时候,他肯定要告诉他是谁?他怎么告诉我,客户端必须有一个id号 Saltsack你装一个客户端 ...

  5. h3c mstp的举例

    h3c交换机的图如下: 分别对于SWA,SWB,SWC,SWD,SWE 配置如下: SWA: vlan 10 vlan 20 vlan 30 region-name h3c instance 0 vl ...

  6. MyEclipse编码方式设置

    1.windows -> Preferences -> general -> Workspace:

  7. PL/SQl编程 基本语法

    /*输出hello world*/ DECLARE BEGIN DBMS_OUTPUT.PUT_LINE('Hello World'); END; --set serveroutput on; /** ...

  8. webpack学习笔记——打包后直接访问页面,图片路径错误

    我说的这种图片路径错误是这样的,运行webpack-dev-server,一切正常,没有错误.当webpack之后,直接打开index页面,报错,图片找不到,找不到的原因是路径错误. 先看我的项目代码 ...

  9. python开发遇到的坑(2)mongodb安装路径权限问题

    个人比较懒,Mac 电脑直接使用命令行安装,brew install mongodb,但是遇到两个问题,其一: Error: The following directories are not wri ...

  10. 【转】利用apktool反编译apk,并且重新签名打包

    网站:https://ibotpeaches.github.io/Apktool,下载安装好apktool. 我的安装在 C:\Users\Administrator\Downloads\apktoo ...