Dating with girls(1)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2745 Accepted Submission(s): 865
Problem Description
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!
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!
![](https://i-blog.csdnimg.cn/blog_migrate/4e177c0668aebf83429bc52069e1d84a.png)
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.
Output
For each cases,output the numbers of solutions to the equation.
Sample Input
2 5 4 1 2 3 4 5 8 8 1 4 5 7 8 9 2 6
Sample Output
3 5
Source
/*这道题目是简单的二分应用,原来二分还有搜索的作用啊
注意1+2=3和2+1=3是不同的两种
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int a[100005];
int n,k;
int findroot(int x)
{
int left=1,right=n;
while(left<=right)
{
int mid=(left+right)/2;
if(x==a[mid])
return 1;
else
{
if(x>a[mid])
left=mid+1;
else
right=mid-1;
}
}
return 0;
}
int main()
{
int T;
int cnt;
scanf("%d",&T);
while(T--)
{
cnt=0;
scanf("%d %d",&n,&k);
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
if(i>=2)
{
if(a[i-1]==a[i])
continue;
}
if(findroot(k-a[i]))
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}