Can you find it?

Time Limit : 10000/3000ms (Java/Other)   Memory Limit : 32768/10000K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 7
Problem Description
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
 
Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
 
Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
 
Sample Input
3 3 3 1 2 3 1 2 3 1 2 3 3 1 4 10
 
Sample Output
Case 1: NO YES NO
题意:分别有a,b,c三个数组,给出一个数,看这个数能不能再a,b,c,里面分别找出一个数求和得到
题解:先把两个数组加起来,另外一个数组用二分;
代码:
 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
__int64 D[],A[],B[],C[],L,N,M;//D数组要开大;;;
int erfen(__int64 *a,__int64 c,__int64 d){
int l,r,mid;
l=;r=L*N-;////////
while(l<=r){
mid=(l+r)/;
if(a[mid]+c<d)l=mid+;
else r=mid-;
if(a[mid]+c==d)return ;//这点弄错了,交了几十遍。。。。。
}
return ;
}
int search(__int64 *a,__int64 *b,__int64 c){
for(int i=;a[i];i++){
if(erfen(b,a[i],c))return ;
}
return ;
}
void merge(__int64 *a,__int64 *b,__int64 *c){
int t=;
for(int i=;a[i];i++){
for(int j=;b[j];j++){
c[t++]=a[i]+b[j];
}
}
}
int main(){
__int64 S,flot=,X;
while(~scanf("%I64d%I64d%I64d",&L,&N,&M)){flot++;
for(int i=;i<L;++i)scanf("%I64d",&A[i]);
for(int i=;i<N;++i)scanf("%I64d",&B[i]);
for(int i=;i<M;++i)scanf("%I64d",&C[i]);
merge(A,B,D);
sort(D,D+L*N);//这里也错了好多次,是相乘
//for(int i=0;i<L+N;i++)printf("%d ",D[i]);
scanf("%I64d",&S);
printf("Case %I64d:\n",flot);
while(S--){
scanf("%I64d",&X);
if(search(C,D,X))puts("YES");
else puts("NO");
}
}
return ;
}

stl:

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
__int64 D[],A[],B[],C[],L,N,M;//D数组要开大;;;
/*int erfen(__int64 *a,__int64 c,__int64 d){
int l,r,mid;
l=0;r=L*N-1;////////
while(l<=r){
mid=(l+r)/2;
if(a[mid]+c<d)l=mid+1;
else r=mid-1;
if(a[mid]+c==d)return 1;//这点弄错了,交了几十遍。。。。。
}
return 0;
}*/
int search(__int64 *a,__int64 *b,__int64 c){
for(int i=;a[i];i++){
if(*lower_bound(b,b+L*N,c-a[i])==c-a[i])return ;/*lower_bound返回的是b数组中第一个大于等于c-a[i]元素的的地址
upper(begin,end,index)找到大于某值的第一次出现;
*/
}
return ;
}
void merge(__int64 *a,__int64 *b,__int64 *c){
int t=;
for(int i=;a[i];i++){
for(int j=;b[j];j++){
c[t++]=a[i]+b[j];
}
}
}
int main(){
__int64 S,flot=,X;
while(~scanf("%I64d%I64d%I64d",&L,&N,&M)){flot++;
for(int i=;i<L;++i)scanf("%I64d",&A[i]);
for(int i=;i<N;++i)scanf("%I64d",&B[i]);
for(int i=;i<M;++i)scanf("%I64d",&C[i]);
merge(A,B,D);
sort(D,D+L*N);//这里也错了好多次,是相乘
//for(int i=0;i<L+N;i++)printf("%d ",D[i]);
scanf("%I64d",&S);
printf("Case %I64d:\n",flot);
while(S--){
scanf("%I64d",&X);
if(search(C,D,X))puts("YES");
else puts("NO");
}
}
return ;
}

map和set做全都MLE了,无奈了,别人的都对的;

set代码:

 #include<stdio.h>
#include<set>
using namespace std;
const int MAXN=;
int A[MAXN],B[MAXN],C[MAXN];
int main(){
int L,N,M,S,X,flot=,ok;
while(~scanf("%d%d%d",&L,&N,&M)){set<int>num;
for(int i=;i<L;++i)scanf("%d",&A[i]);
for(int i=;i<N;++i)scanf("%d",&B[i]);
for(int i=;i<M;++i)scanf("%d",&C[i]);
for(int i=;i<L;i++)for(int j=;j<N;j++)num.insert(A[i]+B[j]);
scanf("%d",&S);
printf("Case %d:\n",flot++);
while(S--){ok=;
scanf("%d",&X);for(int i=;C[i];i++){
if(num.find(X-C[i])!=num.end()){ok=;break;}
}
if(ok)puts("YES");
else puts("NO");
}
}
return ;
}

map代码:

 #include<stdio.h>
#include<map>
using namespace std;
const int MAXN=;
int A[MAXN],B[MAXN],C[MAXN];
int main(){
int L,N,M,S,X,flot=,ok;
while(~scanf("%d%d%d",&L,&N,&M)){map<int,bool>num;
for(int i=;i<L;++i)scanf("%d",&A[i]);
for(int i=;i<N;++i)scanf("%d",&B[i]);
for(int i=;i<M;++i)scanf("%d",&C[i]);
for(int i=;i<L;i++)for(int j=;j<N;j++)num[A[i]+B[j]]=;
scanf("%d",&S);
printf("Case %d:\n",flot++);
while(S--){ok=;
scanf("%d",&X);for(int i=;C[i];i++){
if(num.count(X-C[i])){ok=;break;}
}
if(ok)puts("YES");
else puts("NO");
}
}
return ;
}

Can you find it?(二分 二分+STL set map)的更多相关文章

  1. Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列)

    Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列) Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺 ...

  2. [USACO09DEC]音符Music Notes (二分、STL)

    https://www.luogu.org/problem/P2969 题目描述 FJ is going to teach his cows how to play a song. The song ...

  3. hdu2413(二分+二分匹配)

    题意:人和外星人星球大战,人总共有H个星球,外星人有A个星球,现在人要用飞船去打外星人的飞船,要求每个人类星球只能对战一个外星球,且每个星球都开始有己知的飞船数,不论是人或外星人的星球,并每个星球都有 ...

  4. Hihocoder 1128 二分·二分查找

    二分·二分查找 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Nettle最近在玩<艦これ>,因此Nettle收集了很多很多的船(这里我们假设Nettle氪 ...

  5. hihoCoder 1133 二分·二分查找之k小数(TOP K算法)

    #1133 : 二分·二分查找之k小数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回里我们知道Nettle在玩<艦これ>,Nettle的镇守府有很 ...

  6. hihocoder hiho第38周: 二分·二分答案 (二分搜索算法应用:二分搜索值+bfs判断可行性 )

    题目1 : 二分·二分答案 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回和上上回里我们知道Nettle在玩<艦これ>,Nettle在整理好舰队之后 ...

  7. hiho week 38 P1 : 二分·二分答案

    P1 : 二分·二分答案 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回和上上回里我们知道Nettle在玩&l ...

  8. hiho week 37 P1 : 二分·二分查找之k小数

    P1 : 二分·二分查找之k小数 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回里我们知道Nettle在玩&l ...

  9. [STL] Implement "map", "set"

    练习热身 Ref: STL中map的数据结构 C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Re ...

  10. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

随机推荐

  1. 判断包含字符String.contains

    Java String.contains()方法用法实例教程, 返回true,当且仅当此字符串包含指定的char值序列 java.lang.String.contains() 方法返回true,当且仅 ...

  2. Kafka在Linux环境下搭建过程

    准备工作 Kafka集群是把状态保存在Zookeeper中的,首先要搭建Zookeeper集群.由于我们之前的分布式系统中已经安装zookeeper服务,这里不进行zookeeper安装教程以及应用教 ...

  3. android CMWAP, CMNET有何差别

    什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...

  4. Apache与tomcat

    联系 1)Apache和tomcat都是web网络服务器 2)Apache是普通的服务器,本身支持html即普通网页,可以通过插件支持php也可以与Tomcat连通  (Apache单向连接tomca ...

  5. HTTP协议报文格式

    HTTP协议报文格式 接下来我们看看HTTP协议(Hypertext Transfer Protocol――超文本传输协议)浏览器端(客户端)向WEB服务器端访问页面的过程和HTTP协议报文的格式. ...

  6. javascript 切换动画

    function startMove(obj, json, fn) { clearInterval(obj.timer); obj.timer = setInterval(function() { v ...

  7. 数据存储: sqlite,coredata plist 归档

    sql 语句  结构化查询语言 通用数据库操作语言1.创建数据库create database 1407EDB2.删除数据库drop database 1407EDB3.备份use master ex ...

  8. eclipse中默认的提示键

    Alt+/:代码提示 Ctrl+/:注释/取消注释 Ctrl+D:删除光标所在行 Ctrl+K:将光标停留在变量上,按Ctrl+K键可以查找到下一个同样的变量 Shift+Ctrl+K:和Ctrl+K ...

  9. (a*b)%c 问题

    给你两个数__int64 类型的整数 a ,b,c.问你,(a*b)%c的值是多少??我们知道: (a*b)%c = ((a%c)*(b%c))%c .不过即使这样做,在c很大的情况下,(a%c)*( ...

  10. web 前端 shopnc项目 首页分类一开始做前端,我是拒绝的

    看图别说话 经过几小时的折腾 主要还是靠耐心