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),所以 ...
随机推荐
- IOS AppStore介绍图的尺寸大小(还有一些自己被拒的分享...)
4s:640*960 5:640*1136 6:750*1334 6P:1242*2208 -------现在新版本的iTunes connect里只上传6P版本的大小就可以了,其他版本苹果会自动分辨 ...
- 《java编程思想》读后笔记:二,吸血鬼数字
书本p75中一道读后练习思考题,题目如下: 吸血鬼数字是指位数为偶数的数字,可以有一对数字相乘得到,而这对数字各包含成绩的一半位数的数字,其中从最初的数字中选取的数字可以任意排序.一两个0结尾的数字是 ...
- 网络编程学习笔记-listen函数
listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定相应的套接字变为被动连接 ...
- AtCoder AGC #2 Virtual Participation
在知乎上听zzx大佬说AGC练智商...于是试了一下 A.Range Product 给$a$,$b$,求$\prod^{b}_{i=a}i$是正数,负数还是$0$ ...不写了 B.Box and ...
- 洛谷P3372线段树模板1——线段树
题目:https://www.luogu.org/problemnew/show/P3372 线段树模板. 代码如下: #include<iostream> #include<cst ...
- puppet初始化安装和配置(puppet自动化系列1)
一.服务器规划 以下均直接yum安装最新版. 服务器操作系统为centos6.2 Puppetmaster1 10.168.32.116 puppstmaster1.jq.com Puppetmast ...
- 模拟select选择器
<form method="post"> <div class="selectly" id="s1"> Select ...
- Elasticsearch的前后台运行与停止(tar包方式)
备注:在生产环境中,往往一般用后台来运行.jps查看. 1.ES的前台运行 [hadoop@djt002 elasticsearch-2.4.3]$ pwd/usr/local/elasticsear ...
- 细说 CSS margin
作者:https://coding.net/u/zhengkenghong原文:https://blog.coding.net/blog/css-margin 细说 CSS margin 本文着重描述 ...
- Spring入门第八课
看如下代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...