首页 > 其他 > 详细

HDU 2578 Dating with girls(1).

时间:2014-04-13 06:16:00      阅读:549      评论:0      收藏:0      [点我收藏+]
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!
bubuko.com,布布扣
 

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
 
====
二分啊,一到比赛就忘了。。。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int a[100100];
int n;

bool bin(int x)
{
    int l=0,r=n-1,mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(a[mid]>x)
            r=mid-1;
        else if(a[mid]<x)
            l=mid+1;
        else
            return 1;
    }
    return 0;
}

int main ()
{
    int t,k;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        for(int i=0;i<n;i++)
          scanf("%d",&a[i]);
        sort(a,a+n);
        int s=0;
        if(bin(k-a[0]))         //注意这里的写法。
            s++;
        for(int i=1;i<n;i++)
        {
            if(a[i]==a[i-1])    //注意相同时的情况。
                continue;
            else if(bin(k-a[i]))
                s++;
        }
        cout<<s<<endl;
    }
    return 0;
}


 
 

HDU 2578 Dating with girls(1).,布布扣,bubuko.com

HDU 2578 Dating with girls(1).

原文:http://blog.csdn.net/darwin_/article/details/23539609

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!