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. Windows API 常用函数---转载

    Windows API 常用函数 2014-10-15 14:21  xiashengwang  阅读(2105)  评论(0)  编辑  收藏 .Net中虽然类库很强的,但还是有些时候功能有限,掌握 ...

  2. 【剑指Offer面试编程题】题目1504:把数组排成最小的数--九度OJ

    题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 输入: 输 ...

  3. (C#)Image.FromFile 方法会锁住文件的原因及可能的解决方法

    Image.FromFile 一旦使用后,对应的文件在一直调用其生成的Image对象被Disponse前都不会被解除锁定,这就造成了一个问题,就是在这个图形被解锁前无法对图像进行操作(比如删除,修改等 ...

  4. 新闻网大数据实时分析可视化系统项目——13、Cloudera HUE大数据可视化分析

    1.Hue 概述及版本下载 1)概述 Hue是一个开源的Apache Hadoop UI系统,最早是由Cloudera Desktop演化而来,由Cloudera贡献给开源社区,它是基于Python ...

  5. JS数据统计表 highcharts.js的运用

    参考地址 http://www.runoob.com/highcharts/highcharts-column-basic.html 1.下载JS文件引入,或者用CDN function getCou ...

  6. redis api-set

  7. Day2-F-A Knight's Journey POJ-2488

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  8. FPGA流程设计

    做fpga也有四年时间了,该有个总结.刚开始那会,学习东西都是死记硬背,去面试也是直接带着答案去了. 时间久了,才懂得设计一些基本思路. 1. 设计输入: verilog代码和原理图.画原理图都是懒得 ...

  9. WebEx如何录制电脑内的声音

    WebEx如何录制电脑内的声音     事情是这样的,我需要参加一个网络课程,视频讲课的,但是呢,又因为自己白天需要干别的事,就想着怎么把视频录下来晚上回去看,找了WebEx录屏软件,尝试了下,录屏听 ...

  10. 使用loadrunner监控apcahe资源

    一般要修改的内容在Httpd.conf文件中已经存在,如果不存在请自行添加相应内容. (1)修改Apache中Httpd.conf文件, (2)添加ExtendedStatus,设置ExtendedS ...