垃圾佬的旅游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 ...
随机推荐
- 【JS档案揭秘】第二集 Event loop与执行栈
我时常在思考关于JS的很多知识在工作中有什么用?是否只能存在于面试这种理论性的东西中,对于我们的业务和工作,它们又能扮演怎样的角色.以后在JS档案揭秘的每一期里,都会加入我对于业务的思考,让这些知识不 ...
- 干货 | Elasticsearch、Kibana数据导出实战
1.问题引出 以下两个导出问题来自Elastic中文社区. 问题1.kibana怎么导出查询数据? 问题2:elasticsearch数据导出 就像数据库数据导出一样,elasticsearch可以么 ...
- 根据屏幕分辨率判断当前手机型号(swift4.1)
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...
- .NET Core 单元测试
应用程序测试的类型很多,包括集成测试,Web 测试,负载测试等.在最底层的是单元测试,此测试可以测试单个软件组件或方法.单元测试一般只测试开发人员的代码,不应该测试基础结构普.问题,如数据库,文件系统 ...
- 1.Sentinel源码分析—FlowRuleManager加载规则做了什么?
最近我很好奇在RPC中限流熔断降级要怎么做,hystrix已经1年多没有更新了,感觉要被遗弃的感觉,那么我就把眼光聚焦到了阿里的Sentinel,顺便学习一下阿里的源代码. 这一章我主要讲的是Flow ...
- Joda学习笔记
Joda Time简介 日常业务开发中,经常需要处理日期.比如:获取当前一个月之内的开播记录,获取十分钟之内的红包记录等等.我们之前是用java.util.Calendar实现的,直到我看到占小 ...
- 2019DX#3
Solved Pro.ID Title Ratio(Accepted / Submitted) 1001 Azshara's deep sea 凸包 6.67%(6/90)
- Codeforces 919D Substring (拓扑排序+树形dp)
题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...
- 牛客小白月赛6 水题 求n!在m进制下末尾0的个数 数论
链接:https://www.nowcoder.com/acm/contest/135/C来源:牛客网 题目描述 其中,f(1)=1;f(2)=1;Z皇后的方案数:即在Z×Z的棋盘上放置Z个皇后,使其 ...
- hdu6351 Beautiful Now 杭电第五场 暴力枚举
Beautiful Now Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...