2024.12.14 周六
Q1. 1000
You are given two strings $a$ and $b$ of equal length, consisting of only characters 0 and/or 1; both strings start with character 0 and end with character 1.
You can perform the following operation any number of times (possibly zero):
- choose one of the strings and two equal characters in it; then turn all characters between them into those characters.
You have to determine if it's possible to make the given strings equal by applying this operation any number of times.
Input
($2 \le |a| \le 5000$), ($2 \le |b| \le 5000$), $|a| = |b|$
both strings start with 0 and end with 1;
Q2. 1000
Pak Chanek is the chief. On one night filled with lights, Pak Chanek has a sudden and important announcement that needs to be notified to all of the $n$ residents.
First, Pak Chanek shares the announcement directly to one or more residents with a cost of $p$ for each person. After that, the residents can share the announcement to other residents . For each $i$, if the $i$-th resident has got the announcement at least once , he/she can share the announcement to at most $a_i$ other residents with a cost of $b_i$ for each share.
If Pak Chanek can also control how the residents share the announcement to other residents, what is the minimum cost for Pak Chanek to notify all $n$ residents of Khuntien about the announcement?
Q3. 1000
Artem suggested a game to the girl Olya. There is a list of $n$ arrays, where the $i$-th array contains $m_i \ge 2$ positive integers $a_{i,1}, a_{i,2}, \ldots, a_{i,m_i}$.
Olya can move at most one (possibly $0$) integer from each array to another array. Note that integers can be moved from one array only once, but integers can be added to one array multiple times, and all the movements are done at the same time.
The beauty of the list of arrays is defined as the sum $\sum_{i=1}^n \min_{j=1}^{m_i} a_{i,j}$. In other words, for each array, we find the minimum value in it and then sum up these values.
The goal of the game is to maximize the beauty of the list of arrays. Help Olya win this challenging game!
Q4. 1000
Alex got a new game called "GCD permutations" as a birthday present. Each round of this game proceeds as follows:
- First, Alex chooses a permutation$^{\dagger}$ $a_1, a_2, \ldots, a_n$ of integers from $1$ to $n$.
- Then, for each $i$ from $1$ to $n$, an integer $d_i = \gcd(a_i, a_{(i \bmod n) + 1})$ is calculated.
- The score of the round is the number of distinct numbers among $d_1, d_2, \ldots, d_n$.
Alex decided to find a permutation $a_1, a_2, \ldots, a_n$ such that its score is as large as possible.
Q5. 1000
Vlad remembered that he had a series of $n$ tiles and a number $k$. The tiles were numbered from left to right, and the $i$-th tile had colour $c_i$.
If you stand on the first tile and start jumping any number of tiles right, you can get a path of length $p$. The length of the path is the number of tiles you stood on.
Vlad wants to see if it is possible to get a path of length $p$ such that:
- it ends at tile with index $n$;
- $p$ is divisible by $k$
- the path is divided into blocks of length exactly $k$ each;
- tiles in each block have the same colour, the colors in adjacent blocks are not necessarily different.
For example, let $n = 14$, $k = 3$.
The colours of the tiles are contained in the array $c$ = [$\color{red}{1}, \color{violet}{2}, \color{red}{1}, \color{red}{1}, \color{gray}{7}, \color{orange}{5}, \color{green}{3}, \color{green}{3}, \color{red}{1}, \color{green}{3}, \color{blue}{4}, \color{blue}{4}, \color{violet}{2}, \color{blue}{4}$]. Then we can construct a path of length $6$ consisting of $2$ blocks:
$\color{red}{c_1} \rightarrow \color{red}{c_3} \rightarrow \color{red}{c_4} \rightarrow \color{blue}{c_{11}} \rightarrow \color{blue}{c_{12}} \rightarrow \color{blue}{c_{14}}$
All tiles from the $1$-st block will have colour $\color{red}{\textbf{1}}$, from the $2$-nd block will have colour $\color{blue}{\textbf{4}}$.
It is also possible to construct a path of length $9$ in this example, in which all tiles from the $1$-st block will have colour $\color{red}{\textbf{1}}$, from the $2$-nd block will have colour $\color{green}{\textbf{3}}$, and from the $3$-rd block will have colour $\color{blue}{\textbf{4}}$.
------------------------独自思考分割线------------------------
-
Q1Q2是昨天被卡了,这次都换了思路,一发过。还是得思考最原始的方式然后优化,不能只想着观察出答案。剩下3题wa3发1小时多点。
A1.
- 昨天读题漏了:0开始1结束。本质就是左右2段向中间最多覆盖各一次且不相交,能否将不同点全部覆盖。
- 0/1可覆盖点最多分别n个。n^2种覆盖情况。
- 考虑如何快速判断是否全部覆盖:前缀预处理查点数。
其实这道题观察还真可以解。
A2.
- 过程性贪心,每一步需要维护所有可选代价找最优代价。
A3.
- 贪心加结论。贪心就是把最小值移到一个数组。枚举所有数组作为这个数组即可。
- 答案就是min(a[i][0])+ sum(a[i][1])/次小值的和-a[k][1]。
A4.
- 考虑可构造出不同gcd的个数。发现为n/2个。
- 同时对于构造x,必须有x的倍数在旁边。
- 2 4 8 16 3 6 12 5 10 20 7 14 9 18 11 13 15 17 19 1
A5
- 读漏题wa2发:开始结束必须选。
- 本质就是根据首尾是否相同分为1/2块,每块是否有 $k$ 个相同的数。
------------------------代码分割线------------------------
A1.
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);cout << fixed << setprecision(6);int T = 1;cin >> T;while (T--)_();return 0;
}void _()
{string a, b;cin >> a >> b;int n = a.size();a = " " + a;b = " " + b;vector<int> l, r, pre(n + 2);for (int i = 1; i <= n; i++){pre[i] = pre[i - 1] + (a[i] != b[i]);if (a[i] == b[i]){if (a[i] == '0')l.push_back(i);elser.push_back(i);}}int f = 0;for (auto l : l)for (auto r : r)if (l < r){if (pre[r - 1] - pre[l] == 0)f = 1;}cout << (f ? "YES" : "NO") << endl;
}
A2.
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);cout << fixed << setprecision(6);int T = 1;cin >> T;while (T--)_();return 0;
}void _()
{int n, p;cin >> n >> p;struct Node{/* data */int a, b;};vector<Node> vec(n);for (auto &[a, b] : vec)cin >> a;for (auto &[a, b] : vec)cin >> b;sort(vec.begin(), vec.end(), [](Node &a, Node &b){if (a.b == b.b)return a.a > b.a;elsereturn a.b < b.b; });priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;int res = 0;for (int i = 0; i < n; i++){int w = p;if (q.size() && q.top().first < w){auto [w1, cnt] = q.top();q.pop();w = w1;if (cnt > 1)q.push({w1, cnt - 1});}res += w;q.push({vec[i].b, vec[i].a});}cout<< res << endl;
}
// 未讨论p,bi大小,仅遍历贪心不够优
// void _()
// {
// int n, p;
// cin >> n >> p;
// struct Node
// {
// /* data */
// int a, b;
// };
// vector<Node> vec(n);
// for (auto &[a, b] : vec)
// cin >> a;
// for (auto &[a, b] : vec)
// cin >> b;
// vector<int> pre(n + 1);
// sort(vec.begin(), vec.end(), [](Node &a, Node &b)
// {
// if (a.b == b.b)
// return a.a > b.a;
// else
// return a.b < b.b; });
// for (int i = 0; i < n; i++)
// pre[i + 1] = pre[i] + vec[i].a;
// int res = 0;
// for (int i = 1; i <= n; i++)
// {
// res += vec[i - 1].a * vec[i - 1].b;
// if (i + pre[i] >= n)
// {
// res += i * p;
// res -= (i + pre[i] - n) * vec[i - 1].b;
// break;
// }
// }
// res = min(res, n * p);
// cout
// << res << endl;
// }
A3.
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);cout << fixed << setprecision(6);int T = 1;cin >> T;while (T--)_();return 0;
}void _()
{int n;cin >> n;vector<vector<int>> a(n);for (int i = 0; i < n; i++){int m;cin >> m;while (m--){int x;cin >> x;a[i].push_back(x);}sort(a[i].begin(), a[i].end());}int sum2 = 0, min1 = 1e9;for (auto a : a)sum2 += a[1], min1 = min(min1, a[0]);int res = 0;for (int i = 0; i < n; i++){int t = sum2 - a[i][1] + min1;res = max(res, t);}cout << res << endl;
}
A4.
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);cout << fixed << setprecision(6);int T = 1;cin >> T;while (T--)_();return 0;
}void _()
{int n;cin >> n;vector<int> vis(n + 1);for (int i = 2; i <= n; i++)for (int j = 0; i * (1ll << j) <= n; j++)if (!vis[i * (1ll << j)]){cout << i * (1ll << j) << ' ';vis[i * (1ll << j)] = 1;}cout << 1 << ' ' << endl;
}
A5.
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);cout << fixed << setprecision(6);int T = 1;cin >> T;while (T--)_();return 0;
}void _()
{int n, k;cin >> n >> k;int cnt = 0;map<int, int> has;map<int, int> res;vector<int> a(n + 1);for (int i = 1; i <= n; i++)cin >> a[i];int f = 0, f1 = 0;for (int i = 1; i <= n; i++){int x = a[i];has[x]++;if (!f1){if (has[a[1]] == k){f1 = 1;has.clear();}}else if (has[a[n]] == k)f = 1;}if (a[1] == a[n] && f1)f = 1;cout << (f ? "YES" : "NO") << endl;
}