Hdu 2141 binary

Source: Internet
Author: User

Hdu 2141 binary

The meaning is to give you three arrays with n m k numbers, respectively. Can we take a number from the three groups to be equal to s?

At the beginning, I didn't pay attention to the fact that the number can be negative wa many times.


Don't try brute force. It must have timed out.

Merge the two arrays into a list with a smaller number of brute force enumerations)


#include
 
  #include
  
   #include
   
    using namespace std
    ;int main(){    int num1
    [510
    ],num2
    [510
    ],num3
    [510
    ];    int mark
    [300000
    ];    int i
    ,j
    ,k
    ,p
    ,d
    =1
    ;    int n
    ,m
    ;    while(~scanf
    ("%d%d%d"
    ,&n
    ,&m
    ,&k
    ))    {        for(i
    =1
    ;i
    <=n
    ;i
    ++)        scanf
    ("%d"
    ,&num1
    [i
    ]);        for(i
    =1
    ;i
    <=m
    ;i
    ++)        scanf
    ("%d"
    ,&num2
    [i
    ]);        p
    =0
    ;        for(i
    =1
    ;i
    <=n
    ;i
    ++)        for(j
    =1
    ;j
    <=m
    ;j
    ++)        mark
    [++p
    ]=num1
    [i
    ]+num2
    [j
    ];        sort
    (mark
    +1
    ,mark
    +1
    +p
    );        for(i
    =1
    ;i
    <=k
    ;i
    ++)        scanf
    ("%d"
    ,&num3
    [i
    ]);        sort
    (num3
    +1
    ,num3
    +1
    +k
    );        int s
    ;        scanf
    ("%d"
    ,&s
    );        printf
    ("Case %d:\n"
    ,d
    ++);        while(s
    --)        {            int sum
    ;            scanf
    ("%d"
    ,&sum
    );            int flash
    =0
    ;            int left
    ,right
    ,mid
    ;            for(i
    =1
    ;i
    <=k
    ;i
    ++)            {                left
    =1
    ;                right
    =p
    ;                while(left
    <=right
    )                {                    mid
    =(left
    +right
    )/2
    ;                    if(mark
    [mid
    ]>sum
    -num3
    [i
    ]) right
    =mid
    -1
    ;                    else if(mark
    [mid
    ]<sum
    -num3
    [i
    ]) left
    =mid
    +1
    ;                    else                     {                        flash
    =1
    ;                        break;                    }                }                if(flash
    ) break;            }            if(flash
    ) printf
    ("YES\n"
    );            else printf
    ("NO\n"
    );        }    }    return 0
    ;}
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.