HDU 5178 pairs —— 思维 + 二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178
pairs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3090 Accepted Submission(s): 1085
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)
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.
5 5
-100
0
100
101
102
5 300
-100
0
100
101
102
10
题解:
两个要求:1:b>a 2.abs(x[b]-x[a])<=k。
先对数组进行排序。对于满足上述条件的a、b(ab为排序前的序号)来说。a、b的关系是可以互相转化的:
当abs(x[b]-x[a])<=k 且 b>a时,那这一对a、b固然满足条件。
当abs(x[b]-x[a])<=k 且 a>b时,那a就变成b,b就变成a,所以也满足条件。
综上,只需要找到abs(x[b]-x[a])<=k 的a、b对即可,无所谓ab的大小关系。但是直接这样计算会出现重复,正确的做法是,只取一边,即:x[a]+k或者是 x[a]-k,这样就能避免重复。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define rep(i,s,t) for(int (i)=(s); (i)<=(t); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const double eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e5+; int n, k;
int a[maxn]; int sch(int x)
{
int l = , r = n;
while(l<=r)
{
int mid = (l+r)>>;
if(a[mid]<=x)
l = mid + ;
else
r = mid - ;
}
return r;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
rep(i,,n)
scanf("%d",&a[i]); sort(a+,a++n);
LL ans = ;
rep(i,,n-)
{
int p = sch(a[i]+k);
// int p = upper_bound(a+1,a+1+n,a[i]+k) - (a+1);
ans += p-i;
}
printf("%I64d\n",ans);
}
}
HDU 5178 pairs —— 思维 + 二分的更多相关文章
- HDU 5178 pairs【二分】||【尺取】
<题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...
- HDU 5178 pairs(双指针)
HDU 5178 pairs(双指针) Hdu 5178 解法:因为要求的是绝对值小于等于k,因此数字的序号关系并不重要,那么排序后使用双指针即可解决这个问题. #include<queue&g ...
- 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 pairs(BC第一题,,方法不止一种,,我用lower_bound那种。。。)
题意: X坐标上有n个数.JOHN想知道有多少对数满足:x[a]-x[b]<=k(题意给)[a<b] 思路: 额,,,直接看代码吧,,,, 代码: int T,n,k; int x[100 ...
- HDU 5178:pairs(二分,lower_bound和upper_bound)
pairs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- [2013 ACM/ICPC Asia Regional Nanjing Online C][hdu 4750]Count The Pairs(kruskal + 二分)
http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意: 定义f(u,v)为u到v每条路径上的最大边的最小值..现在有一些询问..问f(u,v)>=t ...
- hdu 5178(二分-lower_bound,upper_bound)
pairs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 2366 Space(二分计数)
Problem Description During a programming contest, teams cannot sit close to each other, because then ...
随机推荐
- Nastya Studies Informatics
Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes in ...
- msp430项目编程37
msp430中项目---usb接口编程37 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- Day 11 正则表达式
正则表达式 一.简介 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配到的行打印出来.grep全称是Globally search for a Regular ...
- nginx的配置文件 【nginx.conf】
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- 《从零开始搭建游戏服务器》Eclipse和Tomcat安装配置
我选择用来进行服务器开发的语言是Java,开发流程更接近于JavaWeb,所以需要先为开发配置一个开发环境,需要配置的主要是Eclipse和Tomcat(Web工程的容器或管理工具). 一.资源下载: ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- ubuntu下安装翻译软件
原文: http://sixipiaoyang.blog.163.com/blog/static/6232358820144146386437/ Ubuntu下常用的翻译软件有StarDict,Gol ...
- NOIP前必须记住的30句话
NOIP前必须记住的30句话 1.比赛前一天晚上请准备好你的各种证件,事先查好去往考场的路线2.比赛之前请先调整你的屏幕分辨率到你喜欢的大小3.比赛之前请把编译器的字体调为你平时惯用的字体,尤其是注意 ...
- ByteArrayInputStream的作用,和BufferedOutputStream 的区别
个人好奇ByteArrayInputStream,到底是有什么用于是百度了一些资料 整合了下,********这两个类对于要创建临时性文件的程序以及网络数据的传输.数据压缩后的传输等可以提高运行的的效 ...
- ps --sort排序功能
ps aux --sort +rss/rss根据内存正序排 ps aux --sort -rss 逆序 ps aux --sort -pid/pid ps aux --sort %cpu/-%cpu ...