题解:P9306 「DTOI-5」进行一个排的重 (Minimum Version)
题目
小 L 有一个长为 的序列 ,其中每一项 都是一个 pair 。
为了让 看起来规整一些,他钦定 分别均为长为 的排列。
为了对 的规整程度进行量化计算,他给出了一个权值函数:
注意 时两个方括号都能取到值,因为我们认为 。
为了让 看起来更加规整,他决定分别以某种方式重排 得到 使得 最小。注意重排时必须将 视为整体。
他希望你求出 $f(a’){\min}a’f(a’){\min}$。
由于方案数可能很大,你只需要求出结果对 取模的值。
对于 的数据,,,保证 均为排列。
题解
比较容易观察到,把最大的放在前面肯定是最好的,这就产生了两种情况:
情况一:存在 使
- 把 放在第一个, 为最优解。
- 考虑排列个数,由于 不会再有贡献,所以为 。
情况二:不存在
分为两种类型:
类型一:
那么后面 这一维没有更多的影响,只考虑 这一维。
我们把 放到距离 足够近的位置,比如在第二个, 取到最小。
所以我们需要找到一个 的范围,使得 。
思路一
方法:
- 找到所有 满足 ,把他们撒到 的范围上;
- 在产生的第一个空位上填入 ;
这样,成功用 挡住其他 ,并且保证了 之前的不会由于 产生影响。
所以总的方案数就是:撒点方案 × 小 全排列 × 大 全排列。
类型二:
同理,为:
答案相加即可,代码如下:
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 54 55 56 57
| #include<bits/stdc++.h> #define LL long long #define st first #define nd second
using namespace std;
const LL mod = 998244353; const int MAXN = 5e5 + 7;
int n; pair<int, int> a[MAXN];
LL fac[MAXN]; pair<int, int> maxP; pair<int, int> maxQ;
inline LL Pow(LL base, LL expo) { LL res = 1; while (expo) { if (expo & 1) res = (res * base) % mod; base = (base * base) % mod; expo >>= 1; } return res; }
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].st; for (int i = 1; i <= n; ++i) cin >> a[i].nd;
fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (fac[i - 1] * i) % mod;
for (int i = 1; i <= n; ++i) { if (a[i].st == n && a[i].nd == n) cout << 2 << ' ' << fac[n - 1], exit(0); if (a[i].st == n) maxP = a[i]; if (a[i].nd == n) maxQ = a[i]; } LL ans = 0; ans = (ans + fac[n - 1] * Pow(n - maxP.nd, mod - 2)) % mod; ans = (ans + fac[n - 1] * Pow(n - maxQ.st, mod - 2)) % mod; cout << 3 << ' ' << ans; return 0; }
|
思路二
考虑 的时候,移动 ,在两者之间选中一些 插入。
方案数为:对不同选数个数求和(小数的选法 × 选中部分的排列 × 没选中部分的排列)。
代码如下(好事多 %):
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include<bits/stdc++.h> #define LL long long #define st first #define nd second
using namespace std;
const LL mod = 998244353; const int MAXN = 5e5 + 7;
int n; pair<int, int> a[MAXN];
LL fac[MAXN]; pair<int, int> maxP; pair<int, int> maxQ;
inline LL Pow(LL base, LL expo) { base = base % mod; LL res = 1; while (expo) { if (expo & 1) res = (res * base) % mod; base = (base * base) % mod; expo >>= 1; } return res; } inline LL C(int x, int y) { return (fac[x] * Pow(fac[y] * fac[x - y], mod - 2)) % mod; }
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].st; for (int i = 1; i <= n; ++i) cin >> a[i].nd;
fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (fac[i - 1] * i) % mod;
for (int i = 1; i <= n; ++i) { if (a[i].st == n && a[i].nd == n) cout << 2 << ' ' << fac[n - 1], exit(0); if (a[i].st == n) maxP = a[i]; if (a[i].nd == n) maxQ = a[i]; }
LL ans = 0; for (int i = 0; i < maxP.nd; ++i) { LL res = C(maxP.nd - 1, i); res = (res * fac[i]) % mod; res = (res * fac[n - 2 - i]) % mod; ans = (ans + res) % mod; } for (int i = 0; i < maxQ.st; ++i) { LL res = C(maxQ.st - 1, i); res = (res * fac[i]) % mod; res = (res * fac[n - 2 - i]) % mod; ans = (ans + res) % mod; } cout << 3 << ' ' << ans; return 0; }
|