HDU-2578 Dating with girls(1) (map/set/折半法)

Dating with girls(1)

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


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


题意:给T组数据;

   输入n,k;

   输入n个数,在这n个数中找到两个数相加等于k的一共有几组?

注意:1.a+b与b+a,算两组;

   2.连续两个相等的数字如n个数为(1,1,3,2,6),k=4;答案是3种;两个1只能看一个;

           3.若用折半法提前排序;

代码:

折半法

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int n,k;
int a[100010];

int findx(int t)
{
    int low=0,high=n-1,mid;
    while(low<high)
    {
        mid = low+ (high-low)/2;
        if(a[mid]==t)
            return mid;
        else if(a[mid]>t)
            high=mid;
        else
            low=mid+1;
    }
    return -1;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&k);
        int i,sum=0;


        for(i = 0 ; i < n ; i ++)
            scanf("%d",&a[i]);
        sort(a,a+n);
            
        for( i = 0 ; i < n ; i ++)
        {
            if(a[i]==a[i+1])// 
                continue;
            else if(findx(k-a[i])!=-1)
                sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}

map

#include<stdio.h>
#include<math.h>
#include<map>
using namespace std;
int main()
{
map<int,int>m;
int n,k,t;
scanf("%d",&t);
while(t--)
{
m.clear();
scanf("%d%d",&n,&k);
int i,j,z,sum=0,x,s=0,l;
for(i=0;i<n;i++)
{
scanf("%d",&z);
m[z]=1;
} 
map<int,int>::iterator it;
    for(it=m.begin();it!=m.end();it++)
{
x=k-(*it).first;
if(m.find(x)!=m.end())
sum++;
}
printf("%d\n",sum);
}
return 0;
}

set

#include<stdio.h>
#include<math.h>
#include<set>
using namespace std;
int main()
{
int n,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
set<int>ss;
int i,j,z,sum=0,x;
for(i=0;i<n;i++)
{
scanf("%d",&z);
ss.insert(z);
} 
set<int>::iterator it;
    for(it=ss.begin();it!=ss.end();it++)
{
x=k-*it;
if(ss.find(x)!=ss.end())
sum++;
}
printf("%d\n",sum);
}
return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值