题解:P13760 Clock Paradox

· · 题解

前置知识

  1. 双指针
  2. 前缀和
  3. 简单数学推导

解题思路

这显然是一道简单的数学题。

首先时间复杂度 O(N^3) 的暴力很容易想到,直接暴力枚举区间并统计贡献即可,这样就有 10 分了。

但是我们对区间的枚举明显是多余的,可以用双指针来优化。我们考虑枚举一个 r,然后对所有以 r 结尾的合法区间统计贡献。由于以 r 结尾的最大合法区间的左端点 l 单调不减,所以每个 r 对应的 l 都可以通过 r-1 对应的 l 得来,只需要开一个容器 last 记录每个数上一次出现的位置即可。然后每次再 O(N) 统计贡献,就可以做到 O(N^2) 的时间复杂度求出答案,然后就有 30 分了。

但它显然还可以更优。对于每个以 r 结尾的最大合法区间 [l,r],对答案的贡献是 \sum_{i=l}^{r}\sum_{j=i}^{r}a_j。设 sum\_a_i=\sum_{j=1}^{i}a_j,则可将式子化简为 \sum_{i=1}^{r}sum\_a_r - sum\_a_{i-1},将求和拆开后可得 \sum_{i=l}^{r}sum\_a_r - \sum_{i=l}^{r} sum\_a_{i-1}=(r-l+1)\times sum\_a_r-\sum_{i=l}^{r} sum\_a_{i-1}。然后我们再设 sum\_s_i=\sum_{j=1}^{i} sum\_a_j,即可将式子化简为 \sum_{i=l}^{r}(r-l+1)\times sum\_a_r-(sum\_s_{r-1}-sum\_s_{l-2})。这样我们就可以 O(N) 或 O(N\log N) 的时间复杂度求出答案了,期望得分 100 分,具体复杂度由具体实现决定。

代码实现中有几个小细节需要注意:sum\_s_{l-2} 可能越界,需要特判;最终输出的结果需要对 10^9 + 7 取模。

代码实现

#include <bits/stdc++.h>
using namespace std;
constexpr int N(2e5 + 5), mod(1e9 + 7); // 注意:模数是 10^9 + 7
struct ModInt { // 自动取模结构体(仅实现了部分功能)
    int value;
    constexpr ModInt() : value(0) {}
    int normalize(long long x) const {
        x %= mod;
        return static_cast<int>(x < 0 ? x + mod : x);
    }
    ModInt(long long v) : value(normalize(v)) {}
    ModInt operator-() const {return {value ? mod - value : 0};}
    ModInt qpow(int exp) const {
        if (exp < 0) return qpow(-exp).inv(); 
        ModInt base = *this, res(1);
        while (exp) {
            if (exp & 1) res *= base;
            base *= base, exp >>= 1;
        }
        return res;
    }
    ModInt inv() const {return qpow(mod - 2);}
    ModInt& operator+=(const ModInt& rhs) { 
        if ((value += rhs.value) >= mod) value -= mod; 
        return *this;
    }
    ModInt& operator-=(const ModInt& rhs) { 
        if ((value -= rhs.value) < 0) value += mod; 
        return *this;
    }
    ModInt& operator*=(const ModInt& rhs) {
        value = (static_cast<long long>(value) * rhs.value) % mod;
        return *this;
    }
    ModInt& operator/=(const ModInt& rhs) {
        value /= rhs.value;
        return *this; 
    }
    friend ModInt operator+(ModInt lhs, const ModInt& rhs) {return lhs += rhs;}
    friend ModInt operator-(ModInt lhs, const ModInt& rhs) {return lhs -= rhs;}
    friend ModInt operator*(ModInt lhs, const ModInt& rhs) {return lhs *= rhs;}
    friend ModInt operator/(ModInt lhs, const ModInt& rhs) {return lhs /= rhs;}
    constexpr bool operator==(const ModInt& rhs) const {return value == rhs.value;}
    constexpr bool operator!=(const ModInt& rhs) const {return value != rhs.value;}
    constexpr bool operator<(const ModInt& rhs) const {return value < rhs.value;}
    constexpr bool operator>(const ModInt& rhs) const {return value > rhs.value;}
    constexpr bool operator<=(const ModInt& rhs) const {return value <= rhs.value;}
    constexpr bool operator>=(const ModInt& rhs) const {return value >= rhs.value;}
    friend ostream& operator<<(ostream& os, const ModInt& m) {return os << m.value;}
    friend istream& operator>>(istream& is, ModInt& m) {
        long long Value;
        is >> Value, m = ModInt(Value);
        return is;
    }
    explicit operator bool () {return value;}
} sum_a[N], sum_s[N];
int n, a[N];
map<int, int> last; // 这里选用适当的容器即可(注意:unordered_map 可能被卡成单次操作复杂度 O(N))
int main() {
    ios::sync_with_stdio(false); // 关闭同步流
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n;
    for (int i(1); i <= n; ++i) cin >> a[i];
    for (int i(1); i <= n; ++i) // 预处理前缀和
        sum_a[i] = sum_a[i - 1] + a[i];
    for (int i(1); i <= n; ++i) // 预处理前缀和数组的前缀和
        sum_s[i] = sum_s[i - 1] + sum_a[i];
    ModInt ans(0);
    for (int l(1), r(1); r <= n; ++r) {
        if (last.count(a[r])) // 更新以 r 结尾的最大合法区间的左端点
            l = max(l, last[a[r]] + 1); // 注意:last[a[r]] 可能小于等于 l
        last[a[r]] = r; // 更新 last[a[r]]
        ModInt len(r - l + 1); // 计算当前区间长度
        // 累加答案
        ans += (len * sum_a[r] - (sum_s[r - 1] - (l > 2 ? sum_s[l - 2] : 0)));
    }
    cout << ans;
    return 0;
}