hdu 2141 (二分)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141

Can you find it?

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

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

这题写的不爽,不写题解了,看代码吧

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <iostream>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 long long sum[250005];
10 int str1[505],str2[505],str3[505];
11
12 int Table(int l,int n,int m)
13 {
14
15     int cas=0;
16     for(int i=0; i<l ;i++)
17     {
18
19         for(int j=0; j<n; j++)
20         {
21
22             sum[cas++]=str1[i]+str2[j];
23         }
24     }
25     return cas;
26 }
27
28 bool BSearch(long long sum[],int k,int cas)
29 {
30
31     int left=0,right=cas-1;
32     while(left<=right)
33     {
34
35         int mid=(left+right)>>1;
36         if(sum[mid] == k) return true;
37         else if(k < sum[mid]) right=mid-1;
38         else left = mid+1;
39     }
40     return false;
41 }
42
43 int main()
44 {
45
46     int l,m,n,i,j;
47     int ss=1;
48     while(scanf("%d%d%d",&l,&n,&m)!=EOF)
49     {
50
51         memset(str1,0,sizeof(str1));
52         memset(str2,0,sizeof(str2));
53         memset(str3,0,sizeof(str3));
54
55         for(i=0; i<l; i++)
56             scanf("%d",&str1[i]);
57         for(j=0; j<n; j++)
58             scanf("%d",&str2[j]);
59         for(i=0; i<m; i++)
60             scanf("%d",&str3[i]);
61         int k,tmp;
62         scanf("%d",&k);
63         int cas=Table(l,n,m);
64         sort(sum,sum+cas);
65         printf("Case %d:\n",ss++);
66         while(k--)
67         {
68                 scanf("%d",&tmp);
69                 for(i=0; i<m; i++)
70                 {
71
72                     if(BSearch(sum,tmp-str3[i],cas))
73                     {
74
75                         printf("YES\n");
76                          break;
77                     }
78                 }
79                 if(i>=m)printf("NO\n");
80         }
81
82     }
83     return 0;
84 }

时间: 2024-12-28 16:17:44

hdu 2141 (二分)的相关文章

hdu 2141 二分

题意是给你三个数组   分别有n m k个数     从三个数组里分别拿一个数能不能等于s 刚开始没注意数可以为负数    wa了好多次 别想直接暴力  肯定超时 现将两个数组合并  暴力枚举数少的      二分枚举数多的(非常省时) #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int main() { int num1[510],num2[510],num3

Can you find it?(hdu 2141 二分查找)

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 19416    Accepted Submission(s): 4891 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu 2141 Can you find it?(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. 1 /* 2 3 x^2+6*x-7==y 4 输入y 求x 精确度为10^-5 5 0=<x<=10000 6 7 */ 8 #include <iostream> 9 #include <cstdio> 10 using namespace std; 11 int main (void) 1

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in

HDU 2141(二分&amp;三分 _B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 ----------------------------------------------------------------------------------- 题意:三个数组,找到每个数组中加和为M的值的组数. 思路:首先将后两个数组加和 O(N^2),排序 O(NlogN),然后利用二分求解. 代码: #include<cstdio> #include<cstring>

HDU 2141 Can you find it? 二分查找

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 21485    Accepted Submission(s): 5446 Problem Description Give you three sequences of numbers A, B, C, then we give you a number