10-2 Test
rhdeng
·
2020-10-02 17:57:57
·
个人记录
10-2
T1
题意:给定一个由小写字母组成的字符串 s 。有 m 次操作,每次操作给定 3 个参数 l,r,x 。如果 x=1 ,将 s[l]-s[r] 升序排序;如果 x=0 ,将 s[l]-s[r] 降序排序。你需要求出最终序列。
差不多的题目:P2824 [HEOI2016/TJOI2016]排序
范围稍小,时限更大的题目:CF558E A Simple Task
对于第一道题, 因为把一个0/1的序列降序/升序会很简单, 所以二分答案后就都可以转化成0/1了, 最后检查这一位上有没有数即可.
用第一道题的方法,稍加改动,可以在1.5s内跑完.时限1s时能拿到70~80分.但同机房zxyhymzg有一种极快的方法......
T2
题意:
求出满足以下条件的 n\times m 的 01 矩阵个数:
(1) 第 i 行第 1-li 列恰好有 1 个 1。
(2) 第 i 行第 ri-m 列恰好有 1 个 1。
(3) 每列至多有 1 个 1。
很明显应该是DP.但状态是真的难想......设 f[i][j] 为前 i 列中, 有 j 列的 1 属于 r_i \leq i 的行的右区间. 怎么想到的? 或许是因为每一列只能放 1 个 1吧......
转移会相对简单些.
对于一般情况,
f[i][j] = A_{i-j-suml[i-1]}^{suml[i]-suml[i-1]}\times(f[i-1][j]+f[i-1][j-1]\times(sumr[i]-(j-1)))
其中 i \geq suml[i]
$sumr[i]$ 为 一共有多少行的右区间的左端点在前 $i$ 列.
$suml[i]-suml[i-1]$ 就为恰好有多少行左区间的右端点在第 $i$ 列.
它的意思大概就是在第 $i$ 列恰好在某些行右端点时计算这些行的方案数.
而对于 $j = 0$ 的时候要特殊处理一下.
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 3000;
int n, m, suml[maxn+10], sumr[maxn+10], f[maxn+10][maxn+10], a[maxn+10][maxn+10];
int main() {
for (int i = 0; i <= maxn; ++i)
a[i][0] = 1;
for (int i = 1; i <= maxn; ++i)
for (int j = 1; j <= maxn; ++j)
a[i][j] = 1ll*a[i][j-1]*(i-j+1)%mod;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
int l, r; scanf("%d%d", &l, &r);
++suml[l]; ++sumr[r];
}
for (int i = 1; i <= m; ++i) {
suml[i] += suml[i-1];
sumr[i] += sumr[i-1];
}
f[0][0] = 1;
for (int i = 1; i <= m; ++i) {
f[i][0] = f[i-1][0];
for (int j = 1; j <= min(i-suml[i-1], sumr[i]); ++j)
f[i][j] = (f[i-1][j]+1ll*f[i-1][j-1]*(sumr[i]-(j-1))%mod)%mod;
for (int j = 0; j <= min(i-suml[i-1], sumr[i]); ++j)
f[i][j] = 1ll*f[i][j]*a[i-j-suml[i-1]][suml[i]-suml[i-1]]%mod;
}
printf("%d\n", f[m][n]);
return 0;
}
```
### T3
题意:你需要在 $[0,2^n)$ 中选一个整数 $x$,接着把 $x$ 依次异或 $m$ 个整数 $a_1-a_m$。在你选出 $x$ 后,你的对手需要选择恰好一个时刻(刚选完数时、$x$ 异或一些数后或是最后), 将 $x$ 变为 $( \lfloor \frac{2x}{2^n} \rfloor + 2x) \mod 2^n$ 。你想使 $x$ 最后尽量大,而你的对手会使 $x$ 最后尽量小。你需要求出 $x$ 最后的最大值,以及得到最大值的初值数量。
----
这题我一开始没怎么理解 "你想使 $x$ 最后尽量大,而你的对手会使 $x$ 最后尽量小。" 这句话.
实际上,它的意思是对于一个初值 $x$, 对手会选择一个时刻操作使得最后的值最小,得到一个最小值. 而答案, 就是这些最小值中的最大值.
设初值为 $x$ 时, 对手在第 $i$ 轮进行了操作的最后的结果为 $g(x,i)$, 则
$$
ans = \max_{x=0}^{2^n-1} \Big\{ \min_{i=0}^{m} \{g(x, i)\} \Big\}
$$
然后设那个奇怪的操作设为 $f(x)$, 仔细观察发现这就是把 $x$ 在二进制下左移 $1$ 再把原来第 $n-1$ 上的数补在最后, 再取前 $n-1$ 位.则
$$
g(x,i) = f(x \oplus a_i) \oplus(a_m \oplus a_i).
$$
其中 $a_i$ 为异或前缀和.
再然后, 由于异或是每一位分别计算的, 所以 $f(x \oplus a_i) = f(x) \oplus f(a_i)$.
所以 $g(x,i) = f(x) \oplus f(a_i) \oplus(a_m \oplus a_i)
而 f(a_i) \oplus(a_m \oplus a_i) 是个定值. 于是就相当于要找一个 f(x) 使得 \min\{f(x) \oplus f(a_i) \oplus(a_m \oplus a_i)\} 最大.
再再然后,异或 \Rightarrow trie.
把f(a_i) \oplus(a_m \oplus a_i) 插进一个trie中.从高位往下递归,分三种情况讨论.
最后到底了就更新下 ans .
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, m, tot, a[maxn+10], ch[maxn*30+10][2], siz[maxn*30+10], ans, cnt;
int f(int x) {
return ((2*x)/(1<<n)+2*x)%(1<<n);
}
void insert(int x) {
int u = 0;
for (int i = n-1; i >= 0; --i) {
int v = (x>>i)&1;
if (!ch[u][v]) ch[u][v] = ++tot;
u = ch[u][v];
}
++siz[u];
}
void dfs(int u, int i, int x) {
if (ch[u][0] && ch[u][1]) {
dfs(ch[u][0], i-1, x);
dfs(ch[u][1], i-1, x);
} else if (ch[u][0]) {
dfs(ch[u][0], i-1, x+(1<<i));
} else if (ch[u][1]) {
dfs(ch[u][1], i-1, x+(1<<i));
} else {
if (x > ans) ans = x, cnt = 1;
else if (x == ans) ++cnt;
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d", &a[i]);
a[i] ^= a[i-1];
}
for (int i = 0; i <= m; ++i)
insert(f(a[i])^a[i]^a[m]);
dfs(0, n-1, 0);
printf("%d\n%d\n", ans, cnt);
return 0;
}