zoukankan      html  css  js  c++  java
  • HDU2141还是二分查找

    Can you find it?

    Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others) Total Submission(s): 7898    Accepted Submission(s): 2052

    Problem 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 3 1 2 3 1 2 3 1 2 3 3 1 4 10
     
    Sample Output
    Case 1: NO YES NO
     
    Author
    wangye
     
    Source
     
    Recommend
    威士忌
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <cassert>
    11 #include <set>
    12 #include <sstream>
    13 #include <map>
    14 using namespace std ;
    15 #ifdef DeBUG
    16 #define bug assert
    17 #else
    18 #define bug //
    19 #endif
    20 #define zero {0}
    21 const int MAXN=1000;
    22 int L,M,N;
    23 int a[MAXN],b[MAXN],c[MAXN];
    24 int ab[300000];
    25 int S,n;
    26 int cmp(const void*a,const void*b)
    27 {
    28     return *(int *)a-*(int *)b;//升序 
    29 }
    30 int find(int min,int max,int x)
    31 {
    32     int mid;
    33     while(min<=max)
    34     {
    35         mid=(min+max)/2;
    36         if(ab[mid]==x)
    37         return 1;
    38         if(ab[mid]>x)
    39         max=mid-1;
    40         if(ab[mid]<x)
    41         min=mid+1;
    42     }
    43     return 0;
    44 }
    45 int main()
    46 {
    47     #ifdef DeBUG
    48         freopen("C:\Users\Sky\Desktop\1.in","r",stdin);
    49     #endif
    50     
    51     int i,j,k;
    52     int cas=1;
    53     int num;
    54     while(scanf("%d%d%d",&L,&N,&M)!=EOF)
    55     {
    56         num=0;
    57         for(i=0;i<L;i++)
    58         scanf("%d",&a[i]);
    59         for(i=0;i<N;i++)
    60         scanf("%d",&b[i]);
    61         for(i=0;i<M;i++)
    62         scanf("%d",&c[i]);
    63         scanf("%d",&S);
    64         for(i=0;i<L;i++)//将两个数组合并成一个数组,使其为两个数组中各个不同元素的和 
    65         for(j=0;j<N;j++)
    66         ab[num++]=a[i]+b[j];
    67     //    qsort(c,M,sizeof(c[0]),cmp);//可排可不排 
    68         qsort(ab,num,sizeof(ab[0]),cmp);//二分查找的前提是有序的数组 
    69         printf("Case %d:
    ",cas++);
    70         while(S--&&scanf("%d",&n))
    71         {
    72             bool _flag=false;
    73             for(i=0;i<M;i++)
    74             {
    75                 if(find(0,num-1,n-c[i]))
    76                 {
    77                     _flag=true;
    78                     break;
    79                 }
    80             }
    81             if(_flag)
    82             printf("YES
    ");
    83             else
    84             printf("NO
    ");
    85         }
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    Max Sum(经典DP)
    Codeforces Round #166 (Div. 2)D. Good Substrings(字符串散列)
    Edge(简单)
    Just a Hook(线段树,成段更新)
    Codeforces Round #169 (Div. 2) D. Little Girl and Maximum XOR(贪心,中等)
    最大连续子序列(经典DP)
    Compromise(求解最长公共子序列并输出)
    如何查看IE型号
    并查集中Sfind函数的返回值错误,伤了我两天~!
    最大流的非递归Dinic算法
  • 原文地址:https://www.cnblogs.com/Skyxj/p/3201566.html
Copyright © 2011-2022 走看看