Can you find it?
2015-06-10 19:55
211 查看
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
一开始TLE因为是三个for循环在一起,后来将两个for循环分开就没TLE了
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
一开始TLE因为是三个for循环在一起,后来将两个for循环分开就没TLE了
[code]#include<cstdio> #include<iostream> #include<algorithm> #include<cstdlib> int n,m,l,t,f,tmp,q; using namespace std; int a[1000],b[1000],c[1000],d[2555555]; int main() {int cnt=0; while(~scanf("%d%d%d",&l,&n,&m)) {f=0; for(int i=0;i<l;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[i]); for(int i=0;i<m;i++) scanf("%d",&c[i]); int y=0; for(int j=0;j<l;j++) for(int k=0;k<n;k++) d[y++]=a[j]+b[k]; sort(d,d+y); scanf("%d",&t); printf("Case %d:\n",++cnt); while(t--) {scanf("%d",&tmp); for(int i=0;i<m;i++) {q=tmp-c[i]; int low=0; int high=y-1; int mid=(y-1)/2; while(low<high) { if(d[mid]==q) {f=1; break;} if(d[mid]<q) low=mid+1; if(d[mid]>q) high=mid-1; mid=(high+low)/2; } if(d[low]==q || d[high]==q) f=1; if(f) break;} if(f==1) printf("YES\n"); else printf("NO\n"); f=0; }} return 0; }
相关文章推荐
- 可变参数列表及printf函数的实现
- TIJ英文原版书籍阅读之旅——Chapter Five:Initialization & Cleanup
- 复制对象 copy 与mutable copy
- JQ checkbox 全选 反选获取值
- Android FragmentStatePagerAdapter.notifyDataSetChanged()刷新数据的问题
- 【Java】Java Socket 通信示例
- ubuntu如何修改超级用户密码
- ios UITableView separatorColor 默认颜色
- java处理图片--图片的缩放,旋转和马赛克化
- java处理图片--图片的缩放,旋转和马赛克化
- 获取远程设备IP地址
- android---线性布局
- linux之MySQL(Structured Query Language)结构化查询语言
- core文件分析
- oracle的oci和thin区别
- 认识自己——我所丧失的能力2
- Python之global
- tabBarItem动画
- MYSQL数据库学习十 单表数据记录查询
- MySql模糊查询like通配符简介