zoukankan      html  css  js  c++  java
  • G

    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[501];
    int b[501];
    int c[501];
    int sum[500*500+1];
    int pos;
    int judge(int x){
        
        if(x<sum[0]||x>sum[pos-1]) return 0;
        
        int low=0,high=pos-1;
        while(low<=high){
            int mid=(high+low)/2;
    //        cout<<mid<<endl;
            if(sum[mid]>x){
                high=mid-1;
            }
            else if(sum[mid]<x) low=mid+1;
            else {
                return 1;
            }
        }
        return 0;
    }
    
    int main()
    {
        int l,m,n,ll=0;
        while(cin>>l>>m>>n)
        {
            ll++;
            for(int i=0;i<l;i++)
                cin>>a[i];
            for(int j=0;j<m;j++)
                cin>>b[j];
            for(int k=0;k<n;k++)
                cin>>c[k];
                
            pos=0;
            for(int i=0;i<l;i++)
                for(int j=0;j<m;j++){
                    sum[pos++]=a[i]+b[j];
                }
            sort(sum,sum+pos);
            
            
            int xx;
            cin>>xx;
            
            printf("Case %d:
    ",ll);
            
            for(int i=1;i<=xx;i++){
                int x,flag=0;
                cin>>x;            
                for(int i=0;i<n;i++){
                    if(judge(x-c[i])){
                        flag=1;
                        break;
                    }
                }
                
                if(flag)
                    printf("YES
    ");
                else printf("NO
    ");
                
            }
        }
    return 0;
    }
  • 相关阅读:
    ROXFiler 2.6
    ubuntu下lxr的运用
    NTFS3G-Linux 的 NTFS 驱动步骤
    Songbird 0.2.5 Final
    ePDFView:一个轻量的 PDF 文档阅读东西
    Gmail Notifier:又一个 Gmail 邮件通知法式
    Hybrid Share-文件分享软件
    Dolphin:KDE 中的文件管理器
    文泉驿点阵宋体 0.8(嬴政)正式公布
    KDE 4 Kludge 发布宣布
  • 原文地址:https://www.cnblogs.com/Accepting/p/11245268.html
Copyright © 2011-2022 走看看