考试情况
满分 500,得分 210,C 题没有做出来比较可惜。
A
题目大意
给定一个字符串 $s$,我们需要在其中插入一个字符 ,使得新字符串成为回文串 。如果能找到这样的插入方式,输出任意一种可能的回文串;否则输出 NA。
思路
这道题可以用一些聪明的做法通过,但我不是聪明人,所以我用了不聪明的方法通过。
我们可以直接搜索来看,如果发现有一个地方的字符不一样的时候,我们就分别对两边添加字符,使得当前这两位变得一样,然后继续搜索。如果发现还有一个地方字符串不相等,那么这种添加字符的方法就不行,否则就行。
聪明的做法就是用一个双指针直接直接遍历一遍,如果发现不一样的点和搜索一样,分两种情况讨论,但这样的做法并不比搜索的时间复杂度优秀,并且还容易出现细节错误,不如写搜索。
代码
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 #include <bits/stdc++.h> using namespace std;int T;string s; bool dfs (int l, int r, bool flag) { if (l >= r) { if (flag) { if (((int )s.size () - 1 ) & 1 ) { int tmp = s.size () / 2 ; s.insert (s.begin () + tmp, s[tmp]); } else { int tmp = (s.size () + 1 ) / 2 ; s.insert (s.begin () + tmp, 'a' ); } } s.erase (s.begin ()); cout << s << '\n' ; return 1 ; } if (s[l] != s[r]) { if (!flag) { return 0 ; } else { s.insert (s.begin () + l, s[r]); if (dfs (l + 1 , r, 0 )) return 1 ; s.erase (s.begin () + l); s.insert (s.begin () + r + 1 , s[l]); if (dfs (l + 1 , r, 0 )) return 1 ; s.erase (s.begin () + r + 1 ); } } else { if (dfs (l + 1 , r - 1 , flag)) return 1 ; } return 0 ; } int main () { cin.tie (0 )->sync_with_stdio (0 ); cin >> T; while (T--) { cin >> s; s = ' ' + s; if (!dfs (1 , s.size () - 1 , 1 )) { cout << "NA\n" ; } } return 0 ; }
B
题目大意
给定两组身高数据:
男生身高:$a_1, a_2, \dots, a_n$
女生身高:$b_1, b_2, \dots, b_m$
需要从男生和女生中各选若干人组成仪仗队,排列成两列(一列男生,一列女生),要求:
思路
这道题很明显可以用二分图匹配来做,并且数据范围也非常小,$n$ 只有 $100$ 这么大,所以就可以直接 $n^2$ 建边,然后跑一遍二分图匹配即可。
但这道题也可以使用贪心来做,我们直接对 $a, b$ 数组排序,然后从小到大进行匹配,如果发现可以匹配,那么就直接匹配。
代码
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 #include <bits/stdc++.h> using namespace std;const int N = 110 ;int n, m, a[N], b[N], dp[N][N], ans[N], vis[N];vector<int > e[N]; int dfs (int u, int tag) { if (vis[u] == tag) return 0 ; vis[u] = tag; for (auto v : e[u]) { if (ans[v] == 0 || dfs (ans[v], tag)) { ans[v] = u; return 1 ; } } return 0 ; } int main () { cin >> n; for (int i = 1 ; i <= n; i++) cin >> a[i]; cin >> m; for (int i = 1 ; i <= m; i++) cin >> b[i]; for (int i = 1 ; i <= n; i++) { for (int j = 1 ; j <= m; j++) { if (abs (a[i] - b[j]) <= 1 ) e[i].push_back (j + n); } } int ans = 0 ; for (int i = 1 ; i <= n; i++) ans += dfs (i, i); cout << ans; return 0 ; }
C
题目大意
给定两个数组 $a$ 和 $b$,需要选择一个整数 $d$,计算两个数组的得分:
对于数组中的每个元素,如果 $\geq d$ 得 3 分,否则得 2 分
目标是最大化 $a$ 的总分减去 $b$ 的总分(即 $score_a - score_b$)
如果有多个 $d$ 能达到相同的最大差值,选择使 $score_a$ 最大的那个
思路
我们发现这个整数 $d$ 值一定是数组 $a, b$ 中的元素,我们可以手模样例就可以发现,任何不适 $a, b$ 数组中的数都可以用数组 $a, b$ 的数代替,并且效果都还一样。
所以我们就可以直接枚举 $a, b$ 中的所有元素,计算答案的话,我们就可以先对数组 $a, b$ 先排序,然后用 lower_bound 来查找有多少个比其小的和大的,计算即可。
但这里有一个需要注意的点,就是两个数组的分数差不需要加绝对值,加了的话就只有 20 分了(就是没看题)。
代码
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 #include <bits/stdc++.h> using namespace std;const int N = 2e5 + 10 ;int n, m, a[N], b[N], maxi = -1 , ans1, ans2;;signed main () { cin >> n; for (int i = 1 ; i <= n; i++) cin >> a[i]; cin >> m; for (int i = 1 ; i <= m; i++) cin >> b[i]; sort (a + 1 , a + n + 1 ); sort (b + 1 , b + m + 1 ); a[n + 1 ] = 2e9 + 1 , b[m + 1 ] = 2e9 + 1 ; for (int i = 1 ; i <= n + 1 ; i++) { if (a[i] == a[i - 1 ]) continue ; int pos = lower_bound (b + 1 , b + m + 2 , a[i]) - b - 1 ; int d1 = (i - 1 ) * 2 + (n - i + 1 ) * 3 ; int d2 = pos * 2 + (m - pos) * 3 ; if (d1 - d2 > maxi) { ans1 = d1, ans2 = d2; maxi = d1 - d2; } else if (d1 - d2 == maxi && d1 > ans1) { ans1 = d1, ans2 = d2; } } for (int i = 1 ; i <= m + 1 ; i++) { if (b[i] == b[i - 1 ]) continue ; int pos = lower_bound (a + 1 , a + n + 2 , b[i]) - a - 1 ; int d2 = (i - 1 ) * 2 + (m - i + 1 ) * 3 ; int d1 = pos * 2 + (n - pos) * 3 ; if (d1 - d2 > maxi) { ans1 = d1, ans2 = d2; maxi = d1 - d2; } else if (d1 - d2 == maxi && d1 > ans1) { ans1 = d1, ans2 = d2; } } cout << ans1 << ':' << ans2; return 0 ; }
E
题目大意
蜗蜗有 $n$ 种颜色的宝石,每种颜色 $i$ 有 $a_i$ 颗宝石(同颜色宝石互不相同),每种颜色有一个价值 $v_i$。展览方案的华丽值计算规则为:
求所有可能的展览方案的华丽值总和,结果对 $10^9+7$ 取模。
思路
这道题就是一个纯粹的数学题,式子非常难推。
如果一种颜色的宝石有 $c_i$ 个,那么华丽值就为 $v_i^{c_i}$。
每个 $c_i$ 的取值都有 $a_i + 1$ 个,然而每取 $c_i$ 个宝石都有 $\binom{a_i}{c_i}$ 种选取可能。
于是根据以上分析,我们就可以写出第 $i$ 颗宝石所能贡献出的华丽值:
$$
\binom{a_i}{0} \times v_i^0 + \binom{a_i}{1} \times v_i^1 + \dots + \binom{a_i}{a_i} \times v_{i}^{a_i}
$$
我们发现这个式子有点眼熟啊,这不就是二项式定理吗?
于是我们就可以将这个式子写成 $(1 + v_i)^{a_i}$。
此外,还有其他颜色的宝石,对于对于每种颜色 $j$ ,宝石都有选与不选的两种选择,所以就有 $2^{a_j}$ 种选择,于是 $n - 1$ 个不同颜色的宝石就有 $\Pi_{j \not= i} 2^{a_j}$ 种选择,所以对于一种宝石的贡献就是:
$$
\binom{a_i}{k} \cdot v_i^k \cdot \prod_{j \neq i} 2^{a_j}
$$
把所有的 $n$ 给考虑进去得到:
$$
\sum_{i = 1}^{n} \left( \binom{a_i}{k} \cdot v_i^k \cdot \prod_{j \neq i} 2^{a_j} \right)
$$
将 $\prod_{j \neq i} 2^{a_j}$ 提出来得到:
$$
\left( \sum_{k=1}^{a_i} \binom{a_i}{k} v_i^k \right) \cdot \prod_{j \neq i} 2^{a_j}
$$
我们发现中间括号里的这一坨非常的熟悉,这一部分非常接近二项式定理
$$
(1 + v_i)^{a_i} = \sum_{k = 0}^{a_i} \binom{a_i}{k} v_i^{k} = \binom{a_i}{0}v_i^0 + \sum_{k = 1}^{a_i} \binom{a_i}{k} v_i^k
$$
其中 $\binom{a_i}{0}v_i^0 = 1$,所以 $\sum_{k=1}^{a_i} \binom{a_i}{k} v_i^k = (1+v_i)^{a_i} - 1$。
最终,第 $i$ 种颜色的贡献为:
$$
((1 + v_i)^{a_i} - 1) \times \prod_{j \not= i} 2^{a_j}
$$
那么答案就为:
$$
\sum_{i = 1}^{n} \left( ((1 + v_i)^{a_i} - 1) \times \prod_{j \not= i} 2^{a_j} \right)
$$
这个式子我们就可以直接用快速幂来求解了,但我们需要处理一下 $\prod_{j \not= i} 2^{a_j}$ 这个式子如何求解,如果一个个算的话肯定超时,但我们可以将这个式子给改写一下变成:
$$
\prod_{j\not= i} 2^{a_j} = 2^{\left( \sum_{k = 1}^{n} a_k \right) - a_i}
$$
那么这个式子我们就可以求解了。
代码
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 #include <bits/stdc++.h> #define int long long using namespace std;const int N = 2e5 + 10 ;const int mod = 1e9 + 7 ;int n, a[N], v[N], ans, cnt;int quick_pow (int a, int b) { int res = 1 ; while (b) { if (b & 1 ) res = res * a % mod; a = a * a % mod; b >>= 1 ; } return res; } signed main () { cin >> n; for (int i = 1 ; i <= n; i++) cin >> a[i], cnt += a[i]; for (int i = 1 ; i <= n; i++) { cin >> v[i]; v[i]++; } for (int i = 1 ; i <= n; i++) { ans = (ans + (quick_pow (v[i], a[i]) - 1 ) * (quick_pow (2 , cnt - a[i]))) % mod; } cout << ans; return 0 ; }