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. 多线程程序 怎样查看每个线程的cpu占用

    可以用下面的命令将 cpu 占用率高的线程找出来: ps H -eo user,pid,ppid,tid,time,%cpu,cmd --sort=%cpu 这个命令首先指定参数'H',显示线程相关的 ...

  2. RSA加密算法及其与SpringMVC集成

    如有不足,敬请各位提出批评,定会改正.THX! 本文介绍的是RSA加密算法+Spring Security在SpringMVC中的集成使用. Spring Security是什么? 引用: Sprin ...

  3. Windows7 无法打开ASA SSL VPN和ASDM首页

    原文地址:Windows7 无法打开ASA SSL VPN 首页和无法打开 ASDM GUI 页面作者:futhy              windows 7 无法打开ASA SSL VPN 和AS ...

  4. IOS 开发调试方法

    0.警告 尽量一个警告都不要有 1.错误 1)红色提示 编译过不去的原因大部分是语法,检查括号的匹配,变量名称,作用域范围 2)编译可以通过,可以运行 a.运行过程中程序崩溃 在debug区域的右侧, ...

  5. 客户端调用web中js方法(C调B)跨域问题

    这几天遇到了个棘手问题(c调b),经过排错查出了问题. 一,问题描述如下: 1.客户端需要调用father.html中一个js方法,特殊之处在于 这个father.html中有个iframe嵌套了一个 ...

  6. 常用的方法,读取XML节点并赋值给List集合

    一.前言 很多时候也可以直接在XML文件中配置好节点,在程序需要用到的时候,修改XML文件并不需要重新编译,这里是在极光推送中拿出来的一部分代码.代码简单,大家直接看例子吧. 二.实现过程 1.新创建 ...

  7. VS2010无法断点调试解决办法

    今天我的VS2010忽然出现设置断点的时候,无法进行调试.现象:每次在设置断点调试的时候都会出现卡机,然后VS弹出如下图所示的状况: 解决办法: 依次点击:“工具-->扩展管理器”然后找到.Ne ...

  8. 为什么在CSS中不要再使用@import

    http://www.stevesouders.com/blog/2009/04/09/dont-use-import/为什么在CSS中不要再使用@import

  9. label的for属性与inputde的id元素绑定

    <form> <label for="male">Male</label> <input type="radio" n ...

  10. shell练习--批量创建账号

    #!/bin/bash #By spinestars #-- #cksum5位数获取方法,可能有重复 #pd="user`head -200 /dev/urandom | cksum | h ...