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

【STL版本】

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define lowbit(x) -x&x
  34. #define all 1,n,1
  35. #define rep(i,x,n) for(int i=(x); i<(n); i++)
  36. #define in freopen("in.in","r",stdin)
  37. #define out freopen("out.out","w",stdout)
  38. using namespace std;
  39. typedef long long ll;
  40. typedef unsigned long long ULL;
  41. typedef pair<int,int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const ll LNF = 1e18;
  44. const int maxn = 1e3 + 20;
  45. const int maxm = 1e6 + 10;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-8;
  48. const int dx[] = {-1,1,0,0,1,1,-1,-1};
  49. const int dy[] = {0,0,1,-1,1,-1,1,-1};
  50. int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
  51. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  52. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  53. ll quickpow(ll a, ll b) {
  54. ll ans = 0;
  55. while (b > 0) {
  56. if (b % 2)ans = ans * a;
  57. b = b / 2;
  58. a = a * a;
  59. }
  60. return ans;
  61. }
  62. int gcd(int a, int b) {
  63. return b == 0 ? a : gcd(b, a%b);
  64. }
  65. bool cmp(int a, int b) {
  66. return a > b;
  67. }
  68. int l,n,m,q,x;
  69. int a[maxn],b[maxn],c[maxn];
  70. int ab[250005];
  71. /*
  72. ai + bj + ck = x ——>
  73. ai + bj = x -ck
  74. 1 2 3
  75. 1 2 3
  76. 1 2 3
  77. */
  78. int main()
  79. {
  80. int cas = 1;
  81. while(~scanf("%d%d%d",&l,&n,&m))
  82. {
  83. int f = 0, k;
  84. ms(a,0),ms(b,0),ms(c,0),ms(ab,0);
  85. rep(i,0,l)
  86. scanf("%d",&a[i]);
  87. rep(i,0,n)
  88. scanf("%d",&b[i]);
  89. rep(i,0,m)
  90. scanf("%d",&c[i]);
  91. k = 0;
  92. rep(i,0,l)
  93. {
  94. rep(j,0,n)
  95. {
  96. ab[k++] = a[i] + b[j];
  97. }
  98. }
  99. sort(ab,ab+k);
  100. sort(c,c+m);
  101. printf("Case %d:\n",cas++);
  102. scanf("%d",&q);
  103. while(q--)
  104. {
  105. //在ab数组二分查找 x - c[i]
  106. f = 0;
  107. scanf("%d",&x);
  108. for(int j=0;j<m;j++)
  109. {
  110. int pos = lower_bound(ab,ab+k,x-c[j]) - ab;
  111. if(ab[pos] == x - c[j])
  112. {
  113. f = 1;
  114. break;
  115. }
  116. }
  117. if(f) printf("YES\n");
  118. else printf("NO\n");
  119. }
  120. }
  121. }

【手写二分版本】:

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define lowbit(x) -x&x
  34. #define all 1,n,1
  35. #define rep(i,x,n) for(int i=(x); i<(n); i++)
  36. #define in freopen("in.in","r",stdin)
  37. #define out freopen("out.out","w",stdout)
  38. using namespace std;
  39. typedef long long ll;
  40. typedef unsigned long long ULL;
  41. typedef pair<int,int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const ll LNF = 1e18;
  44. const int maxn = 1e3 + 20;
  45. const int maxm = 1e6 + 10;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-8;
  48. const int dx[] = {-1,1,0,0,1,1,-1,-1};
  49. const int dy[] = {0,0,1,-1,1,-1,1,-1};
  50. int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
  51. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  52. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  53. ll quickpow(ll a, ll b) {
  54. ll ans = 0;
  55. while (b > 0) {
  56. if (b % 2)ans = ans * a;
  57. b = b / 2;
  58. a = a * a;
  59. }
  60. return ans;
  61. }
  62. int gcd(int a, int b) {
  63. return b == 0 ? a : gcd(b, a%b);
  64. }
  65. bool cmp(int a, int b) {
  66. return a > b;
  67. }
  68. int l,n,m,q,x;
  69. int a[maxn],b[maxn],c[maxn];
  70. int ab[250005];
  71. /*
  72. ai + bj + ck = x ——>
  73. ai + bj = x -ck
  74. 1 2 3
  75. 1 2 3
  76. 1 2 3
  77. */
  78. int main()
  79. {
  80. int cas = 1;
  81. while(~scanf("%d%d%d",&l,&n,&m))
  82. {
  83. int f = 0, k;
  84. ms(a,0),ms(b,0),ms(c,0),ms(ab,0);
  85. rep(i,0,l)
  86. scanf("%d",&a[i]);
  87. rep(i,0,n)
  88. scanf("%d",&b[i]);
  89. rep(i,0,m)
  90. scanf("%d",&c[i]);
  91. k = 0;
  92. rep(i,0,l)
  93. {
  94. rep(j,0,n)
  95. {
  96. ab[k++] = a[i] + b[j];
  97. }
  98. }
  99. sort(ab,ab+k);
  100. sort(c,c+m);
  101. printf("Case %d:\n",cas++);
  102. scanf("%d",&q);
  103. while(q--)
  104. {
  105. //在ab数组二分查找 x - c[i]
  106. f = 0;
  107. scanf("%d",&x);
  108. for(int j=0;j<m;j++)
  109. {
  110. int l = 0, r = k - 1, mid;
  111. while(l <= r)
  112. {
  113. mid = (l+r)/2;
  114. if(ab[mid] == x-c[j])
  115. {
  116. f=1;break;
  117. }
  118. else if(ab[mid]<x-c[j]) l=mid+1;
  119. else if(ab[mid]>x-c[j]) r=mid-1;
  120. }
  121. if(f) break;
  122. }
  123. if(f) printf("YES\n");
  124. else printf("NO\n");
  125. }
  126. }
  127. }

HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】的更多相关文章

  1. HDU 2141 Can you find it? [二分]

    Can you find it? Give you three sequences of numbers A, B, C, then we give you a number X. Now you n ...

  2. C - 啥~ 渣渣也想找玩数字 HDU - 2141(有序序列枚举 + 二分优化查找)

    题目描述 可爱的演演又来了,这次他想问渣渣一题... 如果给你三个数列 A[],B[],C[],请问对于给定的数字 X,能否从这三个数列中各选一个,使得A[i]+B[j]+C[k]=X? 输入 多组数 ...

  3. hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/1000 ...

  4. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

  5. HDU 5265 pog loves szh II (二分查找)

    [题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...

  6. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. hdu 2141:Can you find it?(数据结构,二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  8. hdu 2141 Can you find it?(二分查找变例)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  9. Can you find it? HDU - 2141 (二分查找)

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

随机推荐

  1. Struts1表单校验

    ActionForm中对表单元素进行校验 @Override public ActionErrors validate(ActionMapping mapping, HttpServletReques ...

  2. 机器学习基础知识笔记(一)-- 极大似然估计、高斯混合模型与EM算法

    似然函数 常说的概率是指给定参数后,预测即将发生的事件的可能性.拿硬币这个例子来说,我们已知一枚均匀硬币的正反面概率分别是0.5,要预测抛两次硬币,硬币都朝上的概率: H代表Head,表示头朝上 p( ...

  3. BZOJ4347 POI2016Nim z utrudnieniem(博弈+动态规划)

    由nim游戏的结论,显然等价于去掉一些数使剩下的数异或和为0. 暴力的dp比较显然,设f[i][j][k]为前i堆移走j堆(模意义下)后异或和为k的方案数.注意到总石子数量不超过1e7,按ai从小到大 ...

  4. Java 如何正确停止一个线程

    自己在做实验性小项目的时候,发现自己遇到一个问题:如何控制线程的"死亡"? 首先,如何开启一个线程呢? 最简单的代码: public class Main { public sta ...

  5. BZOJ 4777 Usaco2017 Open Switch Grass Kruskal+替罪羊树+权值线段树

    这道题首先可以看出答案一定是一条边,而且答案一定在最小生成树上,那么我们就可以在这个最小生成树上维护他与异色儿子的边最小值,所以我们就可以已通过Kruskal和一棵平衡树来解决,时间复杂度是O(n*l ...

  6. Spring源码解析-基于注解依赖注入

    在spring2.5版本提供了注解的依赖注入功能,可以减少对xml配置. 主要使用的是 AnnotationConfigApplicationContext: 一个注解配置上下文 AutowiredA ...

  7. bzoj 2525 [Poi2011]Dynamite 二分+树形dp

    [Poi2011]Dynamite Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 270  Solved: 138[Submit][Status][D ...

  8. 生产服务器环境最小化安装后Centos 6.5优化配置备忘

    生产服务器环境最小化安装后 Centos 6.5优化配置备忘 本文 centos 6.5 优化 的项有18处,列表如下: 1.centos6.5最小化安装后启动网卡 2.ifconfig查询IP进行S ...

  9. tcp/ip网络协议学习

    链路层介绍 网络层协议的数据单元是 IP 数据报 ,而数据链路层的工作就是把网络层交下来的 IP 数据报 封装为 帧(frame)发送到链路上,以及把接收到的帧中的数据取出并上交给网络层. 以太网 以 ...

  10. 51Nod 1081前缀和

    #include <iostream> #include <stdio.h> using namespace std; ]; ]; int main() { int n; ci ...