BZOJ 4382 [Hash][TwoPointers]

Description

长度为 n 的一串项链,每颗珠子是k种颜色之一。 第 i 颗与第i1, i+1 颗珠子相邻,第 n 颗与第1颗也相邻。切两刀,把项链断成两条链。要求每种颜色的珠子只能出现在其中一条链中。求方案数量(保证至少存在一种),以及切成的两段长度之差绝对值的最小值。( 2kn1000000 )

Solution

这里写图片描述
刚开始可能 Hash 函数太水了。。。数据大一点碰撞点太多。后来改成了双 Hash 函数。。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;
const int N = 1010101;
const int M = 9999991;
const int P = 3628801;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0;
    for (c = get(); c < '0' || c > '9'; c = get());
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}

int n, k, F1, F2, m, p1, p2, ans1, d;
ll ans2;
int col[N], cnt[N], sd1[N], sd2[N], a[N];
struct Pair {
    int h1, h2, id;
    inline friend bool operator <(const Pair &a, const Pair &b) {
        return a.h1 == b.h1 ? (a.h2 == b.h2 ? a.id < b.id : a.h2 < b.h2) : a.h1 < b.h1;
    }
    inline friend bool operator ==(const Pair &a, const Pair &b) {
        return a.h1 == b.h1 && a.h2 == b.h2;
    }
};
Pair Hash[N];
struct cpp {
    int id[N];
    inline int &operator [](const int &x) {
        return id[x];
    }
};
cpp id;
inline int Abs(int x) {
    return x < 0 ? -x : x;
}
inline int Min(int a, int b) {
    return a < b ? a : b;
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    read(n); read(k); sd1[1] = sd2[1] = 1; ans1 = N;
    for (int i = 1; i <= n; i++) {
        read(col[i]); cnt[col[i]]++;
    }
    sd1[k] = 1; for (int i = k - 1; i; i--) sd1[i] = (ll)sd1[i + 1] * n % M;
    sd2[k] = 1; for (int i = k - 1; i; i--) sd2[i] = (ll)sd2[i + 1] * n % P;
    for (int i = 1; i <= k; i++) {
        id[i] = 1;
        F1 = (F1 + (ll)sd1[i]) % M;
        F2 = (F2 + (ll)sd2[i]) % M;
    }
    for (int i = 1; i <= n; i++) {
        cnt[col[i]]--;
        F1 = (F1 - (ll)sd1[col[i]] * id[col[i]] % M) % M;
        F2 = (F2 - (ll)sd2[col[i]] * id[col[i]] % P) % P;
        if (!cnt[col[i]]) {
            id[col[i]] = 1;
            F1 = (F1 + M + sd1[col[i]]) % M;
            F2 = (F2 + P + sd2[col[i]]) % P;
        } else {
            id[col[i]]++;
            F1 = ((ll)sd1[col[i]] * id[col[i]]+ F1 + M) % M;
            F2 = ((ll)sd2[col[i]] * id[col[i]]+ F2 + P) % P;
        }
        Hash[i].h1 = F1; Hash[i].h2 = F2; Hash[i].id = i;
    }
    sort(Hash + 1, Hash + n + 1);
    for (int i = 1, j; i <= n; i = j + 1) {
        for (j = i; Hash[j] == Hash[i]; j++); j--;
        m = 0; for (int k = i; k <= j; k++) a[++m] = Hash[k].id;
        if (m == 1) continue;
        sort(a + 1, a + m + 1); ans2 += (ll)m * (m - 1) / 2;
        for (int i = 1; i <= m; i++) a[i + m] = a[i] + n;
        for (p1 = p2 = 1; p1 <= m; p1++) {
            while (a[p1] + n - a[p2] >= a[p2] - a[p1] && p2 < p1 + m) {
                d = Abs(a[p1] + n - a[p2] - a[p2] + a[p1]);
                ans1 = Min(ans1, d); p2++;
            }
        }
    }
    cout << ans2 << ' ' << ans1 << endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值