hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组
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
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!

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.
#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有几组的更多相关文章
- 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+ ...
- hdu 2578 Dating with girls(1)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the ...
- 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 ...
- 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 ...
- hdu 2579 Dating with girls(2)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...
- 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 ...
- Dating with girls(1)(二分+map+set)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 二分-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 ...
- hdoj 2579 Dating with girls(2)【三重数组标记去重】
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- ZCGL大数据平台性能优化
对HBase数据库建索引 参见:基于Phoenix对HBase建索引 https://www.cnblogs.com/ratels/p/11203313.html 将HBase访问微服务整合到模块 ...
- 第2节 storm实时看板案例:11、实时看板综合案例工程构建,redis的专业术语
redis当中的一些专业术语: redis缓存击穿 redis缓存雪崩 redis的缓存淘汰 =========================================== 详见代码
- Vue - 引入本地图片的两种方式
第一种,只引入单个图片,这种引入方法在异步中引入则会报错. 比如需要遍历出很多图片展示时 <image :src = require('图片的路径') /> 第二种,可引入多个图片,也可引 ...
- linux环境下查看tomcat日志
1.先切换到:cd usr/local/tomcat5/logs 2.tail -f catalina.out 3.这样运行时就可以实时查看运行日志 Ctrl+c 是退出tail命令. alt+E+R ...
- Cookie跨域setDomain和setPath
CSDN日报20170226--<你离心想事成只差一个计划> 程序员2月书讯 [招募]Python学习班招生啦 Cookie跨域setDomain和setPath 标签: cookiesp ...
- Mac如何升级自带的vim
brew install vim --with-lua --with-override-system-vi brew install macvim --with-lua --with-override ...
- [AHK]输入法状态提示,中文状态提示“中”,英文状态提示“EN”[转]
测试中发现使用小鹤双拼,如果不是使用shift切换中英文,效果不理想,留下此文章做个标记 https://www.autoahk.com/archives/8755 输入法状态提示,中文状态提示&qu ...
- extract()和extact_first()的区别
extract()和extact_first()都是提取Selector的data部分.但现在你要先知道什么是Selector. Selector是选择器的意思.具体定义我不知道,但通过下面的例子,你 ...
- YUV图解 (YUV444, YUV422, YUV420, YV12, NV12, NV21)
背景: 最近在研究音视频,了解YUV这样的格式对于音视频开发比较重要. 虽然这篇文章大部分是转载别人的,但是经过了校对以后,重新排版并补充了一部分内容 概览: 之所以提出yuv格式的原因,是为了解 ...
- 2-10 就业课(2.0)-oozie:12、cm环境搭建的基础环境准备
8.clouderaManager5.14.0环境安装搭建 Cloudera Manager是cloudera公司提供的一种大数据的解决方案,可以通过ClouderaManager管理界面来对我们的集 ...