Dating with girls(1)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3944 Accepted Submission(s): 1223
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
Recommend
好浪漫的题,做出来,但是依旧没有女朋友,就是二分查找,附ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
__int64 a[110000],i,j,k,l,m,n,right,left,mid;
int main()
{
scanf("%I64d",&k);
while(k--)
{
int flag=0;
scanf("%I64d%I64d",&m,&n);
for(i=0;i<m;i++)
{
scanf("%I64d",&a[i]);
if(a[i]*2==n)
flag=1;
}
sort(a,a+m);
a[m]=n+1;
__int64 ans=0;
for(i=0;i<m;i++)
{
if(a[i]==a[i+1])
continue;
left=i+1;
right=m-1;
if(a[i]+a[m-1]<n)
continue;
if(a[m-1]+a[m-2]<n)
break;
while(left<=right)//二分查找,少了一个等于,超时了 好多次
{
mid=(left+right)/2;
if(a[i]+a[mid]>n)
right=mid-1;
else
{
if(a[i]+a[mid]==n)
{
ans+=2;
break;
}
if(a[i]+a[mid]<n)
left=mid+1;
}
}
}
printf("%I64d\n",ans+flag);
}
return 0;
}