做题记录

· · 个人记录

::::info[选入标准]{open}

P6067

Subtask#1 做一个 \mathcal{O}(n ^ 2) 的暴力可以过,对于 1 \le n \le 10 ^ 5 考虑优化。

数学表达方式:

\sum_{i = 1}^{n} \sum_{j = 1}^{n} |a_i - a_j| (i \ne j)

拆开里面的求和公式:

(i - 1) \times a_i - \sum_{k = 1} ^ {i - 1} a_k

里面这个求和用一个前缀和算。

得到答案公式:

\sum_{i = 1} ^ {n} (i - 1) \times a_i - \sum_{j = 1} ^ {i - 1} a_j

注意答案需要乘以 2。

单次询问复杂度 \mathcal{O}(1),前缀和跑一次 \mathcal{O}(n),瓶颈在排序。

总时间复杂度为 \mathcal{O}(n \log n)。

::::success[code]

#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 100010
#define endl '\n'
using namespace std;

int n, a[maxn], sum[maxn];
int query(int l, int r) {
    return sum[r] - sum[l - 1];
}
int solve() {
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1] + a[i];
    }
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += a[i] * (i - 1) - query(1, i - 1);
    }
    return sum * 2;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t; t = 1;
    while (t--) {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        cout << solve() << endl;
    }
    return 0;
}

::::

B4163

严肃不会简单题。

我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll

CSP 的题目不给特殊性质可以死吗了。

可以给一个特殊性质例如 5\% \sim 10\% 的数据保证 a_i = b_i,此时答案为 0。

这特殊性质太几把简单了,没啥用。

Those who cannot remember the past are condemned to repeat it.

有无英语大手子能翻译一下。

考虑 dp 正解。

懒得写,先咕这里了。

P15057

前言:搜并查集标签时搜到的,挑战不看题解独立切了。

直接并查集扔图上做就没了。

至于数学方法没够明白,但看到题解区第一个是个屎名的题解彻底怒了。不给证明啥阴。

这里查阅了机房巨佬 @youngwayboy 的题解然后看不懂,反正并查集能过,不学了。

直接输出 YES 有 20pts。

欸欸欸,怎么评了 5 分钟还没好。

::::success[code]

#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 1000010
#define endl '\n'
using namespace std;

int n;
int fa[maxn];
void init() {
    for (int i = 1; i <= n; i++) {
        fa[i] = i;
    }
}
int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int a, int b) {
    int x = find(a), y = find(b);
    if (x != y) {
        fa[y] = x;
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    init();
    for (int i = 1; i <= n; i++) {
        fa[find(i)] = find(i + i <= n ? i + i : i + i - n);
    }
    for (int i = 1; i <= n; i++) {
        if (find(i) != find(1)) {
            cout << "NO" << endl;
            cout << i << " " << 1 << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
    return 0;
}
=

::::

P14359

啊哈,CSP-J 2025 的题。

草草草,当时我甚至连前缀和都不会你信吗?况且是前缀异或,天塌了/ll

本文中的 \oplus 均指代二进制按位异或。

CSP 的题当然先讲特殊性质骗分,我们挨个查阅特殊性质:

特殊性质 A:对于所有 1 \leq i \leq n,均有 a_i = 1。

特殊性质 B:对于所有 1 \leq i \leq n,均有 0 \leq a_i \leq 1。

特殊性质 C:对于所有 1 \leq i \leq n,均有 0 \leq a_i \leq 255。

观察 A,我们知道 1 \oplus 1 = 0,所以当 k = 0 时,我们的答案应该是 \frac{n}{2},10 分到手。

观察 B,0 \leq a_i \leq 1,我不会。

观察 C,0 \leq a_i \leq 255,我也不会。

所以我考场上只骗到了 10 分。

还有我为什么会想到双指针啊。

@Ethan_sun <= fvv。

再思考部分分做法。

显然我们有一个 \mathcal{O}(n ^ 2) 的 dp,适用于 n \le 10 ^ 3 的数据,也就是前 12 个数据点。

我们定义 x_i = x_{i - 1} \oplus a_i (1 \le i \le n),并且补充定义 x_0 = 0。

转移方程如下:

dp_i = \max(dp_{i - 1}, dp_j + (x_i \oplus x_{j - 1} = k))

::::error[TLE 60pts code]

#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 500010
#define endl '\n'
using namespace std;

int n, k;
int a[maxn], x[maxn], dp[maxn];
void solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        x[i] = x[i - 1] ^ a[i];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            dp[i] = max(dp[i], dp[j - 1] + ((x[i] ^ x[j - 1]) == k));
        }
    }
    cout << dp[n] << endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t; t = 1;
//  cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

::::

其实呢你如果前两题都做对得分就是 100 + 100 + 60 + eps = [260, 260 + eps]。

在我们 SH 就有一等奖了。

考虑正解。

我们的时间复杂度应该需要降到 \mathcal{O}(n) 或 \mathcal{O}(n \log n),当然好像没有后者这个方法。哪里来的一只 \log 啊。

其实还是 dp。

枚举左端点,后面比较容易便不过多叙述。

写这么简略是因为我要去打摆了。

有空再补吧。

::::success[code]

#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 3000010
#define endl '\n'
using namespace std;

int n, k;
int a[maxn], x[maxn], dp[maxn];
void solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        x[i] = x[i - 1] ^ a[i];
    }
    int r = 0, ans = 0;
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        int s = x[i] ^ k;
        if (dp[s] >= r) {
            ans++, r = i;
        }
        dp[x[i]] = i;
    }
    cout << ans << endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t; t = 1;
//  cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

::::

P3909

给你一个长度为 n 的序列 a,对于 a_1, a_2 \dots a_{n - 1}, a_n,求:

(6 \times \sum_{i = 1} ^ n \sum_{j = i + 1} ^ n \sum_{k = j + 1} ^ {n} a_i \times a_j \times a_k) \bmod (10 ^ 9 + 7)

Subtask 1

考虑一个 \mathcal{O}(n ^ 3) 的暴力,按理来说能过掉 Subtask 1 的数据。可是为什么我的代码全是 WA + TLE?

::::error[WA + TLE code]

#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define mod (int)(1e9 + 7)
#define int long long
#define maxn 1000010
#define endl '\n'
using namespace std;

int n;
int a[maxn];
void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            for (int k = j + 1; k <= n; k++) {
                ans += 6 * (a[i] * a[j] * a[k]);
                ans = (ans + mod) % mod;
            }
        }
    }
    cout << (ans + mod) % mod << endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t; t = 1;
//  cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

::::

让我想想。