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
这个题稍微不注意就有点小坑出现,也不知道是什么原因,最开始自己的数组开小了,OJ上报错反而是内存超限,导致一直在找其它的错误
这里给出两个AC代码,思路都是一样的,运用二分来做
#include<cstdio>
#include<cctype>
#include<iostream>
#include<stack>
#include<map>
#include<cstring>
#include<string>
#include<sstream>
#include<queue>
#include<set>
using namespace std ;
int a[505],b[505],c[505];
set<int>sum;
int main() {
int l,n,m;
int cou=1;
while(~scanf("%d%d%d",&l,&n,&m)){
sum.clear();
for(int i=0;i<l;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
scanf("%d",&b[i]);
for(int j=0;j<m;j++)
scanf("%d",&c[j]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
sum.insert(b[i]+c[j]);
}
int t;
scanf("%d",&t);
printf("Case %d:\n",cou++);
while(t--){
int tp;
scanf("%d",&tp);
int flag=0;
set<int>::iterator f;
for(int i=0;i<l;i++){
f=sum.find(tp-a[i]);//利用find函数来搜索值,其实set集合类的find函数内部就是用二分来实现的,所以想要快速AC的同学,可以使用这种方法.
if(f!=sum.end()){
flag=1;
break;
}
}
if(!flag)
printf("NO\n");
else
printf("YES\n");
}
}
return 0 ;
}
下面给出正常的二分AC代码
#include<cstdio>
#include<cctype>
#include<iostream>
#include<stack>
#include<map>
#include<cstring>
#include<string>
#include<sstream>
#include<queue>
#include<set>
#include<algorithm>
using namespace std ;
int a[505],b[505],c[505];
int sum[250005];
int k=0,flag=0;
void bs(int tp,int i)
{
int l=0,r=k-1;
while(l<=r)
{
int m=(r+l)>>1;
if(sum[m]+a[i]==tp)
{
//printf("%d\n",m);
flag=1;
break;
}
else if(sum[m]+a[i]>tp)
r=m-1;
else
l=m+1;
}
}
int main()
{
int l,n,m;
int cou=1;
while(~scanf("%d%d%d",&l,&n,&m))
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(sum,0,sizeof(sum));
for(int i=0; i<l; i++)
scanf("%d",&a[i]);
for(int i=0; i<n; i++)
scanf("%d",&b[i]);
for(int j=0; j<m; j++)
scanf("%d",&c[j]);
k=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
sum[k++]=b[i]+c[j];
}
sort(sum,sum+k);
int t;
scanf("%d",&t);
printf("Case %d:\n",cou++);
while(t--)
{