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 ...
随机推荐
- 视图家族 & 路由组件
目录 视图家族 & 路由组件 视图集与路由组件 基于 GenericAPIView 的十大接口 基于 generics 包下工具视图类的六大基础接口 视图集 路由组件:必须配合视图集使用 自定 ...
- Mac OS 下升级gcc遇到的坑
为了升级gcc,原来版本是4.8,想升级到4.9,因为在Mac上安装cocoNLP一直出错,想试试看是不是gcc版本太低的原因,因此在此想要升级到4.9 经历了三个问题: - brew update太 ...
- Linux centos7 sed工具介绍
一.sed上 grep工具功能只能实现查找,不能把查找的内容替换. sed本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行查找.删除.替换字符或字符串.调换字符串位置.直接修改文件内容等 ...
- IOS导航器 + 表控制器 常用功能函数/属性
1. 设置标题栏(顶部)颜色 在表控制器中 e.g -(void)viewDidLoad中添加 self.navigationController.navigationBar.barTintColor ...
- 可旋转Treap(树堆)总结
树堆,在数据结构中也称Treap,是指有一个随机附加域满足堆的性质的二叉搜索树,其结构相当于以随机数据插入的二叉搜索树.其基本操作的期望时间复杂度为O(logn).相对于其他的平衡二叉搜索树,Trea ...
- [DllImport("kernel32.dll")]
这叫引入kernel32.dll这个动态连接库. 这个动态连接库里面包含了很多WindowsAPI函数,如果你想使用这面的函数,就需要这么引入.举个例子: [DllImport("kerne ...
- POJ 3436:ACM Computer Factory 网络流
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6247 Accepted: 2 ...
- Python测试进阶——(4)Python程序监控、存储、分析并可视化CPU和内存利用率
monitor190617.py 监控cpu和内存利用率信息,组织成json格式,并写入到 record.txt 文件中: import psutil import time import json ...
- BFC的基础理解及应用场景
最近学习了BFC,开始学习的时候,单纯看概念,有种云里雾里的感觉,字都认识,凑一起啥意思大致也知道,但是具体有什么用呢? 这个就有点迷迷糊糊的,经过老师的讲解,以及自己课后的代码实验与总结,就拨云见日 ...
- 看完本文,Essay写作再也不需要凑字数
很多同学都说过自己写论文的时候出现“词穷”的情况,无奈只能靠“胡编乱造”来凑字数写出开头段,这其实是大家的阅读量没有达到要求.但不能因为出现这种情况就对自己的论文不负责任,否则你的论文分数可能就不会对 ...