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. 

InputThere 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. 
OutputFor 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 题目大意:输入3个数组,3个数组中的元素相加,判断是否能得到x;
题目的输入输出有点恶心人,,,写的时候弄的我晕 ,,哇了好几次
思路 :一开始想到的是暴力枚举,,但是肯定会TLE 看了一下大佬们的博客,,用二分法方便一点 就是让A+B构成一个新的数组sum,x-c[i]构成一个新的数组cc,然后在sum中查找是否存在cc中的元素有的话返回YES否则返回NO
AC代码:(本人不太擅长二分所以代码质量不高)
#include<iostream>
#include<algorithm>
using namespace std;
int a[];
int b[];
int c[];
int sum[*+];
int pos;
int judge(int x){ if(x<sum[]||x>sum[pos-]) return ; int low=,high=pos-;
while(low<=high){
int mid=(high+low)/;
// cout<<mid<<endl;
if(sum[mid]>x){
high=mid-;
}
else if(sum[mid]<x) low=mid+;
else {
return ;
}
}
return ;
} int main()
{
int l,m,n,ll=;
while(cin>>l>>m>>n)
{
ll++;
for(int i=;i<l;i++)
cin>>a[i];
for(int j=;j<m;j++)
cin>>b[j];
for(int k=;k<n;k++)
cin>>c[k]; pos=;
for(int i=;i<l;i++)
for(int j=;j<m;j++){
sum[pos++]=a[i]+b[j];
}
sort(sum,sum+pos); int xx;
cin>>xx; printf("Case %d:\n",ll); for(int i=;i<=xx;i++){
int x,flag=;
cin>>x;
for(int i=;i<n;i++){
if(judge(x-c[i])){
flag=;
break;
}
} if(flag)
printf("YES\n");
else printf("NO\n"); }
}
return ;
}

G - Can you find it? 二分的更多相关文章

  1. codeforces 589G G. Hiring(树状数组+二分)

    题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...

  2. G. Of Zorcs and Axes 二分 + 贪心 —— STL的用法

    http://codeforces.com/gym/101149/problem/G 一开始还以为要用二分图去做,但是复杂度也太高了,O(n * m)的话直接爆炸. 考虑贪心,考虑第i个东西优先选一个 ...

  3. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  4. ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps(二分+线段树区间最小)

    During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...

  5. 洛谷——P3939 数颜色(暴力vecotr+二分)

    P3939 数颜色 $vecotr$里二分就是好用,全是$STL$ 颜色数目比较少,可以对每一种颜色弄一个$vector$记录一下,查找$l,r$内颜色数为$x$的兔子数,直接在$G[x]$这个$ve ...

  6. POJ3189二分最大流(枚举下界,二分宽度,最大流判断可行性)

    题意:       有n头猪,m个猪圈,每个猪圈都有一定的容量(就是最多能装多少只猪),然后每只猪对每个猪圈的喜好度不同(就是所有猪圈在每个猪心中都有一个排名),然后要求所有的猪都进猪圈,但是要求所有 ...

  7. noip2016十连测round2

    A: Divisors 题意:给定 m 个不同的正整数 a 1 ,a 2 ,...,a m ,请对 0 到 m 每一个 k 计算,在区间 [1,n] 里有多少正整数 是 a 中恰好 k 个数的约数. ...

  8. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

  9. POJ 1631 Bridging signals(LIS O(nlogn)算法)

    Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...

随机推荐

  1. 洛谷1880 区间dp+记忆化搜索 合并石子

    题目网址:https://www.luogu.com.cn/problem/P1880 题意是:给定一个序列,最小规则是相邻两个值的合并,开销是他们的和,将整个序列合并成一个值的情况下,求解该值的最小 ...

  2. [单调栈] 2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest-Maximum Element In A Stack

    题目:https://codeforces.com/gym/102222/problem/A Maximum Element In A Stack time limit per test 10.0 s ...

  3. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  4. 采用vue编写的功能强大的swagger-ui页面

    think-swagger-ui-vuele swagger-ui有非常多的版本,觉得不太好用,用postman,每个接口都要自己进行录入.所以在基于think-vuele进行了swagger格式js ...

  5. 02 Python函数与模块

     • 函数是什么 print() input() format() list() lower() upper()  • 函数的定义 def 函数名(形参1,2,3)  函数体 return  • 函数 ...

  6. 使用PyTorch进行情侣幸福度测试指南

    欢迎关注磐创博客资源汇总站: http://docs.panchuang.net/ 欢迎关注PyTorch官方中文教程站: http://pytorch.panchuang.net/ 计算机视觉–图像 ...

  7. 学霸双胞胎开源斯坦福CS 221人工智能备忘录,图文并茂看懂反射、状态、变量、逻辑...

    一份斯坦福CS 221人工智能备忘录最近登上了GitHub Trending. 这份备忘录解释了课程中的许多名词.公式和原理,动图.文字.表格并茂,作者之一还是官方助教,堪称CS 221最佳学习笔记. ...

  8. SVN钩子HOOK设置自动备份,服务本地可以看到所有更新内容。

    可以实现SVN本机备份.或者其他备份.关键是可以保持有一份最新的SVN文件可以查看. 实现SVN与WEB同步,可以CO一个出来,也可以直接用自动更新web目录的方法,我们要在svn版本库中配置钩子来实 ...

  9. [POJ2533]Longest Ordered Subsequence<dp>

    题目链接:http://poj.org/problem?id=2533 描述: A numeric sequence of ai is ordered if a1 < a2 < ... & ...

  10. [vijos]1066弱弱的战壕<线段树>

    题目链接:https://www.vijos.org/p/1066 这道题没什么难度,只是要一个排序然后就是线段树的基本套路模版了 但是我还是讲一讲思路吧: 给出的是坐标x,y,当一个点的x,y都小于 ...