hdu 2578 Dating with girls(1) (hash)

Dating with girls(1)

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


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
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2575 2579 2573 2576 2574 
 
 1 //1968MS    2000K    551 B    G++    
 2 //1421MS    1932K    551 B    C++
 3 /*
 4 
 5     题意:
 6         给你n个数,和一个数k,问n个数中有多少d对不同的x、y使 
 7             x+y=k    成立
 8         x可以等于y
 9     
10     hash:
11         直接使用C++的map容器做映射。
12         注意一点就是要排序,避免出现相同的x、y 
13 
14 */
15 #include<iostream>
16 #include<algorithm>
17 #include<map>
18 using namespace std;
19 int main(void)
20 {
21     int t,n,k,a[100005];
22     scanf("%d",&t);
23     while(t--)
24     {
25         map<int,int>M;
26         M.clear();
27         scanf("%d%d",&n,&k);
28         for(int i=0;i<n;i++){
29             scanf("%d",&a[i]);
30             if(M[a[i]]==0) M[a[i]]=1;
31         }
32         sort(a,a+n);
33         int ans=0;
34         a[n]=-1;
35         for(int i=0;i<n;i++)
36             if(M[k-a[i]]==1 && a[i]!=a[i+1]) ans++;
37         printf("%d
",ans);
38     }
39     return 0;
40 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3442615.html