二分-B - Dating with girls(1)
Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with the boys with higher IQ. In order to test the boys ' IQ, The girls make a problem, and the boys who can solve the problem
correctly and cost less time can date with them.
The problem is that : give you n positive integers and an integer k. You need to calculate how many different solutions the equation x + y = k has . x and y must be among the given n integers. Two solutions are different if x0 != x1 or y0 != y1.
Now smart Acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!Input
The first line contain an integer T. Then T cases followed. Each case begins with two integers n(2 <= n <= 100000) , k(0 <= k < 2^31). And then the next line contain n integers.
Outpu
tFor each cases,output the numbers of solutions to the equation.
#include<iostream>
#include<algorithm>
using namespace std; int main(){
int T;
scanf("%d",&T);
while(T--){
int n,k,a[],cnt=;
scanf("%d %d", &n, &k);
for(int i=;i<n;i++)
scanf("%d", a+i);
sort(a,a+n);
for(int i=; i<n; i++){
int left = , right= n-;
while(right > left){
int mid = (left + right)/;
if(a[i] + a[mid] > k) right = mid-;
else if(a[i] + a[mid] < k) left = mid+;
else{
left = mid;
break;
}
}
if(a[i]+a[left]==k){
cnt++;
if(i!=&&a[i]==a[i-]) cnt--;
}
}
printf("%d\n",cnt);
}
}
二分-B - Dating with girls(1)的更多相关文章
- Dating with girls(1)(二分+map+set)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 2578 Dating with girls(1)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the ...
- HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)
HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...
- hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 2579 Dating with girls(2)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...
- hdoj 2579 Dating with girls(2)【三重数组标记去重】
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 2579 Dating with girls(2) (bfs)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 2578 Dating with girls(1) (hash)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2578 Dating with girls(1) [补7-26]
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- ADO.NET事务封装
在数据库工具类编写的过程中,对事务的处理操作想避免各个原子操作的事务对象赋值重复操作,想对外暴露的方法为如下形式 public bool ExecuteTransition(Action Transi ...
- js有关字符串拼接问题
我们经常写代码要遇见要拼接字符串,比如说我们要把 "yyy" 和一个动态数字拼接,接下来我们怎么办? 其实我们都会想到直接用“yyy” + 一个数字不就可以了吗? 对的, ...
- 源码编译安装python2/3版本
1- 源码编译安装python2.7版本 1.1- 下载源码包 wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz 1.2- ...
- postman请求(请求方式、请求url、请求参数、参数类型、请求头)
请求方式:get 请求地址: 请求参数:url与参数用?间隔,多个参数用&间隔 请求方式:post 请求地址: 请求参数: 请求参数格式:前面两种是key-value.第三种可以选择json/ ...
- Android开发第一天---AndroidStudio的安装和第一个安卓开发
今天已经是开始学习Android的第二天,我居然才把AndroidStudio开发环境安装并配置好,我只能说“我太难了”,下了好几个版本,终于找到了一个合适的,得出一个结论外国的东西是真的不太好用啊, ...
- springBoot 与 springMVC的区别
springMVC 1.是基于Servlet的一个MVC框架(DispatcherServlet.ModelAndView.ViewResolver),解决web开发问题 缺点是 :配置复杂 要简化使 ...
- P1525 关押罪犯【二分+二分图】
输入输出样例 输入 #1 复制 4 6 1 4 2534 2 3 3512 1 2 28351 1 3 6618 2 4 1805 3 4 12884 思路 对于要求最大值最小的问题,不难想到二分. ...
- OpenCV基本绘图函数
线段:line 函数 CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& co ...
- array every
every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试.它返回一个布尔值.
- openlayers显示比例尺
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...