can you find it?

来源:互联网 发布:高等教育知乎 编辑:程序博客网 时间:2024/05/01 17:02

最近感觉二分压力超级大,所以和妹子一起做了二分的题目,好水的题目啊,可是没办法谁叫我们太弱了呢,继续加油,我要变大牛,不要做菜鸟。

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 31 2 31 2 31 2 331410
一看数据那么大,就想到二分了,首先把二个变成一个再二分时间复杂度为nlog(n*n)这样就不会超时了。二分算法就不说了,地球人都知道。
代码:
#include<iostream>#include<cmath>#include<algorithm>using namespace std;int a[505],b[505],c[505];int dp[250025];int main(){  int i,j,k,m,n,s,t,cas=0,p,flag;  while(scanf("%d%d%d",&k,&m,&n)!=EOF)  {    for(i=0;i<k;i++)      scanf("%d",&a[i]);    for(i=0;i<m;i++)      scanf("%d",&b[i]);    for(i=0;i<n;i++)      scanf("%d",&c[i]);    p=0;    for(i=0;i<k;i++)      for(j=0;j<m;j++)        dp[p++]=a[i]+b[j];     sort(dp,dp+p);     printf("Case %d:\n",++cas);     scanf("%d",&t);    while(t--)    {     scanf("%d",&s);   flag=0;     for(i=0;i<n;i++)     {       int l=0,r=p-1,mid;       while(l<=r)       {         mid=(l+r)/2;         if(dp[mid]+c[i]==s){ flag=1;break;}         else if(dp[mid]+c[i]<s) l=mid+1;         else if(dp[mid]+c[i]>s) r=mid-1;       }       if(flag) break;    }       if(flag) printf("YES\n");    else printf("NO\n");  }}  return 0;}                  

原创粉丝点击