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);
}

转载于:https://www.cnblogs.com/AlphaWA/p/11012995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值