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. 一周学会Mootools 1.4中文教程:(7)汇总收尾

    转眼之间已经第七课了,这也将成为最后一课,如果这7课下来您感觉水平没有达到预想的水平,没关系您可以继续关注本站的博文,我会陆续发一些类似的文章帮您提升水平,另外我最近打算和群里的几个Mootools爱 ...

  2. 对discuz的代码分析学习(一)目录结构

    主目录 DISCUZ用的是自己的框架,和其他框架应用一样属于单入口应用.主目录下的.php文件,大部分是应用的入口文件. home.php:家园入口,即论坛中类似博客的那个东西.index.php:首 ...

  3. 常见iis错误之一

    1.win7配置iis 出现:HTTP 错误 403.14 - Forbidden 打开 IIS 管理器. 在“功能”视图中,双击“目录浏览”. 在“目录浏览”页上,在“操作”窗格中单击“启用”. 确 ...

  4. Bootstrap禁用响应式布局

    在Bootstrap中极其重要的一个技术内容便是响应式布局了,一次编码针对不同设备终端的强大能力使得响应式技术愈发流行. 不过正所谓"萝卜青菜各有所爱",如果你想要使用Bootst ...

  5. nfs error

    mount -t nfs 10.173.55.154:/oradata /oradatamount: wrong fs type, bad option, bad superblock on 10.1 ...

  6. 字符串的MD5的32位加密和16位加密

    import java.security.MessageDigest; import java.util.Locale; public class MD5Util { public static St ...

  7. 破解win7开机密码,又不取消密码

    Win7开机密码破解方法 1. 进入pe.(可用系统盘,或者u盘启动) 2.进入c:\windows\system32下 更改magnify.exe和cmd.exe的所有者为administrator ...

  8. 张王李相亲应用if else

    package hello; public class to { public static void main(String[]args){ int a =1,b=0; int c =1,d=0; ...

  9. [PHP] PHP初学者想了解"伪静态",必须看这个贴 [复制链接] [推荐]

    一.何为“伪静态”? 以传智播客bbs论坛为例,这篇帖子的链接地址原本应该是“http://bbs.itcast.cn/forum.php?mod=post&action=newthread& ...

  10. 一些tcp通讯代码

    1,nginx-lua 需要设置nginx配置文件 resolver 223.5.5.5 223.6.6.6; lua_package_path "/usr/local/nginx/conf ...