Dating with girls(1)(二分+map+set)
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

5 4
1 2 3 4 5
8 8
1 4 5 7 8 9 2 6
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)的更多相关文章
- 二分-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 ...
- 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) 满足条件x+y=k的x,y有几组
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 ...
- hdoj 2579 Dating with girls(2)【三重数组标记去重】
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 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 ...
- 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 3784 继续xxx定律 & HDU 2578 Dating with girls(1)
HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...
- POJ 1840 Eqs 二分+map/hash
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
随机推荐
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- 同步队列-Queue模块解析
Queue模块解决了生产者.消费者问题,在多线程编程中进行线程通信的时候尤其有用,Queue类封装了加锁解锁的过程. 在Queue模块中有三种不同的队列类,区别是不同队列取出数据的顺序 ...
- ActiveMQ的入门demo
步骤: 1 :下载ActiveMQ 官网:http://activemq.apache.org/ 2 :解压AcitveMQ, 根据自己的操作系统选择运行win64或者win32下的activemq. ...
- check the manual that corresponds to your MySQL server version for the right syntax的错误解析
错误原因一:SQL关键字冲突 分析:例:把desc命名为字段名 错误原因二:$right=$DB->fetch_one_array("SELECT rsnumber FROM &quo ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Eclips入门教程
1. 插件推荐 Eclipse默认情况下是一个纯净版的,所以功能简单,而开源IDE最为强大的莫过于各种插件,通过使用插件可以帮助我们减少大量编写代码的工作量,也帮助我们降低了编写代码的难度,所以懂得安 ...
- 利用 Android Studio 和 Gradle 打包多版本APK
在项目开发过程中,经常会有需要打包不同版本的 APK 的需求. 比如 debug版,release版,dev版等等. 有时候不同的版本中使用到的不同的服务端api域名也不相同. 比如 debug_ap ...
- jdk、jre与jvm的区别与联系
为了学习数据库,重装了系统,之前前一直在用eclipse,现在准备换成myeclipse,这之前当然需要重新设置环境变量,顺手写下有关jdk.jre与jvm的区别与联系以供交流参考. 首先来说一下JD ...
- oracle slient静默安装并配置数据库及仅安装数据库不配置数据库shell
<1,>仅安装数据库软件不配置数据库 ./x86oracle.sh /ruiy/ocr/DBSoftware/app/oracle /ruiy/ocr/DBSoftware/app/ora ...
- PHP高手干货分享:要大大提高PHP效率
1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双 引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的”函数”(译注:PHP ...