要点

  • 官解使用的哈希,给每个数一个二维键值,这样每个排列就有唯一的键值,再预求一下所给数列的前缀键值,暴力寻找有多少个答案即可。
#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(哈希后暴力)的更多相关文章

  1. Codeforces Gym 100015H Hidden Code 暴力

    Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...

  2. Codeforces gym 100685 A. Ariel 暴力

    A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...

  3. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  4. [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力

    Couple Cover Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Descri ...

  5. Codeforces 626D Jerry's Protest(暴力枚举+概率)

    D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  6. CodeForces - 1013B And 与运算暴力

    题目链接: https://vjudge.net/problem/1735275/origin 基本思路: 本题思路比较简单,首先,我们知道 a & x = b, b & x = b; ...

  7. Codeforces 914 C 数位DP+暴力打表+思维

    题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...

  8. codeforces 650D D. Image Preview (暴力+二分+dp)

    题目链接: http://codeforces.com/contest/651/problem/D D. Image Preview time limit per test 1 second memo ...

  9. Codeforces Gym 100203G Good elements 暴力乱搞

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...

随机推荐

  1. BZOJ 1572 [Usaco2009 Open]工作安排Job:贪心 + 优先队列【先放再更新】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1572 题意: 有n个工作,每个工作有一个截止日期dead[i]和收益pay[i]. 完成一 ...

  2. javascript(5)

    (1)数组的细节: 基本用法 var 数组名=[元素值,元素值...]; 元素的值可以是任意类型. 数组是引用类型. js里的引用. 在函数参数列表中,如果传入的是基本类型,那 按值传递.如果传入的是 ...

  3. PHP的深copy和浅copy

    1.对象复制的由来 为什么对象会有“复制”这个概念,这与PHP5中对象的传值方式是密切相关的,让我们看看下面这段简单的代码 /** * 电视机类 */ class Television { /** * ...

  4. 51nod【1196】字符串的数量

    超级神题! 有n种字符,若此种字符的编号( \(1\) ~ \(n\)),\(i*2>n\),则他后面可接任意字符.若不是,则他后面接的字符编号至少要是他的两倍. 问长度为m的字符串的个数. 这 ...

  5. 使用谷歌浏览器进行Web开发技巧

    1.为了避免缓存影响开发,使用使用那个“Ctrl+Shift+N”进入浏览器的隐身模式

  6. Linux_学习_01_ 压缩文件夹

    二.参考资料 1.Linux下压缩某个文件夹命令

  7. Stamps

    链接 分析:dp[i][j]表示前i个数,组成j,最少需要多少个.dp[i][j]=min(dp[i-1][j],dp[i-1][j-k*v[i]]+k),则可以转化为完全背包问题,同样的方法进行降维 ...

  8. 开源LTE代码分析

    跟踪了一个在将开源组织-OpenLTE(将4G通信网络LTE开源),现将自己梳理整理的一些文档Post出来,请有相同兴趣的朋友指点: 一,系统介绍 OpenLTE是一位Mot的工程师在12年发起的一个 ...

  9. 获取CPU ID ,disk ID, MAC ID (windows ARM linux Mac)

    windows 命令行获取CPU ID,可以用ShellExecute wmic cpu get processorid ProcessorId BFEBFBFF000506E3 开源库: 查询CPU ...

  10. java 通过System.getProperties()获取系统参数

    转自:https://www.cnblogs.com/ksuifeng/archive/2010/09/25/1834416.html 1.java的System.getProperty()方法可以获 ...