HDU 5178:pairs(二分,lower_bound和upper_bound)
pairs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4157 Accepted Submission(s): 1481
Problem Description
John has n points on the X axis, and their coordinates are (x[i],0),(i=0,1,2,…,n−1). He wants to know how many pairs<a,b> that |x[b]−x[a]|≤k.(a<b)
Input
The first line contains a single integer T (about 5), indicating the number of cases.
Each test case begins with two integers n,k(1≤n≤100000,1≤k≤109).
Next n lines contain an integer x[i](−109≤x[i]≤109), means the X coordinates.
Output
For each case, output an integer means how many pairs<a,b> that |x[b]−x[a]|≤k.
Sample Input
2
5 5
-100
0
100
101
102
5 300
-100
0
100
101
102
Sample Output
3
10
题意
有x个数,求出这x个数中有多少对数相减的绝对值小于等于k
思路
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int a[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int t;
int n,k;
cin>>t;
while(t--)
{
cin>>n>>k;
for (int i = 1; i <=n; ++i)
{
cin>>a[i];
}
sort(a+1,a+n+1);
ll ans=0;
for(int i=1;i<=n;i++)
{
int l=lower_bound(a+1+i,a+1+n,a[i]-k)-a-1;
int r=upper_bound(a+i+1,a+1+n,a[i]+k)-a-1;
ans+=r-l;
}
cout<<ans<<endl;
}
return 0;
}
HDU 5178:pairs(二分,lower_bound和upper_bound)的更多相关文章
- HDU 5178 pairs(双指针)
HDU 5178 pairs(双指针) Hdu 5178 解法:因为要求的是绝对值小于等于k,因此数字的序号关系并不重要,那么排序后使用双指针即可解决这个问题. #include<queue&g ...
- HDU 5178 pairs —— 思维 + 二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others) ...
- LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- HDU 5178 pairs【二分】||【尺取】
<题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...
- hdu 5178 pairs(BC第一题,,方法不止一种,,我用lower_bound那种。。。)
题意: X坐标上有n个数.JOHN想知道有多少对数满足:x[a]-x[b]<=k(题意给)[a<b] 思路: 额,,,直接看代码吧,,,, 代码: int T,n,k; int x[100 ...
- 二分lower_bound()与upper_bound()的运用
<span style="color:#6633ff;">/* G - 二分 Time Limit:2000MS Memory Limit:32768KB 64bit ...
- hdu 5178 pairs
pairs 问题描述 John 在X轴上拥有nn个点,他们的坐标分别为$(x[i],0),(i=0,1,2,…,n-1)$. 他想知道有多少对< a,b ><a,b>满足|x[ ...
- hdu 5178 pairs (线性探查问题)
Problem Description John has n points on the X axis, and their coordinates are (x[i],),(i=,,,…,n−). ...
- hdu 5178(二分-lower_bound,upper_bound)
pairs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 二分搜素——(lower_bound and upper_bound)
因为每个人二分的风格不同,所以在学习二分的时候总是被他们的风格搞晕.有的人二分风格是左闭右开也就是[L,R),有的人是左开右闭的(L,R]. 二分的最基本条件是,二分的序列需要有单调性. 下面介绍的时 ...
随机推荐
- HDU 1540 Tunnel Warfare
HDU 1540 思路1: 树状数组+二分 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #d ...
- Codeforces 680D - Bear and Tower of Cubes
680D - Bear and Tower of Cubes 思路:dfs+贪心,设剩余的体积为res,存在a,使得a3 ≤ res,每次取边长为a的立方体或者边长为a-1的立方体(这时体积上限变成a ...
- POJ - 2528 Mayor's posters(dfs+分治)
POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...
- Codeforces 285C - Building Permutation
285C - Building Permutation 思路:贪心.因为每个数都不同且不超过n,而且长度也为n,所有排列只能为1 2 3 ......n.所以排好序后与对应元素的差值的绝对值加起来就是 ...
- Java 常用对象-System类
2017-11-02 21:41:06 System类:System 类包含一些有用的类字段和方法.它不能被实例化. *常用方法 public static void gc() 运行垃圾回收器. 调用 ...
- 测序中Q20 Q30 Q40
你能给别人讲清楚这个概念吗? 二代测序中,每测一个碱基会给出一个相应的质量值,这个质量值是衡量测序准确度的.碱基的质量值13,错误率为5%,20的错误率为1%,30的错误率为0.1%.行业中Q20与Q ...
- WPF PasswordBox.Password 的数据绑定
WPF的PasswordBox控件的Password属性不是依赖属性,无法直接进行数据绑定,为使其在MVVM模式中正常使用,可以为PasswordBox增加一个助手类,代码如下: 注:代码摘自:htt ...
- [Leetcode] Unique binary search trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 标准库中 vector list等排序
1.list自带有排序函数sort():可以定义自己的排序规则,如: struct stTest { int count; wstring str; }; bool SortByNum(const s ...
- learning shell built-in variables (1)
Shell built-in variables [Purpose] Learning shell built-in variables, example $0,$1,$2,$3,$#, ...