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),所以 ...
随机推荐
- Java常用四大线程池用法以及ThreadPoolExecutor详解
为什么用线程池? 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处-理效率 2.线程并发数量过多,抢占系统资源从而导致阻塞 3.对线程进行一些简单的管理 在Java中,线 ...
- 分享知识-快乐自己:intellij Idea报错Could not autowire. No beans of...
intellig idea 使用@Resource或者@Autowire报错,出现红色波浪线: 虽然不影响使用,但是看着很不爽,所以还是解决了下: 报错提示: Could not autowire. ...
- python基础-pickle与shelve
pickle Example 写入文件 import pickle integers = [1, 2, 3, 4, 5] with open('pickle-example.p', 'wb') as ...
- YYYY-mm-dd HH:MM:SS 备忘录
d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义. dddd 周中某天的完整名 ...
- javascript macrotask & microtask
先看一个 实例 案例 console.log('script start'); setTimeout(function() { console.log('setTimeout'); }, 0); Pr ...
- tf.stack和tf.unstack
import tensorflow as tf a = tf.constant([1,2,3]) b = tf.constant([4,5,6]) c1 = tf.stack([a,b],axis=0 ...
- Python IOError: [Errno 13] Permission denied:
一般是代码写错了,比如我遇到的问题就是由于 os.listdir() 传参传错导致的. 本应该传入字符串路径名,但传入了一个文件对象(object)
- 微信开放平台开发-授权、全网发布(PHP)
这两天做了微信开发平台的开发,梳理下... 浙江百牛信息技术bainiu.ltd整理发布于博客园 先看看授权的流程: 第一步:接收component_verify_ticket: 1.微信服务器每隔1 ...
- ubuntu 修改资源镜像
要修改的文件 /etc/apt/sources.list 原资源镜像文件 deb http://mirrors.aliyun.com/ubuntu/ yakkety main multiverse r ...
- ambari2.1.1安装
1 安装环境 系统:centos6.6 Ambari版本:2.1.1 安装指南:https://cwiki.apache.org/confluence/display/AMBARI/Ins ...