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. iOS开发之Crash分析,以及收集

    一  先谈谈iOS的Crash收集方式: 1. APP 发生crash,用户手机手机上肯定会有crash纪录,当然删除了该app,或是删了再装 crash纪录还是没了. 2. 如果用户设置-隐私  同 ...

  2. Android应用程序键盘(Keyboard)消息处理机制分析

    在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形 式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的:在上一篇文章< ...

  3. Tomcat 常见问题篇

    Tomcat 常见问题一.Tomcat常见问题 1.Tomcat web容器出现故障时,我们通过Tomcat自带的logs查看原因,以下错误提示都是源于logs 2.Connection refuse ...

  4. ASP.NET内核几大对象、ASP.NET核心知识(6)--转载

    这篇博文主要介绍一下几个对象. 1)HttpContext 2)HttpRequest 3)HttpResponse 4)context. Server 5)context.Session HttpC ...

  5. 同一DataTable下创建多个结构数据相同的DataView的小问题

    昨天在根据经理的要求修改公司后台的时候,遇到了一个很奇怪的问题 DataView dvFocus = ]); DataView dvLook = ]); DataView dvNewUser = ]) ...

  6. JwPlayer播放器【去除Logo、去除版本信息】

    效果图: <html> <head> <title>JwPlayer播放器@杯中红茶</title> <script type="tex ...

  7. IIS Hang Troubleshoot

    Your website maybe stop working and response very lowly. How to find out the reason? Below are the g ...

  8. Unity-资源

    模型.材质.动画 Unity的默认系统单位为"米" 三维软件 三维软件内部米制尺寸/m 默认设置导入Unity中的尺寸/m 与Unity单位的比例关系 Maya 1 100 1:1 ...

  9. Windows7下MySQL5.6.15免安装版的配置(来自yang362046076)

    1.下载mysql-5.5.20-win32.zip,解压到D:\dev,D盘的dev文件夹下就会出现mysql-5.5.20-win32目录,将其重命名为mysql. 2.配置MYSQL的环境变量  ...

  10. 【solr基础教程之九】客户端

    一.Java Script 1.由于Solr本身可以返回Json格式的结果,而JavaScript对于处理Json数据具有天然的优势,因此使用JavaScript实现Solr客户端是一个很好的选择. ...