HDU 2578 Dating with girls(1)
2015-07-20 14:13
417 查看
Dating with girls(1)
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
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!
![](http://7xjob4.com1.z0.glb.clouddn.com/4e177c0668aebf83429bc52069e1d84a)
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 。当 xi != xi+1 && yi != yi+1 即为一组。也就是k = 4时,1,3和3,1 是两组。
分析
sort+二分,分分钟A掉。本题有坑,例如
n = 5,k = 4
数据为 2 2 2 2 2时
只有一组满足条件 (2,2),其他都是重复的。
注意特殊处理,就能跳坑。
本人当时被这种数据卡了两发WA,简直要哭了Orz...
AC代码如下
#include <cstdio>
#include <algorithm>
#define maxn 100000+2
using namespace std;
int a[maxn];
int main()
{
int t,m,n,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i = 0 ; i < n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int ans = 0;
for(i = 0 ; i < n ; i++)
{
while(a[i] == a[i+1]) i++;
int pos = lower_bound(a,a+n,m-a[i]) - a;<span style="white-space:pre"> </span>//lower_bound() 比手写二分简单很多,至少我这种懒人很喜欢,不会的朋友请自行baidu
if(a[i] + a[pos] == m) ans++;
}
printf("%d\n",ans);
}
return 0;
}
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
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 。当 xi != xi+1 && yi != yi+1 即为一组。也就是k = 4时,1,3和3,1 是两组。
分析
sort+二分,分分钟A掉。本题有坑,例如
n = 5,k = 4
数据为 2 2 2 2 2时
只有一组满足条件 (2,2),其他都是重复的。
注意特殊处理,就能跳坑。
本人当时被这种数据卡了两发WA,简直要哭了Orz...
AC代码如下
#include <cstdio>
#include <algorithm>
#define maxn 100000+2
using namespace std;
int a[maxn];
int main()
{
int t,m,n,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i = 0 ; i < n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int ans = 0;
for(i = 0 ; i < n ; i++)
{
while(a[i] == a[i+1]) i++;
int pos = lower_bound(a,a+n,m-a[i]) - a;<span style="white-space:pre"> </span>//lower_bound() 比手写二分简单很多,至少我这种懒人很喜欢,不会的朋友请自行baidu
if(a[i] + a[pos] == m) ans++;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解