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;
    }
  • 相关阅读:
    DB2 for Z/os Statement prepare
    Foreign key (referential) constraints on DB2 LUW v105
    复制Informational constraints on LUW DB2 v105
    DB2 SQL Mixed data in character strings
    DB2 create partitioned table
    MVC中使用EF的技巧集(一)
    Asp.Net MVC 开发技巧(二)
    Linq使用技巧及查询示例(一)
    Asp.Net MVC 开发技巧(一)
    Asp.Net MVC Identity 2.2.1 使用技巧(八)
  • 原文地址:https://www.cnblogs.com/Accepting/p/11245268.html
Copyright © 2011-2022 走看看