Dating with girls(1)

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3954    Accepted Submission(s): 1228

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

题解:取两个数使得x+y=k;因为就两个数,所以用二分,set,map均可,若取多个的和是k就要考虑动态规划了,前面做过,上代码:

二分:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=;
int m[MAXN],k;
bool erfen(int l,int r,int x){
int mid;
while(l<=r){
mid=(l+r)>>;
if(x+m[mid]==k){
// printf("%d %d\n",x,m[mid]);
return true;
}
if(x+m[mid]>=k)r=mid-;
else l=mid+;
}
return false;
}
int main(){
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)scanf("%d",m+i);
m[]=-INF;
sort(m,m+n+);
int cnt=;
for(int i=;i<=n;i++){
if(m[i]>k||m[i]==m[i-])continue;
if(erfen(,n,m[i]))cnt++;
}
printf("%d\n",cnt);
}
return ;
}

map:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
const int INF=0x3f3f3f3f;
using namespace std;
const int MAXN=;
map<int,int>mp;
int m[MAXN];
int main(){
int T,n,k;
scanf("%d",&T);
while(T--){
mp.clear();
scanf("%d%d",&n,&k);
m[]=-INF;
for(int i=;i<=n;i++){
scanf("%d",m+i);
if(!mp[m[i]])
mp[m[i]]=;
else i--,n--;
// cout<<m[i]<<mp[m[i]]<<endl;
}
int cnt=;
for(int i=;i<=n;i++){
if(m[i]>k)continue;
if(mp[k-m[i]])cnt++;
}
printf("%d\n",cnt);
}
return ;
}

set:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
const int INF=0x3f3f3f3f;
using namespace std;
const int MAXN=;
set<int>st;
int m[MAXN];
int main(){
int T,n,k,temp;
scanf("%d",&T);
while(T--){
st.clear();
scanf("%d%d",&n,&k);
for(int i=;i<n;i++){
scanf("%d",&temp);
st.insert(temp);
}
set<int>::iterator iter;
int cnt=;
for(iter=st.begin();iter!=st.end();iter++)
if(st.count(k-*iter))cnt++;
printf("%d\n",cnt);
}
return ;
}

Dating with girls(1)(二分+map+set)的更多相关文章

  1. 二分-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 ...

  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) 满足条件x+y=k的x,y有几组

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. hdu 2579 Dating with girls(2)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...

  5. hdoj 2579 Dating with girls(2)【三重数组标记去重】

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  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. 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 ...

  8. 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+ ...

  9. POJ 1840 Eqs 二分+map/hash

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

随机推荐

  1. JavaSE复习日记 : 实例化对象/构造方法和this关键字

    /* * 实例化对象/对象的构造方法/this关键字 */ /* * 实例化对象 * * 就是实例化某一个类; * 从不同角度去理解的话就是: * 1. 从人的认知角度: * 就是具体化某个东西; * ...

  2. 模拟post请求(PHP)

    <?php //=========================模拟post请求==================================== // ================ ...

  3. github/python/ show me the code 25题(二)

    第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示: { "1":["张三",150,120,100], &q ...

  4. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  5. poj 1206

    /** 题意: 给定一序列,置换k次之后 输出其状态 置换: 考察循环节长度, 思路: 分别求出每个元素的循环节的大小,用k 模其大小,大的k次之后的位置, 输出即可 **/ #include < ...

  6. Fedora 19下Guacamole的安装使用

    由于我要使用RDP实现web远程桌面,因此需要用到了Guacamole这个开源的软件.之前用Ubuntu12.04折腾了一晚上,也没有找到依赖库文件,而Guacamole的官方安装说明却没有介绍这个依 ...

  7. linux 6.4平台利用rman迁移oracle 11g r2数据库

    测试环境分别在虚拟机安装A,B主机 系统:linux 6.4, 数据库:oracle 11g r2 A主机:安装oracle 11g r2数据库 B主机:只安装oracle 11g r2软件 第一步, ...

  8. Qt窗口屏幕居中显示(有专门的QDesktopWidget,先计算后显示)

    窗口的屏幕居中显示问题,在各开发工具中原理相同,首先使用特定的方法得到显示屏幕的宽度和高度,再根据窗口本身的宽度和高度计算出窗口的左上角坐标位置. Qt中可以采用两种方法达到窗口的屏幕居中显示: 方法 ...

  9. zookeeper 伪集群模式

    问题二:开发没有足够机器,一台机子上是否装三个zookeeper服务器集群. 问题解答: 这种安装模式只能说是一种伪集群模式.三个zookeeper服务器都安装在同一个服务器(platform)上,需 ...

  10. m文件转换为C/C++文件的编译、绘图、参数、打包问题总结

    在工程计算相关项目中,常常利用Matlab来完成计算.算法.绘图等功能.使用Matlab来完成这些功能非常简单,Matlab提供的m编程语言功能强大,代码量少.为了在自己的C/C++项目中加入这些功能 ...