博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hdu Can you find it?(二分答案)
阅读量:4609 次
发布时间:2019-06-09

本文共 2447 字,大约阅读时间需要 8 分钟。

Can you find it?

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
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
Author
wangye
Source
HDU 2007-11 Programming Contest

/*二分答案.比较巧妙.先将两个数组合并搞成一个n^2大的数组.然后二分的话复杂度就有一个log.二分和合并后的数组即对n^2取log.然后复杂度就大大降低了.*/#include
#include
#include
#define MAXN 501#define LL long longusing namespace std;LL s[MAXN*MAXN],a[MAXN],b[MAXN],c[MAXN],n1,n2,n3,n,m;LL read(){ LL x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x*f;}bool erfen(int l,int r,int i,int x){ int mid; while(l<=r) { mid=(l+r)>>1; if(s[mid]+c[i]==x) return true; if(s[mid]+c[i]>x) r=mid-1; else l=mid+1; } return false;}void slove(){ int x; bool flag; while(m--) { flag=false;x=read(); for(int i=1;i<=n3;i++) if(erfen(1,n,i,x)){flag=true;printf("YES\n");break;} if(!flag) printf("NO\n"); } return ;}int main(){ int t=0; while(~scanf("%d%d%d",&n1,&n2,&n3)) { printf("Case %d:\n",++t);n=0; for(int i=1;i<=n1;i++) a[i]=read(); for(int i=1;i<=n2;i++) b[i]=read(); for(int i=1;i<=n3;i++) c[i]=read(); for(int i=1;i<=n1;i++) for(int j=1;j<=n2;j++) s[++n]=a[i]+b[j]; sort(s+1,s+n+1); m=read();slove(); } return 0;}

转载于:https://www.cnblogs.com/nancheng58/p/10068111.html

你可能感兴趣的文章
Junit常用操作
查看>>
[原]VS2012编译GLEW 1.11
查看>>
vod_play.html修改播放器页面模板
查看>>
Python 常用的内置函数
查看>>
Console 程序在任务计划程序无法读写文件
查看>>
Visual Studio 2017创建XAML文件
查看>>
第三十章 网路编程------线程
查看>>
C++第9周项目3参考——利息计算器
查看>>
元音字母
查看>>
一个用户多个订单
查看>>
BZOJ4009: [HNOI2015]接水果
查看>>
Flask filter过滤器
查看>>
个人简介
查看>>
Go语言核心之美-必读
查看>>
【Jsp】JSP自己定义标签与MODEL1、MODEL2标准
查看>>
Navicat Premium 12 模型导出sql
查看>>
BZOJ1396识别子串(后缀自动机)
查看>>
全选反选
查看>>
治愈系课程教材 第四课
查看>>
DataSourceUtils源码分析
查看>>