Идея: kbats183
Решение
Tutorial is loading...
Код
def solve():
n, m = [int(i) for i in input().split()]
ans = 0
for i in range(n):
l = input()
if len(l) <= m:
m -= len(l)
ans += 1
else:
for i in range(i + 1, n):
input()
break
print(ans)
t = int(input())
for i in range(t):
solve()
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
long long ods = 0, evs = 0;
for (int i = 0; i < n; i++) {
if (i & 1) ods += a[i];
else evs += a[i];
}
int odc = n / 2, evc = n / 2;
if (n & 1) evc++;
if (ods % odc != 0 || evs % evc != 0 || ods / odc != evs / evc) {
cout << "NO";
return;
}
cout << "YES";
}
int main() {
int TESTS; cin >> TESTS;
while (TESTS --> 0) {
solve();
cout << '\n';
}
return 0;
}
Идея: DanGolov
Решение
Tutorial is loading...
Код
def solve():
s = [int(x) for x in list(input())]
sm = sum(s)
twos = s.count(2)
threes = s.count(3)
for i in range(min(10, twos + 1)):
for j in range(min(10, threes + 1)):
if (sm + i * 2 + j * 6) % 9 == 0:
print('YES')
return
print('NO')
t = int(input())
for _ in range(t):
solve()
2050D - Digital string maximization
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s; cin >> s;
for (int i = 0; i < s.size(); i++) {
int best = s[i] - '0', pos = i;
for (int j = i; j < min(i + 10, (int) s.size()); j++) {
if (s[j] - '0' - (j - i) > best) {
best = s[j] - '0' - (j - i);
pos = j;
}
}
while (pos > i) {
swap(s[pos], s[pos - 1]);
pos--;
}
s[i] = char(best + '0');
}
cout << s;
}
int main() {
int TESTS = 1; cin >> TESTS;
while (TESTS --> 0) {
solve();
cout << '\n';
}
return 0;
}
Решение
Tutorial is loading...
Код
#include <iostream>
#include <algorithm>
static const int inf = 1e9;
void solve() {
std::string a, b, res;
std::cin >> a >> b >> res;
int n = (int) a.size(), m = (int) b.size();
int dp[n + 1][m + 1];
std::fill(&dp[0][0], &dp[0][0] + (n + 1) * (m + 1), inf);
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
dp[i + 1][0] = dp[i][0] + (a[i] != res[i]);
}
for (int j = 0; j < m; j++) {
dp[0][j + 1] = dp[0][j] + (b[j] != res[j]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = std::min(dp[i - 1][j] + (a[i - 1] != res[i + j - 1]),
dp[i][j - 1] + (b[j - 1] != res[i + j - 1]));
}
}
std::cout << dp[n][m] << std::endl;
}
int main() {
int tests;
std::cin >> tests;
while (tests--) {
solve();
}
}
2050F - Maximum modulo equality
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
const int LOGN = 20;
vector<vector<int>> stGCD;
int get_gcd(int l, int r) {
int k = __lg(r - l + 1);
return __gcd(stGCD[k][l], stGCD[k][r - (1 << k) + 1]);
}
void solve() {
stGCD.clear();
int n, q; cin >> n >> q;
vector<int> a(n);
for (int &x : a) cin >> x;
vector<int> b;
for (int i = 1; i < n; i++)
b.push_back(abs(a[i - 1] - a[i]));
stGCD.resize(LOGN, vector<int>(b.size(), 1));
for (int i = 0; i < b.size(); i++)
stGCD[0][i] = b[i];
for (int i = 1; i < LOGN; i++)
for (int j = 0; j + (1 << (i - 1)) < b.size(); j++)
stGCD[i][j] = __gcd(stGCD[i - 1][j], stGCD[i - 1][j + (1 << (i - 1))]);
while (q--) {
int l, r; cin >> l >> r;
if (l == r) {
cout << 0 << " ";
continue;
}
l--; r -= 2;
int gcd = get_gcd(l, r);
cout << gcd << " ";
}
}
int main() {
int TESTS = 1; cin >> TESTS;
while (TESTS --> 0) {
solve();
cout << "\n";
}
return 0;
}
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
void dfs(int v, int p, vector<vector<int>> &sl, vector<pair<int, int>> &dp){
dp[v].x = sl[v].size();
int m1 = -1, m2 = -1;
for(int u: sl[v]){
if(u == p){
continue;
}
dfs(u, v, sl, dp);
dp[v].x = max(dp[v].x, dp[u].x + (int)sl[v].size() - 2);
m2 = max(m2, dp[u].x);
if(m1 < m2) swap(m1, m2);
}
dp[v].y = dp[v].x;
if(m2 != -1){
dp[v].y = m1 + m2 + sl[v].size() - 4;
}
}
void solve(int tc){
int n;
cin >> n;
vector<vector<int>> sl(n);
for(int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
sl[--u].emplace_back(--v);
sl[v].emplace_back(u);
}
vector<pair<int, int>> dp(n);
dfs(0, 0, sl, dp);
int ans = 0;
for(int i = 0; i < n; ++i){
ans = max(ans, max(dp[i].x, dp[i].y));
}
cout << ans;
}
bool multi = true;
signed main() {
int t = 1;
if (multi)cin >> t;
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}








Task G is almost identical to a task from the Polish Olympiad in Informatics called Parade. The only difference is in the POI task, you can not choose $$$a = b$$$.
for those who love vjudge
is the same as
No. For example, $$$n = 6$$$. In the first case
6 / 2 = 3, then3 & 1is true andevc = 6 / 2 + 1 = 4. But in the secondevc = (6 + 1) / 2 = 3.I think you are incorrect, because if $$$n = 6$$$, then it is be 6 & 1 not 3 & 1, so for $$$n = 6$$$ it should give $$$3$$$, so Karim__2 formula is indeed correct.
Oh, ok. Idk, I saw $$$n / 2$$$ instead of $$$n$$$
Where is the formal proof of correctness to the editorial solution of D?
I was hoping to see the cleanest solution and proof to the problem, now the editorial for this problem is very unhelpful.
Maximum digit can be 9. After each operation digit will decrease by 1, so we can do at most 9 operations. What is unclear?
Why isn't the editorial's solution clean?
My understanding of the editorial’s argument is as follows:
First, we aim to maximize the digit at index $$$0$$$, and it’s clear that we need to choose a digit from the first 10 indices. The goal is to find the maximum value of $$$S_i−(i−j)$$$, where $$$j$$$ is the index we are currently maximizing (starting with zero). In case of ties, we select the smallest $$$i$$$. We swap $$$i$$$ and $$$j$$$, and then repeat from the next index. This ensures that $$$s$$$ is maximized.
Is this reasoning incorrect, or is it just informal?
The editorial solution is clean. However, I cannot see a potential clean proof.
Baiscally, the first step is defintiely correct, but the tie breaking is hard. You must show that among all future paths, you can select one to commit to, and it gives the best result (due to one reason or another).
This cannot be done without any statements about the future situations, what you can do/comparing future situations.
That said, the proof given by _Kee below is correct and clean enough. Thanks.
I think the hard part is how to handle tiebreaking.
Let's say we have an array $$$[a, b, 6, c, d, 9, \ldots]$$$ where $$$a \lt 4$$$, $$$b \lt 5$$$, $$$c \lt 7$$$ and $$$d \lt 8$$$, that is, $$$6$$$ and $$$9$$$ are the only digits that we can bring to the front. If we bring $$$6$$$ to the front, we get $$$A = [4, a, b, c, d, 9, \ldots]$$$. If we move $$$9$$$, we have $$$B = [4, a, b, 6, c, d, \ldots]$$$. So the problem here is: which is better?
Let's think about the selection of later digits. First of all, the parts $$$[a, b]$$$ and $$$[\ldots]$$$ are shared in the same place between $$$A$$$ and $$$B$$$, so they equally affect the final result. Next, regarding the part $$$[c, d]$$$, $$$A$$$ has that part earlier than $$$B$$$, so $$$A$$$ will give a better result. Finally, about the parts $$$[9]$$$ and $$$[6]$$$, if we move $$$9$$$ back to the digit where $$$6$$$ resides, it will become $$$7$$$, so $$$A$$$ will give a better result, too. Therefore, taking $$$A$$$ is always advantageous for us overall.
I think we can get a general proof out of this sketch, though it can be very tedious to do.
I have a question about how to arrive at the correct conclusion for a problem, for example, Problem B. That is, how to reach that solution—what was the clue in the problem that led to solving it in that way? I find it difficult to arrive at such a conclusion.
What I mean is, how should I study to improve this? Is it just about practicing problems, or does it require a more mathematical way of thinking? If anyone reads this, I would appreciate it if they could recommend a book or any resource. :(
Well, when I first came up with this problem, I thought about what will happen, if I will aim to make all the elements = 0, then I found out, that we can transfuse all the values to the first and the second elements, so after that we need to transfuse from these two elements back to othe elements and also now aim to make them all equal. Maybe it was a little bit messy idea, but it helped to solve this one out
I am unable to understand the language of E and cant think of any approach i thought may be 3 loops as time is 2.5S but still dont understood minimum number of characters of C w.r.t to all strings we can make ? how can i know there can be many string can be formed. Anyone pls just point me in right direction. thanks
I feel that his code is totally messy, and this code could help you understand that. 295017055
A fact we know is that $$$\vert C \vert = \vert A \vert + \vert B \vert$$$, so for every character of $$$C$$$, you can know it must come from $$$A$$$ or $$$B$$$. Then we only need to use state $$$A$$$ and $$$B$$$ to calculate the answer.
Let $$$dp[i][j]$$$ denote the minimum times of transforming with used first $$$i$$$ characters of $$$A$$$ and first $$$j$$$ characters of $$$B$$$. If $$$C[i + j]$$$ could be $$$A[i]$$$ or $$$B[j]$$$, we don't need to transform. Otherwise, raise $$$1$$$ by transforming a letter.
So the main idea is to represent more states with fewer state measures. Hope this will help.
Who can help me why I had a WA2, problem E? https://mirror.codeforces.com/contest/2050/submission/295355937
For problem D, I used
insert()anderase()functions to modify the string and it gave me TLE on testcase 7, later, I changed my solution to keep track of what characters from the original string are moved to their left using an array and kept adding such characters to an empty string. I think my current solution isO(n)but it got a TLE on testcase 8. Can someone tell why I got TLE? Submission linkWe can use
__gnu_cxx::rope<char>instead ofstd::stringand it passes: 295422598. Rope is a non-standart string implementation based on the treap with implicit keys. Forstd::string,insert()anderase()work in $$$O(n)$$$ in the worst case, where $$$n$$$ is a length of string. For__gnu_cxx::rope<char>,insert()anderase()work in $$$O(\log n)$$$ in the worst case, where $$$n$$$ is a length of string.Damn, I didn't know about this. Thanks!
For C, will not be time complexity O(x*x) in the worst-case scenario where x is the length of the number? When the count of both 2s and 3s becomes equal to x/2. How is it getting accepted for a constraint of 10^5 on number length?
Can anyone clarify?
You don't have to consider nine or more $$$2$$$s or $$$3$$$s because the effect of those digits loop after the ninth one (because we calculate numbers modulo $$$9$$$). So you only need to consider $$$9^2 = 81$$$ cases at most.
Could you guys help me with question G? I got the wrong answer for test case 17. https://mirror.codeforces.com/contest/2050/submission/295318789
https://mirror.codeforces.com/contest/2050/submission/295436206
Your solution with a slight change.
Do let me know if you can't figure it out.
I got it now. Thanks a bunch!
template contest.
Problem C:The only extra piece of proof that I wish should be added is that, after calculating thenum2s and num3sin the string. We can loopat most 9 times for each of twos, threes. As anything after this would be rudimentary as it would be divisible by 9 directly, so the loop should run fromi = [1 to min(num2, 10LL)), j = [1 to min(num3, 10LL)). As, someone might think why running loop(num2*num3)times per test case doesn't add up toTLE, this is cause we would be checking for atmost81 different states, which is of constant time complexity, and breaking out after it.Implementation: 296305754
Hi, thanks for this proof. one follow up doubt- how did we determine after 9 times it would be pointless to check? Also in the editorial it says it is useless to check after 8 times i.e replacing both 2 and 3 eight times. Am i missing something? would also kindly provide a small example for that, it would be easier to understand. Thanks!
After 9, it would be a multiple of 9, so further that it's pointless like say you determined the numbers to be (10,11) but you would have crossed (9,9) on some point where it would be 9*(some factor), so it's like the dead end!
can someone hack me please Problem G 296473359
I Solved It greedly by picking node lca(a,b) which called root which have max number of edges
After that I Picked greedly node a which the node that have max number of connected components with lca(a,b)
After that I pick node b which have max number of connected components with node b
That's also one of the correct solutions. In the editorial it is said, that this problem is kinda similar to finding the diameter of the tree, so your solution is just another way to find a diameter.
Editorial for G is too complicated. Dp on trees wasn't needed and was too advanced for this. Rather just root the tree at the node with max degree, reduce the degree of each node by 2 except for the root and find the max degree sum path for all the children of the root and add the top 2 max Paths.
My submission — 304341864
Why in the problem F we just take gcd(abs(a[l] — a[l + 1]), abs(a[l + 1] — a[l + 2])...), but we just checked not all pairs, cause m must be the divisor for all abs(a[i] — a[j]) l <= i <= r and l <= j <= r, so that i != j,
We need not iterate the count of 3 8 times but rather 3 times at max. Greater than 3 resets the number modulo 9.