Idea: BledDest
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a, x, y;
cin >> a >> x >> y;
cout << ((a < x) == (a < y) ? "YES" : "NO") << '\n';
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++) {
if (abs(a[i - 1] - a[i]) <= 1) {
cout << 0 << endl;
return;
}
}
for (int i = 1; i + 1 < n; i++) {
if (a[i - 1] < a[i] && a[i] > a[i + 1]) {
cout << 1 << endl;
return;
}
if (a[i - 1] > a[i] && a[i] < a[i + 1]) {
cout << 1 << endl;
return;
}
}
cout << -1 << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (auto& x : a) cin >> x;
long long ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
int x = max(a[n - 1], 2 * a[i]) - a[i] - a[j];
int k = upper_bound(a.begin(), a.begin() + j, x) - a.begin();
ans += j - k;
}
}
cout << ans << '\n';
}
}
Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
const int N = int(2e5) + 5;
int n;
vector<int> g[N];
inline bool read() {
if(!(cin >> n))
return false;
fore (i, 0, n)
g[i].clear();
fore (i, 0, n - 1) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
return true;
}
vector<int> used;
vector<pair<int, int>> ans;
void dfs(int v, bool in) {
used[v] = 1;
for (int to : g[v]) {
if (used[to])
continue;
if (in)
ans.emplace_back(to, v);
else
ans.emplace_back(v, to);
dfs(to, !in);
}
}
inline void solve() {
int r = 0;
while (r < n && sz(g[r]) != 2)
r++;
if (r >= n) {
cout << "NO\n";
return;
}
used.assign(n, 0);
ans.clear();
ans.emplace_back(r, g[r][0]);
ans.emplace_back(g[r][1], r);
used[r] = 1;
dfs(g[r][0], true);
dfs(g[r][1], false);
cout << "YES\n";
sort(ans.begin(), ans.end());
for (auto [u, v] : ans)
cout << u + 1 << " " << v + 1 << '\n';
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t; cin >> t;
while (t--) {
read();
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution in M sqrt M (Neon)
#include <bits/stdc++.h>
using namespace std;
const int M = 5e5;
const int INF = 1e9;
int main() {
vector<int> dp(M + 1, INF);
dp[1] = 1;
for (int m = 2; m <= M; ++m) {
for (int i = 1; i * i <= m; ++i) {
if (m % i != 0) continue;
if (m / i > 2) dp[m] = min(dp[m], dp[i] + dp[m / i - 2]);
if (i > 2) dp[m] = min(dp[m], dp[m / i] + dp[i - 2]);
}
}
int q;
cin >> q;
while (q--) {
int m;
cin >> m;
cout << (dp[m] == INF ? -1 : dp[m]) << '\n';
}
}
Solution in M log M (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int N = 500043;
vector<int> divisors[N];
int dp[N], dp2[N];
int calc(int x);
int calc2(int x)
{
if(dp2[x] != -1) return dp2[x];
if(x == 1) return dp2[x] = 0;
dp2[x] = calc(x);
for(auto y : divisors[x]) dp2[x] = min(dp2[x], calc(y) + calc2(x / y));
return dp2[x];
}
int calc(int x)
{
if(dp[x] != -1) return dp[x];
if(x == 1) return dp[x] = 0;
if(x == 3) return dp[x] = 1;
return dp[x] = calc2(x - 2) + 1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
for(int i = 2; i < N; i++)
for(int j = i * 2; j < N; j += i)
divisors[j].push_back(i);
for(int i = 0; i < N; i++) dp[i] = dp2[i] = -1;
int t;
cin >> t;
for(int i = 0; i < t; i++)
{
int x;
cin >> x;
if(x % 2 == 0) cout << -1 << endl;
else cout << calc(x + 2) << endl;
}
return 0;
}
2112F - Variables and Operations
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int N = 543;
const long long INF64 = (long long)(1e18);
long long e[N][N];
long long d[N][N];
int n, m;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(i != j) e[i][j] = INF64;
for(int i = 0; i < m; i++)
{
int x, y, w;
cin >> x >> y >> w;
--x;
--y;
swap(x, y);
e[x][y] = min(e[x][y], (long long)w);
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
d[i][j] = e[i][j];
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
int q;
cin >> q;
for(int i = 0; i < q; i++)
{
long long op;
cin >> op;
vector<long long> a(n);
for(int j = 0; j < n; j++) cin >> a[j];
vector<long long> min_dist = a;
vector<long long> min_one_edge = a;
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
{
min_dist[k] = min(min_dist[k], a[j] + d[j][k]);
min_one_edge[k] = min(min_one_edge[k], a[j] + e[j][k]);
}
vector<long long> min_op(n, INF64);
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
{
if(d[j][k] < e[j][k]) min_op[k] = min(min_op[k], (d[j][k] + a[j]) - (min_one_edge[k] - 1));
}
for(int j = 0; j < n; j++)
{
if(min_op[j] <= op) cout << 1;
else cout << 0;
}
cout << endl;
}
return 0;
}








Tutorial for F shows the one for A.
Very nice contest, thanks!
Whoops. Sorry, fixed
why the contest is unrated? why? I expected to become blue, but it is unrated!
Congratulations, you are blue now
thank you!
Living room is on the left
I guess by mistake you have put the editorial of A in F ;-;
rating changes?
when we get our rating?
I have another interesting solution for E since I couldn't come up with that dp idea. My solution brute forces and generates all the possible answers for n (vertices count) and doing this for all n <= 24 seems enough and if you can implement it carefully it will pass the time limit. Code.
I tried to do similar but got tle after 20
did anyone else not have his rating change, because I did and when I checked it said that it was unrated for me but I am under 2100 and I participated as rated
I also want to know why the ratings are not update yet.
has contest been made unrated due to plagiarism ?
Are you sure or is this just a guess
I am asking
How to check ? I remember I did participate as rated, but I cannot find me in common standings, so strange ...
The problems are very nice, especially for B,E and F :)
325787868 what was wrong with my logic?
try this test case: 1 5 3
answer is 1
lmao got it
Thank you for a wonderful contest, D was beautiful. I was wondering how to count number of good pairs given a directed tree as in D
I think this works :
Given the directed tree, you can count for each node, the number of in-edges (to the node) times the number of out-edges (from the node). Summing it altogether gives the number of paths of length 2.
And if it's equal to 1, then it works, else it doesn't work.
Proof : If it's equal to 1, then there cannot exist a path of length >= 3, because for example if you have a->b->c->d, then you have clearly more than 2 paths of length 2 (a->b->c, b->c->d). So there is exactly one path of length >= 2, so it works. And if not, it clearly doesn't work.
Thank you for this. I am sorry I was not clear but my query was regarding any type of directed tree. Is there an efficient way of counting number of good pairs.
I have thought about it, and I think this works :
You do a DP on the tree (you can root it in any way).
For each node, you look at its subtree and you try to calculate the following three values :
$$$end[node]$$$ -> number of paths that end at the node;
$$$start[node]$$$ -> number of paths that start at the node;
$$$tot[node]$$$ -> the total number of paths;
Now, you can calculate these values recursively :
Suppose you need to calculate the three values of a node $$$r$$$, and you already know the values for his $$$k$$$ children.
Separate these children into two categories :
Then,
For $$$tot[r]$$$, you should count all the $$$tot[a_i]$$$ and $$$tot[b_i]$$$ and $$$end[r]$$$ and $$$start[r]$$$, and you should count all paths that originate from a subtree of $$$a_i$$$ to a subtree of $$$b_i$$$.
This gives
Finally you have
Hopefully it works, and my complexity is $$$O(n)$$$ !
Can you describe what are you trying to calculate exactly? Because if you're counting the number of paths then it's just $$$\sum_{v}{start[v]}$$$ because every path has a starting vertex.
When I say $$$start[v]$$$, $$$end[v]$$$, $$$tot[v]$$$, I only consider the number of paths in the subtree of $$$v$$$, not in the whole tree !
Why not to calculate $$$start[v]$$$ as the number of paths in the whole tree starting at $$$v$$$?
You can just calculate $$$start(v) = \sum\limits_{v \to to}{(1 + start(to))}$$$ recursively without any problems since there are no cycles in the graphs.
This is smart; I wasn't smart enough to find this short smart idea ...
Can someone help me debug the solution for D problem
My Approach is to first assign the direction layer by layer using BFS. If one layer has upward direction of edges then the next layer would have downward direction and so on. this would ensure we have exactly (n — 1) pairs reachable.
Next after this i am finding out the node having degree 2 and has one of the child as leaf node and then reverse its direction to make the 2 length path.
Submission Link :- https://mirror.codeforces.com/contest/2112/submission/326094031
You only need a single condition that if a node exists with degree 2, then simply mark it as the root, then assign edges separately for left and right using BFS/dfs. this left -> root and root ->right will give 3 pairs, and the rest pairs by that alternate assigning of edges
your code fails for this kind of cases it says "NO" but its possible, see the image for the edges
Forgot to use long long… lesson learned.
canonical event, dont worry
I wonder how to use two pointers to improve the time complexity of $$$C$$$ to $$$O(n$$$$$$2$$$$$$).$$$
Check this out
Thanks a lot.
32595261 Hey can anyone tell why this is failing. I modify the for loop to seperate the logic to check for 2 consecutive elements and 3 consecutive elements and it starts working. I wanted an example testcase for which this code wont work. Thanks!
Check for this input:
1 4 1 3 1 1
The answer should be 0, but your code prints 1.
I can tell you the reason but I want you to find out yourself first with the help of the testcase.
t = 1 n = 4 and arr = 1 3 1 1
Thanks, got the reason. Pretty silly of me to try optimizing it unnecessarily.
Very good contest, thanks.
Has this contest become unrated?
...
how much more time (╥﹏╥)
When the rates update
Edi not linked on contest page
So when will rating update?
Heh just when?? it's has been so longgg
Will the ratings not get updated.
Does anyone could say something about rating changes? If it's been a problem or it's in process, etc. We would like to know something please
325760640
I tried this approch in problem A and it works
Can someone explain me this part better for E?
why is this tree illegal? (green)-(blue)-(green)
This is also the third example of the pretest 1, but I don't understand why this coloring has to be excluded
just understood this, both green and yellow are reachable without blue means green-green, yellow-yellow and yellow-green
Thanks, this was helpful. I was scrolling through the comments to see if someone had the same issue.
I have the same issue... Why it is not considering that case??
If we satisfied second condition with (green)-(blue)-(green), then third condition does not works... Because "all yellow and green vertices" should be reachable from each other without blue vertices. it's not (green)-(green) or (green)-(yellow)-(green).
Can anyone explain why we need to add dp[x-2] i.e. dp[m] = dp[m/x] + dp[x-2] in E.
dp[x] means number of vertices needed to form x colorings now we are dealing in subtrees so there are 2 more options of coloring them entirely blue or yellow which don't exist in dp[x] so if we find the number of vertices which can be colored in x-2 ways we can do the other two colorings and get x
when will rating change
Hello nigga I have question .I am new to codeforces I just want to know how u contribution is +3
just write comments and hope for the best lol
is rating updated for educational round div 2
Cana anybody tell that why this is not working for problem C
I think from what I can tell you have not considered the case when the sum of the three elements chosen by Alice is less than the max element which can be chosen by Bob. For example if you have an input of the form below it will fail because Bob will just color the biggest element blue :(
1
5
1 1 1 2 5
For the above test case your code says 1 but the actual answer is 0 since Bob can always choose 5 to color blue and win regardless of Alice's choice.
Nvm I realized my counter example actually works for your case my bad idk what went wrong.
Edit: if (index>j && a[i]+a[j]+a[index]>a[n-1]) { ans+=(index-j); }
This does not work because a[i]+a[j]+a[index]>a[n-1] is not necessarily true for the indexes which are between j and index that you add to the ans variable. For example with the list 27 32 35 37 45 96, When you consider i=0, j=1 you get that a[0]+a[1]+a[4]>a5 and you consider (0,1,2), (0,1,3), (0,1,4) all as valid options while only (0,1,4) index triplet works.
my code is giving 0 only , could you pls check it once again
you are checking if a[i] + a[j] > a[index], But it is possible for some indexes that this condition holds for index<n-1 but a[i]+a[j]+a[index] <= a[n-1].
Consider this test case :
suppose i = 0 (a[i] = 17) and j = 1 (a[j] = 20) then according to your code index = 4 and it is also satisfying index>j and a[i]+a[j]+a[index]>a[n-1] and you are adding (index-j = 3) to your ans or possible index for i = 0, j = 1 are 2,3,4 but index 2 will not work as a[0]+a[1]+a[2] = 62 < a[n-1] = 65
correct approach :
calculate index2 < n-1 for which all k such that index2 <= k < n will satisfy a[i]+a[j]+a[k]>a[n-1]
Use upper bound for calculating this.
in for loop change the ranges i = 0 to n-3 and j = i+1 to n-2 , with slight changes in your code here is the accepted version :
Thanks a lot mistryman for pointing out my mistake .
Thanks for the editorial
In D I have a doubt!!!!!, I have No; in test 2 case 1507 (hidden case), the thing is as per editorial, the only sufficient condition for n good pairs is to have a vertex with 2 components, but I think we need a vertex with 2 components and one of those component should be 1 degree (connected to only 1 component). I am checking that and if not found output 1.
Because as per my logic, if lets say I have a component of 2 degrees connected to each other, no matter what dirc I change, I will always create more paths than n. So I don't know why.
Can someone give me a test case where this is true!!!!!
My code for ref, if needed here
Have you checked MOSS yet?
Drop the ratings please :)
Hello Codeforces Team,
I have received a message that my solution for problem 2112B (#325782053)coincides with many others. I want to clarify that I wrote my code entirely by myself and did not copy it from any source, nor did I share it with anyone.
I used VS Code, and I didn’t post or share my solution publicly. If the coincidence happened due to unintentional exposure (like online IDE visibility or similar logic), I sincerely apologize and assure you that I’ll be more careful in future contests.
Please reconsider the decision if possible, or let me know if there’s anything I can do to provide more details.
Thank you. tanwarbrijessh
I am absolutely stunned by problem E and its solution
A very beautiful problem indeed.
Does anyone have more problems that uses similar arguments?
I got the following message :
"Attention!
Your solution 325720028 for the problem 2112B significantly coincides with solutions LuzZ_ShayanZ/325720028, khanna_dhruv/325731757. Such a coincidence is a clear rules violation. Note that unintentional leakage is also a violation. For example, do not use ideone.com with the default settings (public access to your code). If you have conclusive evidence that a coincidence has occurred due to the use of a common source published before the competition, write a comment to post about the round with all the details. More information can be found at http://mirror.codeforces.com/blog/entry/8790. Such violation of the rules may be the reason for blocking your account or other penalties. In case of repeated violations, your account may be blocked."
I would like to clarify that I did not engage in any form of cheating or code sharing during the contest. I wrote my solution independently using VS Code on my personal computer, without using any online IDEs or shared environments. The problem was quite straightforward and naturally leads to a limited set of logical steps — such as checking adjacent differences and detecting local extrema — which explains the similarity in approach.
All my problems A to D are skipped. This was a big bump in my rating so please help me or guide me what to do.
Does dp[m/x] + dp[x-2] imply that no subtree has more than 2 children? I understand why we can use dp[x-2], but doesn’t dp[m/x] represent the minimum vertices in a single tree with that many colorings? Why can’t the optimal tree have many children whose coloring product equals m/x? Does dp[m/x] work in this case? Edit: never mind, I believe it is because dp[x-2] accounts for the last subtree and dp[m/x] accounts for the root + remaining subtrees. Please correct me if I am wrong.
Nice problems, thanks!
I want to share a bit different approach on problem E.
C(T)is number of ways to color treeT.Cs(n)is set of allC(T)among treesTwith exactlynvertices.Let's find
Cs(n)for alln's that can be the answer. It can be calculated using dynamic programming like this:And then if you print the sizes of
Cs[n], then you can notice that it stops growing aftern = 25. So you don't need to iterate for more than that. Then you can simply find the smallestnfor any number of colorings.I don't know the complexity of this solution, but it is fast enough to pass the testcases with 546 ms. The submission can be found here: 326206231.
Some observations for problem E:
m must be a an odd number. why?
Let cnt(v) denote the no. of colorings of the root v of a subtree given that it is green.
If it’s children are u1,u2,…,uk then they have cnt(ui) + 2 total colourings. +2 for all blue and all yellow cases.
Since all subtrees are independent, cnt(v) = (cnt(u1)+2)(cnt(u2)+2)…(cnt(uk)+2)
Consider the base case of a leaf node, it has 3 possible colorings(i.e. odd).
and any answer that is formed from these base cases is a product of odd numbers which is also odd. So, m must be odd.
If m is a power of 3 then the tree with smallest number of nodes is the tree with a root attached to n leaves where n is the power of 3, so n+1 in total.
Using a linear chain we can create all odd number of colorings starting from 1. //Edit : please consider the following to be void for now.
For any given m, find it’s prime factorisation and write it such that the product shows all factors distinctly. E.g. 75 = 5*5*3.(not 5^2*3)
Then the optimal tree is the root connected to n linear chains whose no. of possible colorings are equal to the prime factors(we can have any prime no. using a chain). where n is the total no. of prime factors, considering recurring equal factors to be distinct.
You can simply determine the no. of nodes in each children chain and form the total answer. I guess the dp solution achieves this in an easier way.
Can you please briefly explain how to calculate the number of nodes in each child chain and how it contributes to the final answer?
For a chain of length n, it has 2n-1 colourings. If component chains are of length n1,n2,n3...nk Then for the final tree ans is m =(2n1+1)(2n2+1)...(2nk+1). I have written 2ni+1 instead of 2ni-1 because the 2 cases of all blue and all yellow for component chains are added too.
Please up-vote.
So, according to you if m itself is a prime number then the optimal tree will be the root connected to 1 linear chain and the number of total nodes in that tree will be (m / 2). Correct me if i'm wrong.
If that's what you are saying then i guess you are wrong. Cause for m = 11, the ans is 4 not (m / 2). And the tree will be {(1,2), (2,3), (2,4)} which is not a linear chain in my knowledge.
Yes, I guess there is some error in the reasoning. I'll think about it but it seems close.
Your guess about the tree being a bunch of linear chains connected to the root isn't correct. You can see this with m = 11, 13, 17, or 29. This is because as soon as you have a tree that isnt a chain (like with m=9) you can construct trees by attaching those non-chain trees to a common root. For example with m=29, you use the optimal tree for m=27 but with an extra node above the root
hello biru
I am still not able to understand C solution. I got it that lower bound is to figure out the range, but not the particular conditions!. Can someone explain me? Stuck for four days!
for shrinking array B Question
For problem E, I'm confused on the line of the editorial "if a vertex is not green, its entire subtree must be colored the same color". If I am coloring a tree with only green and blue (i.e., not coloring any node yellow so there are no yellow-green paths), can't I have green nodes in the subtree of a blue node?
Ah never mind, after seeing the clarification and re-reading the problem statement, I realize that all green nodes must be reachable from each other without passing through blue nodes from the third condition.
we share the same brain cell
E is such a good problem
Can anyone help me to clear the doubt for tutorial A? Suppose a,x,y=1,2,3 then is there any position for Bob to get the prize always? Even though he chooses 4 as position he will never be able to get the prize at x=2 because a is at 1. The questions asks for a guaranteed position for Bob not that position where he may win conditionally. I may be wrong can anyone help me with this?
Shouldn't the correct condition should be: ~~~~~ int dist = min(abs(a — x), abs(a — y)); int side = (x — a) * (y — a); if (dist > 1 && side > 0) cout << "YES" << endl; else cout << "NO" << endl; ~~~~~ At least a distance of 1 so,that Bob can take that position as Alice and Bob cannot be at same position.
Bob starts at position 2. "Bob can choose any integer point, except for a (in particular, he can choose to start in point x, point y, or any other point, but not a)."
Yeah,true. I understood it. Thanks again.
Hi, I am currently practicing the problems in this contest & I seem to have gotten stuck on problem D. I have implemented the solution in Python based on the approach in the editorial.
https://mirror.codeforces.com/contest/2112/submission/327620250
However, I keep getting runtime error on the 9th test case, & it beats me as to what the issue might be. Would be lovely if I could get some pointers on this one.
Problem D seemed almost to be a copy of a previous problem https://mirror.codeforces.com/problemset/problem/1144/F but anyway Thanks for contest !
I suggest to improve the description of the problem F. 'The variable $$$a_{x_i}$$$ gets assigned' or 'The $$$x_i$$$-th variable gets assigned' not 'the variable $$$x_i$$$ gets assigned'. BledDest
Can anybody tell that why this code is giving wrong answer for problem C(2112C — Coloring Game)
For problem c, here is the two pointer implementation
My solution for C iterate to each i and j. Let a = arr[i] ,b = arr[j],sum = a + b Now for the third elements will be between j+1 and the last element.
So now the third element (let it be c) should meet these conditions — a + b + c > arr[n-1] and a + b > c We can find these ranges with the help of lower bound and upper bound. And by taking the intersection for each case, we add to the answer variable to get the answer. Time complexity = (n2logn)