垃圾佬的旅游III(Hash + 暴力)
题目链接:http://120.78.128.11/Problem.jsp?pid=3445
最开始的思路就是直接暴力求解,先把所有的数值两两存入结构体,再从小到大枚举。用二分的思路去判断数值以及出现,结果TLE,但优化一下应该也能过,因为题目说只有两组数据。代码如下:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int n;
int ans,tot;
int a[];
struct node{
int x,a,b;
bool operator < (const node &b) const{
return x < b.x;
}
}q[MAXN]; int find_f(int x,int a,int b){
int l(),r = tot;
while(l<r){
int mid = (l + r) >> ;
if(q[mid].x < x)
l = mid+;
else
r = mid;
}
while(l < tot && q[l].x == x){
if(q[l].a != a && q[l].b != a && q[l].a != b && q[l].b != b)
break;
l++;
}
if(q[l].x != x)
return ;
return ;
} int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
while(scanf("%d",&n)!=EOF){
if(!n)
break;
for(int i = ; i <= n; i++)
scanf("%d",&a[i]);
ans = -INF;
tot = ;
MMT(q);
for(int i = ; i < n; i++){
for (int j = i+; j <= n; j++){
q[tot].x = a[i] + a[j];
q[tot].a = i;
q[tot++].b = j;
}
}
sort(q,q+tot);
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
if(i != j){
int t = a[i]-a[j];
if(find_f(t,i,j)){
if(a[i] > ans)
ans = a[i];
}
}
}
}
if(ans == -INF)
printf("No Solution\n");
else
printf("%d\n",ans);
}
return ;
}
AC思路是队友告诉我的,在暴力枚举的基础上加一个Hash维护。还是记录两两的和,再枚举第三个数,但这里判断一下第三个数和答案是否在两两数和中存在就行了。判断就是每两个数标记一下,排进哈希散列表就行了。值得注意的是,哈希因为本质是同余,所以需要加一句特判,在判断存在之后,取出组成两两和的那两数,再判断三个值是否相等。
为什么要加特判 ,因为哈希是MOD 一个 P ,所以可能出现重复。
还有就是哈希MOD P而不能是任意数,开始就mod1e6+5虽然对于两组数据可能没问题,但是最好还是改成质数例如(1e6+3);
AC代码:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int n, a[], Hash[],d;
int MAXn = MAXN - ;
struct node{
int x,a,b;
bool operator < (const node &b) const{
return x < b.x;
}
}q[MAXN]; int check(int tp, int a, int b){
return (q[tp].a == a || q[tp].b == a || q[tp].b == a || q[tp].b == b);
} int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
while(scanf("%d",&n)!=EOF){
d = ;
MMT(Hash);
MMT(q);
for(int i = ; i <= n; i++)
scanf("%d",a+i);
sort(a+,a++n);
for(int i = ; i < n; i++)
for(int j = i+; j <= n; j++){
int k = (a[i]+a[j]) % MAXn;
if(k < ) k += MAXn;
if(!Hash[k]) {
Hash[k] = ++d;
q[d].a = i, q[d].b = j;
}else{
d++;
int tp = Hash[k];
while(q[tp].x)
tp = q[tp].x;
q[tp].x = d;
q[d].a = i, q[d].b = j;
}
} int ans = n, flag = ;
for(; ans && flag; ans--)
for(int i = n; i; i--){
if(i == ans)
continue;
int k = (a[ans]-a[i]) % MAXn;
if(k < )
k += MAXn;
if(!Hash[k])
continue;
int tp = Hash[k];
while(check(tp, i, ans) && q[tp].x)
tp = q[tp].x;
if(!check(tp, i, ans) && tp){
if(a[q[tp].a] + a[q[tp].b] + a[i] != a[ans]) continue;
flag = ;
printf("%d\n", a[ans]);
break;
}
}
if(flag)
printf("No Solution\n");
}
return ;
}
emmmm还有就是冲突处理,数组模拟一下链表就行了 =7=
这个题可能无从入手的地方就是不知道该怎么模拟,直接四个数枚举肯定炸,然后二二模拟也不行,所以就肯定需要一些手段进行维护和判断。所以就要开数组标记呀, 但肯定这么大的数开不了,那么就只好压缩数组了,这里就想到了二分去判断第三个数以及答案是否存在,但是又TLE,那就hash呗,反正处理数据的只有那么几种方法。
一些小问题就是写hash遇到负数情况,重复冲突情况以及特判。其他的话也就没什么了。
垃圾佬的旅游III(Hash + 暴力)的更多相关文章
- PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)
说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...
- Magic FZU - 2280 无脑HASH暴力
Kim is a magician, he can use n kinds of magic, number from 1 to n. We use string Si to describe mag ...
- Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)
732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...
- HASH暴力破解工具-Hashcat
乌云网看到一篇文章讲述hashcat的使用简介(戳这里),对使用字典破解MD5内容 简单在kali上尝试了一下. (1)首先查看了下hashcat的帮助文档,简单截取了其中的部分常用说明. hashc ...
- [bzoj1692] [Usaco2007 Dec]队列变换 (hash||暴力)
本题同bzoj1640...双倍经验双倍幸福 虽然数据范围n=3w然而O(n²)毫无压力= = http://blog.csdn.net/xueyifan1993/article/details/77 ...
- Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力
D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...
- 【bzoj3796】Mushroom追妹纸 hash/sa+kmp+二分
Description Mushroom最近看上了一个漂亮妹纸.他选择一种非常经典的手段来表达自己的心意--写情书.考虑到自己的表达能力,Mushroom决定不手写情书.他从网上找到了两篇极佳的情书, ...
- 7.26机房报零赛——无尽的矩阵【kmp+hash】
恩,其实大家都没有报零,反正我是蒟蒻 为了纪念我第一次打过哈希,特此写一篇题解 题目描述 从前有一个的小矩阵,矩阵的每个元素是一个字母(区分大小写),突然有一天它发生了 变异,覆盖了整个二维空间,即不 ...
- [luogu1090 SCOI2003] 字符串折叠(区间DP+hash)
传送门 Solution 区间DP,枚举断点,对于一个区间,枚举折叠长度,用hash暴力判断是否能折叠即可 Code #include <cstdio> #include <cstr ...
随机推荐
- php cmd命令行 导入 与备份
- Java反射Reflect的使用详解
目录 一. 什么是反射 二. 反射的基础Class 2.1 Class类概述 2.2 Class类对象获取的三种方式 三. 反射-构造函数 3.1 getDeclaredConstructor(Cla ...
- Yii GridView Ajax 刷新
Yii GridView Ajax 刷新,当页面点击一个按钮时,刷新数据. 1.控制器 <?php class privController extends Controller{ publi ...
- Sqlserver 查询把多行内容拼成一个字符串
当使用:SELECT ','+Id FROM dbo.Test FOR XML PATH('')); //这样读取的数据虽然是1,2,3,4,但是仍然是xml格式,所以当数据超过2033时候,用sql ...
- ggplot2 |legend参数设置,图形精雕细琢
本文首发于微信公众号“生信补给站”,https://mp.weixin.qq.com/s/A5nqo6qnlt_5kF3_GIrjIA 学习了ggplot2|详解八大基本绘图要素后,就可以根据自己的需 ...
- spring-boot-plus项目配置文件(四)
spring-boot-plus项目配置文件 配置文件说明 配置说明 项目中配置文件主要使用yml格式 配置文件位置:spring-boot-plus\src\main\resources\confi ...
- GLFW+GLAD OpenGL Mac开发环境搭建
前言 OpenGL 是什么?The Industry Standard for High Performance Graphics 这是官方解释.说白了他就是一套标准接口.对,是接口,并没有实现具体的 ...
- SpingBoot:整合Elasticsearch7.2.0
Spring boot 2.1.X整合Elasticsearch最新版的一处问题 新版本的Spring boot 2的spring-boot-starter-data-elasticsearch中支持 ...
- 分享各大CMS采集资源站网址合集
分享各大CMS采集资源站网址合集 http://www.172zy.xyz/ 172云资源 http://www.dbzyz.com/ 豆瓣云资源 http://www.gaoqingzy.com/ ...
- 本地项目上传到github上最直接步骤
1.首先得有一个git账号(本地安装git) 2.git上创建一个project 3.回到本地你要提交文件夹位置 4.按住shift + 鼠标右键 选择在此处打开命令窗口 5.输入命令 git in ...