题面:
Problem Description
对于一个长度为 $L$($L$ 为奇数)的数组 $a$,定义它的中位数 $\mathrm{median}(a)$ 为 $a$ 中第 $\dfrac{L+1}{2}$ 大的数。
现在给你一个长度为 $n$ 的排列,对于每对满足 $1 \le i \le j \le n$ 且 $j - i \equiv 0 \pmod{2}$ 的 $(i, j)$,你需要计算 $i \times j \times \mathrm{median}\bigl(p[i \ldots j]\bigr)$
输出所有值的总和
第一行输入一个整数 $T$($1 \le T \le 20$),表示测试的总数。
对于每个测试用例,第一行输入一个整数 $n$($1 \le n \le 2000$)。
接下来一行输入 $n$ 个整数,表示 $1$ 到 $n$ 的排列。
思路一
正如题解中讲到的,只需要判断 sum[l-1] == sum[r] 就行,但是问题是,前面已经有 $n$ 的外层循环了,要如何才能把判断这一步的时间复杂度压到 $O(n)$ 呢?
可以用 桶的思想
即反正我是要判断所有的满足 sum[l-1] == sum[r] 条件的位置,在计算公式 $ij\text{median (}p[i\ldots j])$ 的时候还要乘上 j 同时,那么可以先对左边的 l 部分进行处理,将对应的索引(其实是索引+1),放到对应的前缀和的值 的桶中
那么在在对右边的 r 进行处理的时候,只需要将对应的桶中的值 乘上 当下的索引 j (其实是索引r ) 乘上 中位数的值
例如示例中的:$1 : 3 : 2$
按照题意的思路进行计算是:$1 \times 1 \times 1+2 \times 2 \times 3+3 \times 3 \times 2+1 \times 3 \times 2$
按照桶的方式计算后得到的是:$1 \times 1 \times 1+2 \times 2 \times 3+(1+3) \times 3 \times 2$
标程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include <bits/stdc++.h> #define int long long using namespace std; const int P = 2001; const int N = 2005; int T, ans, n, sum[N], s[N * 2], a[N], b[N], j; signed main() { freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) { ans = 0; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { memset(s, 0, sizeof(s)); for (j = 1; j <= n; j++) b[j] = a[j] > a[i] ? 1 : -1; sum[0] = 0; b[i] = 0; for (j = 1; j <= n; j++) sum[j] = sum[j - 1] + b[j]; for (j = 0; j < i; j++) s[sum[j] + P] += j + 1; for (; j <= n; j++) ans += j * s[sum[j] + P] * a[i]; } cout << ans << '\n'; } }
|
思路二(不完整,很乱)
对于这道题,数据量要求我们一定要在 $O(n^2)$ 的时间内通过,我们的做法是固定中位数
即固定一个数,下标记为 center ,把这个数作为某个数组的中位数,然后去找这个数组
先向右拓展,其中我们要记录的是走过的步长 step、数据比 a[center] 大的个数 balance(若小的话就 -1);
同时,我们将得到的信息记录入一个 rsum 数组中,表示的是 在 ceneter 向右拓展的所有区间中,在当前 balance 值的变化情况下的索引
parity 代表当前步长的奇偶性,这可以帮助我们区分不同步长的贡献
对于每个向左扩展的步长 step,将该步长位置的贡献 (center - step) 和预先记录在 rsum[tbalance][parity] 中的右扩展结果相乘,累加到 total 中
右拓展时用 balance + n 是为了去中心化,是为了避免 s 数组的索引为负
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <bits/stdc++.h> #define int long long #define endl '\n' using namespace std;
const int MAX = 2010; int rsum[MAX * 2][2];
signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { int n; cin >> n; vector<int> a(n + 1); for(int i = 1; i <= n ; i++) cin >> a[i];
int re = 0;
for(int center = 1; center <= n; center++){ for(int i = 0; i < MAX * 2; i++) rsum[i][0] = rsum[i][1] = 0; int balance = 0; rsum[n][0] = center; for(int step = 1; step + center <= n; step++){ if(a[step + center] > a[center]) balance++; else{ balance--; } int parity = step % 2; rsum[balance + n][parity] += center + step; }
balance = 0; int total = 0; for(int step = 1 ; center - step >= 1; step++){ if(a[center - step] > a[center]) balance++; else{ balance--; } int parity = step % 2; int tbalance = n - balance; total += (center - step) * rsum[tbalance][parity]; } total += center * rsum[n][0]; total *= a[center]; re += total; } cout << re << endl; } return 0; }
|
后记
[!Tip]- 官方题解
注意到是一个排列,考虑第 $i$ 个数 $p[i]$ 作为中位数的情况。
如果 $p[i]$ 是中位数,那么其他数跟它就只有大小关系有用,我们不妨设小于 $p2$ 的数为 $-1$ ,大于 $p2$ 的数为 $1$
然后对这个 $-1/1$ 的数组进行一个前缀求和,假设求出来是 sum 数组。
显然如果满足 sum[r]=sum[l-1] ,那么从 $l$ 到 $r$ 的中位数就是 $p[i]$
根据这个性质,我们用一个计数数组就可以统计答案了。
时间复杂度 $O(T n^2)$
题目来源:2025“钉耙编程”中国大学生算法设计暑期联赛(1) 1010