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),所以 ...
随机推荐
- CSS3实现水位充满文字特效
CSS3实现水位充满文字特效是一款既是Loading特效也是文字特效,Loading动画开始时,文字中的水位渐渐上升,为了模拟水位上升的真实效果,水面还会波浪浮动,当Loading动画结束时,文字中的 ...
- ZOJ 1141 Closest Common Ancestors(LCA)
注意:poj上的数据与zoj不同,第二处输入没有逗号 ' , ' 题意:输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 思路:直接求,两个节点一直往上爬,知道爬到同一个节点, ...
- 剑指OFFER18 判断一个二叉树的子树
public class a18_IsSubTree { public static boolean hasSubTree(TreeNode treeRoot1, TreeNode treeRoot2 ...
- linux命令学习:echo详解,格式化输出,不换行输出
shell脚本不换行刷新数据 #!/bin/bash ] do a=$(ifconfig eth0 | grep 'RX pac' | awk '{print $2}' | awk -F: '{pri ...
- 机器学习 F1-Score, recall, precision
在机器学习,模式识别中,我们做分类的时候,会用到一些指标来评判算法的优劣,最常用的就是识别率,简单来说,就是 Acc=Npre/Ntotal 这里的 Npre表示预测对的样本数,Ntotal表示测试集 ...
- Trie 树内存消耗问题
大家都知道,Trie树(又称字典树)是一种树型数据结构,用于保存大量的字符串.它的优点是:利用字符串的公共前缀来节约存储空间. 相对来说,Trie树是一种比较简单的数据结构,比较易于理解.话说上帝是公 ...
- Mesos和Marathon
libz is required for mesos to build 需要安装zlib-devel-1.2.7-17.el7.x86_64.rpm 其实跨Shell的Profile文件同步只要执 ...
- poj3468区间加减查找——树状数组区间修改查询
题目:http://poj.org/problem?id=3468 增加一个更改量数组,施以差值用法则区间修改变为单位置修改: 利用公式可通过树状数组维护两个数组:f与g而直接求出区间和. 代码如下: ...
- TCP头部格式详解,附Wireshark对TCP头部抓包分析
TCP之所以能为数据通讯提供可靠的传输,主要在于TCP数据包头部功能非常多. 那么,我们先来看看TCP头部格式(RFC 793.1323定义了TCP头部): TCP头部格式中的内容解析如下:(文末还有 ...
- Binary Tree Inorder Traversal-非递归实现中序遍历二叉树
题目描述: 给定一颗二叉树,使用非递归方法实现二叉树的中序遍历 题目来源: http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ ...