HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】
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版本】
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll quickpow(ll a, ll b) {
ll ans = 0;
while (b > 0) {
if (b % 2)ans = ans * a;
b = b / 2;
a = a * a;
}
return ans;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
bool cmp(int a, int b) {
return a > b;
}
int l,n,m,q,x;
int a[maxn],b[maxn],c[maxn];
int ab[250005];
/*
ai + bj + ck = x ——>
ai + bj = x -ck
1 2 3
1 2 3
1 2 3
*/
int main()
{
int cas = 1;
while(~scanf("%d%d%d",&l,&n,&m))
{
int f = 0, k;
ms(a,0),ms(b,0),ms(c,0),ms(ab,0);
rep(i,0,l)
scanf("%d",&a[i]);
rep(i,0,n)
scanf("%d",&b[i]);
rep(i,0,m)
scanf("%d",&c[i]);
k = 0;
rep(i,0,l)
{
rep(j,0,n)
{
ab[k++] = a[i] + b[j];
}
}
sort(ab,ab+k);
sort(c,c+m);
printf("Case %d:\n",cas++);
scanf("%d",&q);
while(q--)
{
//在ab数组二分查找 x - c[i]
f = 0;
scanf("%d",&x);
for(int j=0;j<m;j++)
{
int pos = lower_bound(ab,ab+k,x-c[j]) - ab;
if(ab[pos] == x - c[j])
{
f = 1;
break;
}
}
if(f) printf("YES\n");
else printf("NO\n");
}
}
}
【手写二分版本】:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll quickpow(ll a, ll b) {
ll ans = 0;
while (b > 0) {
if (b % 2)ans = ans * a;
b = b / 2;
a = a * a;
}
return ans;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
bool cmp(int a, int b) {
return a > b;
}
int l,n,m,q,x;
int a[maxn],b[maxn],c[maxn];
int ab[250005];
/*
ai + bj + ck = x ——>
ai + bj = x -ck
1 2 3
1 2 3
1 2 3
*/
int main()
{
int cas = 1;
while(~scanf("%d%d%d",&l,&n,&m))
{
int f = 0, k;
ms(a,0),ms(b,0),ms(c,0),ms(ab,0);
rep(i,0,l)
scanf("%d",&a[i]);
rep(i,0,n)
scanf("%d",&b[i]);
rep(i,0,m)
scanf("%d",&c[i]);
k = 0;
rep(i,0,l)
{
rep(j,0,n)
{
ab[k++] = a[i] + b[j];
}
}
sort(ab,ab+k);
sort(c,c+m);
printf("Case %d:\n",cas++);
scanf("%d",&q);
while(q--)
{
//在ab数组二分查找 x - c[i]
f = 0;
scanf("%d",&x);
for(int j=0;j<m;j++)
{
int l = 0, r = k - 1, mid;
while(l <= r)
{
mid = (l+r)/2;
if(ab[mid] == x-c[j])
{
f=1;break;
}
else if(ab[mid]<x-c[j]) l=mid+1;
else if(ab[mid]>x-c[j]) r=mid-1;
}
if(f) break;
}
if(f) printf("YES\n");
else printf("NO\n");
}
}
}
HDU 2141 Can you find it?【二分查找是否存在ai+bj+ck=x】的更多相关文章
- 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 ...
- C - 啥~ 渣渣也想找玩数字 HDU - 2141(有序序列枚举 + 二分优化查找)
题目描述 可爱的演演又来了,这次他想问渣渣一题... 如果给你三个数列 A[],B[],C[],请问对于给定的数字 X,能否从这三个数列中各选一个,使得A[i]+B[j]+C[k]=X? 输入 多组数 ...
- hdu 4190 Distributing Ballot Boxes(贪心+二分查找)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/1000 ...
- Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化
题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$ $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...
- HDU 5265 pog loves szh II (二分查找)
[题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...
- HDU 3280 Equal Sum Partitions(二分查找)
Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 2141:Can you find it?(数据结构,二分查找)
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- 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 ...
- 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 ...
随机推荐
- ScrollBarsEnabled的使用
在WinForm中通过WebBrowser获取网页,我想把WebBrowser的ScollBar去掉,我的网页不需要滚动条. 设置方法如下:单击WebBrowser设计页面,在属性页面有一个Scrol ...
- BZOJ1093 [ZJOI2007]最大半连通子图 【tarjan缩点 + DAG最长路计数】
题目 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G ...
- SDOI 2009 学校食堂 状压dp
这个题的关键处1 紧跟着他的bi个人 —— 由此得出任意一个状态都可以表示为 有第一个人没吃到饭做分隔的前面所有人已吃饭,并用1<<8表示之后的(包括他)的八个人的状态2 信息仍然是上一个 ...
- python3处理pdf
https://github.com/1049451037/pdfminer3k 使用pdfminer3k,如果是python2的话直接用pdfminer就行了. python setup.py in ...
- power designer 绘制E-R 图
总体概括:本篇主要先介绍E-R图的一些基本概念,然后介绍怎么绘制E-R图,特别是用power designer 的反向工程怎么把表中对字段的注释也展示出来. 1.E-R图的基本概念: E-R图就是en ...
- SVN 服务器安装及配置(WIN7)
软件安装包 客户端: 服务端: 安装服务端 不整合 Apache 服务器可以忽略此选项. 安装程序会自动在path下配置好环境变量:D:\Subversion\bin; 查看是否安装成功: C:\Us ...
- es6+最佳入门实践(8)
8.Promise 8.1.什么是异步? 要理解异步,首先,从同步代码开始说 alert(1) alert(2) 像上面的代码,执行顺序是从上到下,先后弹出1和2,这种代码叫做同步代码 alert(0 ...
- macos装多个python
简介 Mac包管理工具brew: 安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Ho ...
- 利用saltstack初始化OpenStack服务器环境
目录架构图如上图所示 sls脚本详情如下: Sync_Host: file.managed: - name: /etc/hosts - source: salt://state/files/hosts ...
- [BZOJ3698]XWW的难题解题报告|上下界网络流|有源汇最大流
XWW是个影响力很大的人,他有很多的追随者.这些追随者都想要加入XWW教成为XWW的教徒.但是这并不容易,需要通过XWW的考核.XWW给你出了这么一个难题:XWW给你一个N*N的正实数矩阵A,满足XW ...