垃圾佬的旅游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 ...
随机推荐
- Xcodebuild命令使用
Xcodebuild简介 Xcodebuild是命令行工具包的其中一项. 命令行工具包(Command Line Tools)是一个轻量的.可以与XCode分开的.在Mac上单独下载的命令行工具包. ...
- 驰骋工作流引擎ccflow-流转自定义功能使用说明
流转自定义功能使用说明 关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 节点跳转 节点流转自定义 应用背景: 有一些流程在运行过程中是 ...
- Collectors.toMap不允许Null Value导致NPE
背景 线上某任务出现报警,报错日志如下: java.lang.NullPointerException: null at java.util.HashMap.merge(HashMap.java:12 ...
- 教你用原生CSS写炫酷页面切换效果,跟第三方组件说拜拜
因为项目需要,别人想让我给他写一个个人博客,并且给了我一个其他人的网页,可以点此查看.有的同学可能说了,第三方博客框架这么多,为什么还要去手写的,你说这个有可能是没有看到打开这个博客. 样式介绍 给大 ...
- @Validated和@Valid区别
注解地方 @Validated:可以用在类型.方法和方法参数上.但是不能用在成员属性(字段)上 @Valid:可以用在方法.构造函数.方法参数和成员属性(字段)上 两者是否能用于成员属性(字段)上直接 ...
- 2019强网杯babybank wp及浅析
前言 2019强网杯CTF智能合约题目--babybank wp及浅析 ps:本文最先写在我的新博客上,后面会以新博客为主,看心情会把文章同步过来 分析 反编译 使用OnlineSolidityDec ...
- 地图POI类别标签体系建设实践
导读 POI是“Point of interest”的缩写,中文可以翻译为“兴趣点”.在地图上,一个POI可以是一栋房子.一个商铺.一个公交站.一个湖泊.一条道路等.在地图搜索场景,POI是检索对象, ...
- ctpn+crnn 训练数据集生成
1. https://github.com/Belval/TextRecognitionDataGenerator 2. https://textrecognitiondatagenerator.re ...
- Liunx软件安装之Nginx
安装 Nginx 1) 添加 Nginx 到 YUM 源 sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-rele ...
- Java IO体系之RandomAccessFile浅析
Java IO体系之RandomAccessFile浅析 一.RandomAccessFile综述: 1.1RandomAccessFile简介 RandomAccessFile是java Io体系中 ...