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 ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- HTML相关知识点(2)
CSS: 字体: 网页默认字体16px; 网站通用字体大小14px 最小是12px,最大无限大 单位换算:1em=16px 选择器:标签选择器:选择页面中所有指定标签,权重为1 通配符选择器:选择所 ...
- 百度统计数据导出demo的坑
1.用户名中文的问题 由于demo文件格式的问题,如果用户名使用中文的话,会出现一下问题 ----------------------preLogin---------------------- [ ...
- Linux centosVMware Vim介绍、vim颜色显示和移动光标、vim一般模式下移动光标、vim一般模式下复制、剪切和粘贴
一.Vim介绍 vim 是一款功能强大的文本编辑器,是vi的升级版,带有颜色显示, 默认有三种模式:一般模式, 命令模式, 编辑模式 安装Vim [root@davery ~]# vim /et ...
- 注册模块上线前安全测试checklist
许多应用系统都有注册模块,正常用户通过注册功能,获得应用系统使用权限:而非法用户通过注册模块,则是为了达到不可告人的目的,非法用户可以通过注册模块与服务端进行交互(一切用户输入都不可信),因此系统上线 ...
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- Maven 使用Nexus搭建Maven私服
Maven学习 (四) 使用Nexus搭建Maven私服 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找 ...
- 三 PrePareStatement注入,DELETE和TRUNCATE
PreparedStatement PreparedStatement是一个接口,它继承了Statement,该接口有以下几个优点: 性能比Statement高,会把sql预编译 可以解决sql注入问 ...
- NodeJs koa2实现文件上传
[转载自:]https://www.jianshu.com/p/34d0e1a5ac70 知识讲解 koa2框架是一个基于中间件的框架,也就是说,需要使用到的功能,比如路由(koa-router),日 ...
- Windows驱动开发-r3和r0通信
用户部分代码: int main() { HANDLE hDevice = CreateFile(L, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ...