Dating with girls(1)

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

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
 
题意:在给定的 n 个数中,满足x + y = k 的x,y有几组(1,3和3,1被认为是不同的两组)
 
题解:
1、二分:用二分在数组a[i]中查找,判断k-a[i]是否存在,若a[i]>k或  a[i]==a[i-1]  (重复),就continue;否则存在就cnt++
 
2、map标记,一开始我是想到开一个vis数组标记,但是考虑到数据范围太大,就放弃了。
 
1、二分
#include<iostream>
#include<algorithm>
#include<math.h>
#define ll long long
using namespace std;
ll a[];
int find1(ll target, ll l,ll r)//l,r是查找的左右区间
{
ll left = l, right = r, mid;
while (left <= right)
{
mid = left + (right - left) / ;
if (a[mid] == target)
return mid;
else if (a[mid] > target)
right = mid - ;
else
left = mid + ;
}
return -;
}
int main()
{
ll t,n,k,cnt;
scanf("%lld",&t);
while(t--)
{
cnt=;
scanf("%lld%lld",&n,&k);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
sort(a,a+n);
for(int i=;i<n;i++)
{
if(a[i]>k||a[i]==a[i-])
continue;
else
{
if(find1(k-a[i],,n)!=-)
cnt++;
}
}
printf("%lld\n",cnt);
} }

2、map

#include<iostream>
#include<map>
#define ll long long
using namespace std;
map<ll,ll>m;
ll a[];
int main()
{
ll t,n,k,cnt;
scanf("%lld",&t);
while(t--)
{
m.clear(),cnt=;
scanf("%lld%lld",&n,&k);
for(int i=;i<n;i++)
{
scanf("%lld",&a[i]);
if(!m[a[i]])
m[a[i]]=;
else
{
i--;//删除重复的数
n--;
}
}
for(int i=;i<n;i++)
{
if(a[i]>k)
continue;
if(m[k-a[i]]==)
cnt++;
}
printf("%lld\n",cnt);
}
return ; }
 

hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组的更多相关文章

  1. HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)

    HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律  对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...

  2. hdu 2578 Dating with girls(1)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the ...

  3. hdu 2578 Dating with girls(1) (hash)

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU 2578 Dating with girls(1) [补7-26]

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. hdu 2579 Dating with girls(2)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...

  6. hdu 2579 Dating with girls(2) (bfs)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. Dating with girls(1)(二分+map+set)

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. 二分-B - Dating with girls(1)

    B - Dating with girls(1) Everyone in the HDU knows that the number of boys is larger than the number ...

  9. hdoj 2579 Dating with girls(2)【三重数组标记去重】

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. Kafka源码系列之源码分析zookeeper在kafka的作用

    浪尖的kafka源码系列以kafka0.8.2.2源码为例给大家进行讲解的.纯属个人爱好,希望大家对不足之处批评指正. 一,zookeeper在分布式集群的作用 1,数据发布与订阅(配置中心) 发布与 ...

  2. Python 数据的输入

    一.单个输入 a=input("输入提示语句")#默认a的类型是字符串 b=input() 二.一行输入两个/三个数据,数据之间用空格间隔开 #a,b的数据类型都是整数 a,b=m ...

  3. C++ Primer Plus 6 笔记(3)

    第5章 1.cout在显示bool值之前将它们转换为int,但cout.setf(ios:: boolalpha)函数调用设置了一个标记,该标记命令cout显示true和false,而不是1和0 2. ...

  4. Vue简介-MVVM是什么?

    Vue.js - Day1 课程介绍 前5天: 都在学习Vue基本的语法和概念:打包工具 Webpack , Gulp 后5天: 以项目驱动教学: 什么是Vue.js Vue.js 是目前最火的一个前 ...

  5. 本地连接 HDFS 报错 Exception in thread "main" org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: Permission denied: user=JM.H,access=WRITE, inode="":r

    此时 到hdfs机器下修改hdfs-site.xml即可 添加如下配置 <property> <name>dfs.permissions</name> <va ...

  6. Centos7 静默安装 Oracle11G

    1.准备安装包: 安装包官网下载地址:https://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-l ...

  7. 将OB86的故障信息保存在DB86中去

    出现DP站故障的时候,CPU会自动调用OB86 ,OB86 的20B 局部变量里面有丰富的故障信息,生成数据块DB86 在DB86 中生成5个双字元素的数组ARAY 在OB86中调用 "BL ...

  8. 洛谷 P1929 迷之阶梯

    题目传送门 解题思路: f[i]表示跳到第i层的最少移动次数,如果可以从下面一级跳上来,那么直接跳上来,如果跳不上来,那就往后退,退到不能退或能跳上第i层 AC代码: #include<iost ...

  9. python matplotlib绘图/sklearn包--make_blobs()

    1.make_bolbs() 函数 from sklearn.datasets.samples_generator import make_blobs import numpy as np impor ...

  10. java#lambda相关之方法引用

    lambda在java中通常是()->{}这样的方式,来书写的.通常的lambda是四大函数型接口的一个“实现”. 如果我们要写的lambda已经有现成的实现了,那么就可以把现成的实现拿过来使用 ...