Codeforces 1175F(哈希后暴力)
要点
- 官解使用的哈希,给每个数一个二维键值,这样每个排列就有唯一的键值,再预求一下所给数列的前缀键值,暴力寻找有多少个答案即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int maxn = 3e5 + 5;
int n, a[maxn], ans;
pll hsh[maxn], jc[maxn], sum[maxn];
mt19937 rnd(time(NULL));
pll operator ^ (pll a, pll b) {
return {a.first ^ b.first, a.second ^ b.second};
}
int calc(int pos) {
int res = 0, len = 0;
for (int r = pos + 1; r <= n && a[r] != 1; r++) {
len = max(len, a[r]);
if (r - len >= pos) break;//肯定是重复的
if (r - len >= 0 && (sum[r] ^ sum[r - len]) == jc[len])
res++;
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {//为每个数i弄一个键值
hsh[i].first = rnd();
hsh[i].second = rnd();
jc[i] = hsh[i];
jc[i] = jc[i] ^ jc[i - 1];//排列1~i的键值
}
for (int it = 0; it < 2; it++) {//第一轮算最大值在右边
for (int i = 1; i <= n; i++) {
sum[i] = hsh[a[i]];
sum[i] = sum[i] ^ sum[i - 1];//数列的前缀
}
for (int i = 1; i <= n; i++)
if (a[i] == 1)
ans += calc(i) + (it == 0);
reverse(a + 1, a + 1 + n);//下一轮算最大值在左边的
}
return !printf("%d\n", ans);
}
Codeforces 1175F(哈希后暴力)的更多相关文章
- Codeforces Gym 100015H Hidden Code 暴力
Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...
- Codeforces gym 100685 A. Ariel 暴力
A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...
- Codeforces Gym 100637G G. #TheDress 暴力
G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...
- [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力
Couple Cover Time Limit: 3000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u Descri ...
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- CodeForces - 1013B And 与运算暴力
题目链接: https://vjudge.net/problem/1735275/origin 基本思路: 本题思路比较简单,首先,我们知道 a & x = b, b & x = b; ...
- Codeforces 914 C 数位DP+暴力打表+思维
题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...
- codeforces 650D D. Image Preview (暴力+二分+dp)
题目链接: http://codeforces.com/contest/651/problem/D D. Image Preview time limit per test 1 second memo ...
- Codeforces Gym 100203G Good elements 暴力乱搞
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...
随机推荐
- RQNOJ 311 [NOIP2000]乘积最大:划分型dp
题目链接:https://www.rqnoj.cn/problem/311 题意: 给你一个长度为n的数字,用t个乘号分开,问你分开后乘积最大为多少.(6<=n<=40,1<=k&l ...
- 分享知识-快乐自己:Oracle中定义及使用同义词
Oracle 同义词概念: Oracle的同义词(synonyms)从字面上理解就是别名的意思,和视图的功能类似,就是一种映射关系. 它可以节省大量的数据库空间,对不同用户的操作同一张表没有多少差别; ...
- hadoop源码剖析--hdfs安全模式
一.什么是安全模式 hadoop安全模式是name node的一种状态,处于该状态时有种量特性: 1.namenode不接受任何对hfds文件系统的改变操作(即此时整个文件系统处于只读状态): 2.不 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 京东SDK模板卡盘效果实现代码
最近在做京东模板,因为是最新平台,好多功能都需要摸索,俺技术一般,摸索出一个简易的卡盘功能 ——————使用的是分类推荐模块哦! 本着共享的精神,俺将代码放到这儿了,各人请自便.(代码还不够完善, ...
- windows下搭建绿色nodejs运行环境
无意中在网上看到NodeJS的文章,发现最近还挺火的,正好我也是做技术的,那就下载个玩玩呗,试试它的魅力,也来亲自感受下. 网上nodejs的环境的搭建到处都是,我这里就不说了,今天我想通过我自己的环 ...
- ACM实用C语言函数
函数名: abs 功 能: 求整数的绝对值 用 法: int abs(int i); 程序例: #include <stdio.h> #include <math.h> int ...
- hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)
C - Bob’s Race Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- 测试你开发的web系统在各种类型浏览器上的兼容性
可以使用 https://www.browserstack.com 来测试你所开发的web系统在各种各样的浏览器,以及各种手机平台上的兼容性.
- Hash和Salt Umbraco 默认的password存储方式
本文章转载自 http://blog.reneorban.com/2014/10/hash-and-salt-umbraco-passwords.html Hash and Salt Umbraco ...