【HRBUST - 1996】数学等式 (HASH 或 二分)

题干:

又到了数学题的时刻了,给出三个数组A,B,C,然后再给出一个数X,现在我想知道是否能找到三个数满足等式A[i]+B[j]+C[k]=X,你能帮助我么??

Input

本题有多组数据,每组数据第一行输入三个数n, m, h,分别表示数组A,B,C内的的元素个数(0<n,m,h<=500)

接下来三行分别输入数组A,B,C的元素

接下来输入一个数Q,表示Q次询问 (1<=Q<=1000)

接下来Q行每行一个数字Xi(Xi在32位整型范围内)

Output

对于每组数据,首先输出“Case d:”,d表示第d组数据,接下输出Q行,表示每次查询结果,如果能够找到满足等式的三个数则输出YES,反之输出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的二分会TLE,手写binarysearch就可以AC。

AC代码:(700多ms貌似)

#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int n,m,h,q;
ll a[505],b[505],c[505];
ll bb[250005];bool bs(int R,ll ans)
{int l,r,mid;l=1,r=R;mid=(l+r)>>1;while(l<=r){mid=(l+r)>>1;if(bb[mid]==ans)return true;else if(bb[mid]>ans)r=mid-1;else if(bb[mid]<ans)l=mid+1;}return false;
}int main()
{int iCase = 0;ll x;while(~scanf("%d%d%d",&n,&m,&h)) {int top = 0,flag;for(int i = 1; i<=n; i++) scanf("%lld",&a[i]);for(int i = 1; i<=m; i++) scanf("%lld",&b[i]);for(int i = 1; i<=h; i++) scanf("%lld",&c[i]);//打表 for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {bb[++top] = a[i] + b[j];}}sort(bb+1,bb+top+1);int tot = unique(bb+1,bb+top+1) - bb-1;scanf("%d",&q);printf("Case %d:\n",++iCase);while(q--) {scanf("%lld",&x);flag = 0;for(int i = 1; i<=h; i++) {if(bs(tot,x-c[i]) == 1) {printf("YES\n");flag = 1;break;}}if(!flag) printf("NO\n"); }}return 0 ;} 

开O2优化了以后520ms飘过:

#pragma GCC optimize(2) 
#include<bits/stdc++.h>
#define ll long long 
#pragma GCC optimize(1)
using namespace std;
int n,m,h,q;
ll a[505],b[505],c[505];
ll bb[250005];//bool bs(int R,ll ans)
//{
//	int l,r,mid;
//	l=1,r=R;mid=(l+r)>>1;
//	while(l<=r)
//	{
//		mid=(l+r)>>1;
//		if(bb[mid]==ans)
//			return true;
//		else if(bb[mid]>ans)
//			r=mid-1;
//		else if(bb[mid]<ans)
//			l=mid+1;
//	}
//	return false;
//}int main()
{int iCase = 0;ll x;while(~scanf("%d%d%d",&n,&m,&h)) {int top = 0,flag;for(int i = 1; i<=n; i++) scanf("%lld",&a[i]);for(int i = 1; i<=m; i++) scanf("%lld",&b[i]);for(int i = 1; i<=h; i++) scanf("%lld",&c[i]);//打表 for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {bb[++top] = a[i] + b[j];}}sort(bb+1,bb+top+1);int tot = unique(bb+1,bb+top+1) - bb-1;scanf("%d",&q);printf("Case %d:\n",++iCase);while(q--) {scanf("%lld",&x);flag = 0;for(int i = 1; i<=h; i++) {if(binary_search(bb+1,bb+tot+1,x-c[i]) == 1) {printf("YES\n");flag = 1;break;}}if(!flag) printf("NO\n"); }}return 0 ;} 

这题也可以Hash

半年前的博客??忽然找到了,赶紧发一下。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440178.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Apollo自动驾驶入门课程第⑨讲 — 控制(上)

目录 1. 简介 2. 控制流程 3. PID控制 4. PID优劣对比 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月26日 上周我们发布了无人驾驶技术的 规划篇&#xff0c;车辆基于高精地图&#xff0c;感知和预测模块的数据来进行这一…

Angular高版本中为自定义的独立class类添加显式的Angular装饰器

Angular9或10及以后的版本&#xff0c;如果自定义的类上面没有写装饰器的话&#xff0c;编译后在Browser平台不会报错&#xff0c;但是在执行打包命令npm run build --prod时就会报错如下所示&#xff1a; error NG2007: Class is using Angular features but is not decorate…

Python操作Kafka爬坑

组内做大数据&#xff0c;需要kafka写入数据&#xff0c;最近在看python正好&#xff0c;练练手&#xff0c;网上找了一圈&#xff0c;都是用的pykafka&#xff0c;经过一整圈的安装&#xff0c;最终搞定&#xff0c;代码如下#coding:u8import sysimport timeimport randomimpo…

Apollo自动驾驶入门课程第⑩讲 — 控制(下)

目录 1. 线性二次调节器 2. 模型控制预测 3. 总结 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 昨天 Apollo自动驾驶课程马上进入尾声&#xff0c;在无人驾驶技术控制篇&#xff08;上&#xff09;中&#xff0c;具体讲解了最…

*【ZOJ - 3703】Happy Programming Contest(带优先级的01背包)

题干&#xff1a; In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved.…

图解算法学习笔记(四):快速排序

目录 1&#xff09; 示例1&#xff1a; 2&#xff09;快速排序 3) 再谈大O表示法 4&#xff09;小结 本章内容&#xff1a;学习分而治之&#xff0c;快速排序 1&#xff09; 示例1&#xff1a; 假设你是农场主&#xff0c;有一小块土地&#xff0c;你要将这块地均匀分成方…

ThriftParserError: ThriftPy does not support generating module with path in protocol 'd'

使用python连接hive&#xff0c;在 from impala.dbapi import connect 语句报如下错误&#xff1a; ThriftParserError: ThriftPy does not support generating module with path in protocol d 定位到 D:\Anaconda3\Lib\site-packages\thriftpy\parser\parser.py的 if u…

【HDU - 5468】Puzzled Elena(容斥原理,dfs序,数学,素因子分解,有坑)

题干&#xff1a; Problem Description Since both Stefan and Damon fell in love with Elena, and it was really difficult for her to choose. Bonnie, her best friend, suggested her to throw a question to them, and she would choose the one who can solve it.Suppo…

图解算法学习笔记(五):散列表

目录 1&#xff09;示例1&#xff1a; 2&#xff09;散列函数 3&#xff09;应用案例 4&#xff09;冲突 5&#xff09;性能 6&#xff09;小结 本章内容&#xff1a; 学习散列表&#xff0c;最有用的数据结构之一。 学习散列表的内部机制&#xff1a;实现、冲突和散列函…

Ros 消息结构1

1、ROS的消息头信息 #Standard metadata for higher-level flow data types #sequence ID: consecutively increasing ID uint32 seq#Two-integer timestamp that is expressed as: # * stamp.secs: seconds (stamp_secs) since epoch # * stamp.nsecs: nanoseconds since sta…

【HDU - 5475】An easy problem(线段树,思维)

题干&#xff1a; One day, a useless calculator was being built by Kuros. Lets assume that number X is showed on the screen of calculator. At first, X 1. This calculator only supports two types of operation. 1. multiply X with a number. 2. divide X with…

图解算法学习笔记(六):广度优先搜索

目录 1&#xff09;图简介 2&#xff09;图是什么 3&#xff09;广度优先搜索 4&#xff09;实现图 5&#xff09;实现算法 6&#xff09;小结 本章内容; 学习使用新的数据结构图来建立网络模型&#xff1b; 学习广度优先搜索&#xff1b; 学习有向图和无向图…

图解算法学习笔记(七):狄克斯特拉算法

目录 1&#xff09;使用狄克斯特拉算法 2&#xff09;术语 3&#xff09;实现 4&#xff09;小结 本章内容; 介绍加权图&#xff0c;提高或降低某些边的权重&#xff1b; 介绍狄克斯特拉算法&#xff0c;找出加权图中前往X的最短路径&#xff1b; 介绍图中的环…

【HDU - 5477】A Sweet Journey(思维,水题)

题干&#xff1a; Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the f…

[转载]Bluetooth协议栈学习之SDP

原文地址&#xff1a;Bluetooth协议栈学习之SDP作者&#xff1a;BigSam78作者&#xff1a; Sam &#xff08;甄峰&#xff09; sam_codehotmail.com SDP(service discovery protocol&#xff1a;服务发现协议)提供了一个方法&#xff0c;让应用程序检测哪些服务是可用的并探测这…

图解算法学习笔记(八):贪婪算法

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;集合覆盖问题 &#xff08;3&#xff09;NP完全问题 &#xff08;4&#xff09;小结 本章内容&#xff1a; 学习如何处理没有快速算法的问题&#xff08;NP完全问题&#xff09;。学习近似算法&#xff…

【CodeForces - 1152C 】Neko does Maths(数学数论,lcm,gcd性质)

题干&#xff1a; 给出a,b<1e9&#xff0c;你要找到最小的k使得lcm(ak,bk)尽可能小&#xff0c;如果有多个k给出同样的最小公倍数&#xff0c;输出最小的一个k。 解题报告&#xff1a; 因为题目中k太多了&#xff0c;先化简一下公式&#xff0c;假设a>b &#xff0c;则…

vs visual studio 2015安装后的几个问题

前言 最近在win7下重新安装了visual studio 2015&#xff0c;没有安装在默认路径下&#xff0c;编译时出现不少问题&#xff0c;整理如下 1.Failed to locate: "CL.exe". The system cannot find the file specified. TRACKER : error TRK0005: Failed to locate: “…

图解算法学习笔记(九):动态规划

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;最长公共子串 &#xff08;3&#xff09;小结 本章内容&#xff1a; 学习动态规划&#xff0c;它将问题分成小问题&#xff0c;并先着手解决这些小问题。学习如何设计问题的动态规划解决方案。 &#xff…

Java(win10安装jdk,第一个hello world)

Java 第一步 &#xff1a;安装jdk 推荐默认安装。&#xff08;安装到C盘&#xff09;第二步 &#xff1a;配置jdk环境 JAVA_HOME C:\Program Files\Java\jdk1.8.0_191 JDK的安装路径 Path&#xff1a; C:\Program Files\Java\jdk1.8.0_191\bin JDK下bin目录的路径 &#xf…