杭电2578

Dating with girls(1)

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1668    Accepted Submission(s): 545


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!

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
 
/*先排序,再二分*/
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b)
{
	return (*(int *)a)-(*(int *)b);
}
int main()
{
	int n,k,t;
	int i,j;
	int s[100005],d[100005],cnt;
	int low,hig,mid;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		for(i=0;i<n;i++)
			scanf("%d",&d[i]);
		qsort(d,n,sizeof(d[0]),cmp);
		for(i=0,j=0;i<n;i++)
			if(d[i]!=d[i+1])
				s[j++]=d[i];
		for(i=0,cnt=0,n=j;i<n;i++)
		{
		     low=0;hig=n-1;
			 while(low<=hig)
			 {
				 mid=(hig+low)/2;
				 if(s[mid]+s[i]==k)
				 {
					 cnt++;
					 break;
				 }
				 else if(s[mid]+s[i]>k)
					 hig=mid-1;
				 else
					 low=mid+1;
			 }
		}

		printf("%d\n",cnt);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值