Dating with girls(1)

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

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
 
要去除重复的元素和大于k的元素,因为大于k的元素是肯定组不成等于k的方程的,不能确定数据里是否有0,所以等于k的元素也保留着,每存一个数都hash一下,然后一遍for循环,看 hash[ k-a[i] ]是否为1,是的话cnt++;
 #include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 123456
#define M 1234 int n,k;
int a[N];
int main()
{
int t;cin>>t;
while(t--)
{
scanf("%d%d",&n,&k);
int ha[N]={};
int j=,c;
for(int i=;i<n;i++)
{
scanf("%d",&c);
if(c>k || ha[c])continue;
a[j++]=c;
ha[c]=;
}
int cnt=;
for(int i=;i<j;i++)
if(ha[ k-a[i] ])
cnt++; cout<<cnt<<endl;
}
return ;
}

最新文章

  1. IIS发布问题汇总
  2. [No000033]码农网-如何锻炼出最牛程序员的编码套路
  3. JD-GUI反编译后代码逻辑分析
  4. BZOJ 3107 二进制a+b
  5. Nginx源码研究三:Epoll在NGINX中的使用
  6. Java 学习笔记 (五) Java Compile\Build\Make的区别
  7. Python基础之自定义工具类
  8. 百万级开源MQTT消息服务器 搭建
  9. redis安装(单节点)
  10. 初识HTTP协议web开发
  11. java知识点3
  12. NO.5 &#160; 算法测试(词条统计)
  13. hyperledger-fabric/qemu/kvm/virtual-manager -------vagrant-virtual-box
  14. English trip M1 - AC6 How to make salad? Teacher:Patrick
  15. vim 删除
  16. LVS(Linus Virtual Server):三种负载均衡方式比较+另三种负载均衡方式
  17. 前端不容错过的jQuery图片滑块插件
  18. EditPlus正则表达式中英文详解(附常用事例操作)
  19. php is_callable()与method_exists()函数
  20. 一个Vue实例-添加、显示列表、删除

热门文章

  1. magic mouse 2 在Mac上灵敏度太低的解决办法
  2. CentOS6配置静态IP
  3. web.xml不同的头文件
  4. 查询UNDO使用情况
  5. iossharesdk微信登录出错
  6. Hadoop 3.1.0 在 Ubuntu 16.04 上的安装过程
  7. 使用腾讯互动直播 遇到的坑 &#39;GLIBC_2.14&#39; not found 问题解决
  8. chef cookbook 实战
  9. angular中关于ng-repeat的性能问题
  10. android:logo