zoukankan      html  css  js  c++  java
  • HDU 2578 Dating with girls(1) 二分

    Dating with girls(1)
    Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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 <iostream>
    #include <cstdio>
    #include <math.h>
    #include <cstring>
    #include <algorithm>
    #include <iomanip>
    
    using namespace std;
    
    int a[100005];
    
    int main()
    {
        //freopen("input.txt","r",stdin);
        int t;
        int n;
        int k;
        scanf("%d",&t);
        while(t--)
        {
            int cou=0;
            scanf("%d%d",&n,&k);
            for(int i=1;i<=n;i++)
                scanf("%d",&a[i]);
            a[0]=-99999;
            sort(a,a+1+n);
            for(int i=1;i<=n;i++)
            {
                if(a[i]==a[i-1])
                    continue;
                int l=1;
                int r=n;
                int mid;
                while(l<=r)
                {
                    mid=(l+r)/2;
                    if(a[mid]+a[i]==k)
                    {
                        //cout<<a[i]<<"  kkkk  "<<a[mid]<<endl;
                        cou++;
                        break;
                    }
                    if((a[i]+a[mid])<=k)
                    {
                        l=mid+1;
                    }
                    else
                    {
                        r=mid-1;
                    }
    
                }
            }
            cout<<cou<<endl;
        }
    }
    

      

     
     
     
     
  • 相关阅读:
    多线程中sleep方法,简单介绍。
    线程终止的四种方式,interrupt 方法使用的简单介绍。
    线程的生命周期 介绍
    线程池之 newSingleThreadExecutor 介绍
    python 中 *args he **kwargs的区别
    转载:创业者和工作谈的是一场永不分手的虐恋
    给自己一份勇气,勇敢的面对生活
    做一面锃亮的镜子吧
    与人交往时关注内容而不是表情
    最近比较需要正能量:经典励志人生感悟的句子
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5709509.html
Copyright © 2011-2022 走看看