HDU 2141 Can you find it?(二分)

2024-08-27 02:38
文章标签 二分 find hdu 2141

本文主要是介绍HDU 2141 Can you find it?(二分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:clicl here~~

【题目大意】:

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.

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
【解题思路】将前两个数组元素合并为一个,在和最后一个进行选择,二分合并后的数组,如果

   if(sort[mid]<ans) left=mid+1;
   else if(sort[mid]>ans) right=mid-1;
   else {flag=true;break;}

代码:

#include <bits/stdc++.h>
using namespace std;
const int N=505;
int A[N],B[N],C[N],D[N*N];
bool get(int sort[],int k,int ans)//合并后的数组/数组元素个数/和减去第三个数组元素剩下的值
{bool flag=false;int left=0,right=k,mid;while(left<=right){mid=(left+right)>>1;if(sort[mid]<ans) left=mid+1;else if(sort[mid]>ans) right=mid-1;else {flag=true;break;}}return flag;
}
int main()
{//freopen("1.txt","r",stdin);int l,n,z,m,S,tot=1;bool ok;while(scanf("%d%d%d",&l,&n,&m)!=EOF){for(int i=0;i<l;i++) scanf("%d",&A[i]);for(int i=0;i<n;i++) scanf("%d",&B[i]);for(int i=0;i<m;i++) scanf("%d",&C[i]);int k=0;for(int i=0;i<l;i++)for(int j=0;j<n;j++) D[k++]=A[i]+B[j];sort(D,D+k);scanf("%d",&S);printf("Case %d:\n",tot++);while(S--){ok=false;scanf("%d",&z);for(int i=0;i<m;i++){if(get(D,k,z-C[i])) {ok=true;break;}}if(ok) puts("YES");else puts("NO");}}return 0;
}


这篇关于HDU 2141 Can you find it?(二分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110395

相关文章

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

Linux find 命令完全指南及核心用法

《Linuxfind命令完全指南及核心用法》find是Linux系统最强大的文件搜索工具,支持嵌套遍历、条件筛选、执行动作,下面给大家介绍Linuxfind命令完全指南,感兴趣的朋友一起看看吧... 目录一、基础搜索模式1. 按文件名搜索(精确/模糊匹配)2. 排除指定目录/文件二、根据文件类型筛选三、时间

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while