首页 技术 正文
技术 2022年11月14日
0 收藏 333 点赞 3,255 浏览 1648 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839