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)的更多相关文章

  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 ...

  2. hdu 2578 Dating with girls(1)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the ...

  3. 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+ ...

  4. 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 ...

  5. hdu 2579 Dating with girls(2)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...

  6. hdoj 2579 Dating with girls(2)【三重数组标记去重】

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 6.python设置代理和添加镜像源介绍

    为什么要修改镜像源? 一般使用python安装库,会用到pip install xxx 指令或者conda install xxx指令,因为pip和conda默认国外镜像源,这时会在Python的官方 ...

  2. AGC011-E Increasing Numbers

    题意 给定一个数\(n\),\(n≤10^{500,000}\),问\(n\)最少可以拆分成几个不降数的和.一个不降数是在十进制位下,从高位往低位看,每个数都不会比高位的数更小的数 做法 不降数可以拆 ...

  3. MATLAB应用专题part1-电力电子仿真技术

    士不可以不弘毅,任重而道远.仁以为己任,不亦重乎?死而后已,不亦远乎?  --曾参 C语言系列需要过一段时间才能更新了.这些天学习C语言我感觉有点崩溃了,所以我先开另外一个专题-matlab应用专题. ...

  4. JS 弹出小窗口

    弹出窗口函数 function openwindow(url,name,iWidth,iHeight){ var url; //转向网页的地址; var name; //网页名称,可为空; var i ...

  5. 【编译原理】TEST递归下降演示

    MyUtil.java package Util; import Value_Final.RRule; public class MyUtil { /** * 判断字符串是否是关键字 * @param ...

  6. redis的一些常见面试题

    1 .在项目中缓存是如何使用的? 2.为啥在项目里要用缓存呢? 用缓存,主要用途,高性能和高并发 高性能: 场景举例:假如有这么一个操作,用户发起请求,操作数据库,查出结果,耗时600ms,然后这个结 ...

  7. Java输入和输出数组(加逗号)

    输入示例 61,2,3,4,5,6 输出示例 1,2,3,4,5,61,2,3,4,5,6 import java.util.Scanner; public class Demo01 { public ...

  8. gitlab持续集成,自动部署

    写这篇文章的目的是,实现提交代码到gitlab上的一个项目时,自动打包,并将包发布到另一台服务器上,重启tomat.如有问题,可以联系本人QQ:409838567 gitlab-ci,是基于gitla ...

  9. Deepin Linux折腾输入法

    Deepin Linux 别折腾输入法了 , 很容易把自己整残了 Deepin 自带五笔拼音输入法 , 平时撸码用完全够了 . 但今天突然快捷键不好用了.怎么都切不了英文输入 . 切输入法也不能切到英 ...

  10. 跨站请求伪造(Cross-site request forgery), 简称为 XSRF

    跨站请求伪造(Cross-site request forgery), 简称为 XSRF,是 Web 应用中常见的一个安全问题.前面的链接也详细讲述了 XSRF 攻击的实现方式. 当前防范 XSRF ...