I hope everyone enjoyed the tasks, and thank you for participating.
Editorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
sort(x.begin(), x.end());
if (x[0] % 2 == x[n - 1] % 2) {
cout << 0 << endl;
return;
}
int left = n, right = n;
for (int i = 1; i < n; ++i) {
if (x[i] % 2 != x[0] % 2) {
left = i;
break;
}
}
for (int i = 1; i < n; ++i) {
if (x[n - i - 1] % 2 != x[n - 1] % 2) {
right = i;
break;
}
}
cout << min(left, right) << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
Editorial
Tutorial is loading...
Solution
#include "bits/stdc++.h"
#define int long long
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define em emplace_back
#define mp make_pair
#define F first
#define S second
using namespace std;
template<class C>
using vec = vector<C>;
using vi = vector<int>;
using vpi = vector<pair<int, int>>;
using pii = pair<int, int>;
void solve() {
string s;
cin >> s;
int n = s.size();
int bal = 0;
for (int i = 1; i + 1 < n; i++) {
if (s[i] == '(') bal++;
else bal--;
if (bal < 0) {
cout << "YES\n";
return;
}
}
if (bal == 0) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
signed main() {
int tt;
cin >> tt;
while (tt--) {
solve();
}
}
Editorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> d(n);
for (auto &x : d) {
cin >> x;
}
vector<int> l(n), r(n);
for (int i = 0; i < n; ++i) {
cin >> l[i] >> r[i];
}
int left = 0;
vector<int> last;
for (int i = 0; i < n; ++i) {
if (d[i] == -1) {
last.push_back(i);
} else {
left += d[i];
}
while (left < l[i]) {
if (last.empty()) {
cout << -1 << '\n';
return;
}
d[last.back()] = 1;
++left;
last.pop_back();
}
while (left + last.size() > r[i]) {
if (last.empty()) {
cout << -1 << '\n';
return;
}
d[last.back()] = 0;
last.pop_back();
}
}
for (auto &x : d) {
cout << max(0, x) << " ";
}
cout << "\n";
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Editorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 11;
struct edge {
int t, w;
edge(int t, int w) : t(t), w(w) {}
};
void solve() {
int n, m;
cin >> n >> m;
vector<int> b(n);
for (auto &x : b) {
cin >> x;
}
vector<vector<edge>> graph(n);
for (int i = 0; i < m; ++i) {
int s, t, w;
cin >> s >> t >> w;
--s; --t;
graph[s].push_back(edge(t, w));
}
auto check = [&](int maxW) {
vector<int> best(n, 0);
for (int i = 0; i < n; ++i) {
if (i > 0 && best[i] == 0) {
continue;
}
best[i] += b[i];
best[i] = min(best[i], maxW);
for (auto p : graph[i]) {
if (p.w <= best[i]) {
best[p.t] = max(best[p.t], best[i]);
}
}
}
return (best.back() > 0);
};
if (!check(INF)) {
cout << -1 << endl;
return;
}
int l = 0, r = INF;
while (r - l > 1) {
int mid = (l + r) / 2;
if (check(mid)) {
r = mid;
} else {
l = mid;
}
}
cout << r << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Editorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
vector<set<int>> graph;
vector<int> ans;
void dfs(int v) {
while (!graph[v].empty()) {
int u = *graph[v].begin();
graph[u].erase(v);
graph[v].erase(u);
dfs(u);
}
ans.push_back(v);
}
signed main() {
int t;
cin >> t;
while (t--) {
graph.clear();
ans.clear();
int n;
cin >> n;
map<int, int> p, v;
map<pair<int, int>, int> toIndex;
vector<pair<int, int>> part;
for (int i = 1; i <= n; ++i) {
int volume, pitch;
cin >> volume >> pitch;
toIndex[make_pair(volume, pitch)] = i;
if (p.count(pitch) == 0) {
p[pitch] = graph.size();
graph.push_back(set<int>());
part.push_back(make_pair(0, pitch));
}
if (v.count(volume) == 0) {
v[volume] = graph.size();
graph.push_back(set<int>());
part.push_back(make_pair(volume, 0));
}
graph[v[volume]].insert(p[pitch]);
graph[p[pitch]].insert(v[volume]);
}
int root = 0;
int cnt = 0;
for (int i = 0; i < graph.size(); ++i) {
if (graph[i].size() % 2 == 1) {
++cnt;
root = i;
}
}
dfs(root);
if (ans.size() != n + 1 || cnt > 2) {
cout << "NO" << endl;
continue;
}
vector<int> out;
for (int i = 0; i < n; ++i) {
auto p1 = part[ans[i]];
auto p2 = part[ans[i + 1]];
out.push_back(toIndex[make_pair(p1.first + p2.first, p1.second + p2.second)]);
if (out[i] == 0) {
out.clear();
break;
}
}
if (out.empty()) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
for (int i = 0; i < n; ++i) {
cout << out[i] << " ";
}
cout << endl;
}
}
return 0;
}
Editorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int f(int x, int y) {
return (x % y) + (y % x);
}
void Solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int ans = 0;
int mx = a[0];
for (int i = 0; i < n; ++i) {
ans = max(ans, f(mx, a[i]));
if (a[i] > mx) {
if (a[i] >= mx * 2) {
mx = a[i];
for (int j = 0; j < i; ++j) {
ans = max(ans, f(a[i], a[j]));
}
} else {
mx = a[i];
ans = mx;
}
}
cout << ans << ' ';
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
Solve();
}
return 0;
}








I don't understand how E was accepted by coordinators since it is so standard to anyone who's ever seen Hierholzer's before.
What's wrong with the fun exercise in the div2 round?
It is not fun
The reduction is quite easy and then you need to copypaste template (which most people dont even have)
Also for no reason a[i] <= 1e9 so you need even more impl :)
This is only yours opinion)
mine as well
mine too
mine too. After contest, so many people tell me that E is a "template-problem".
Bro, why do you want to copypaste 15 lines of code?
Why would I not want to copypaste 15 lines of code?
You complain for bitset which is 1 line :)
bro once you do same for div 4 problem and now you are complaining quite contradictory
he hasn't authored a single div4 meanwhile
Honestly I couldn't find the reduction until the end. Maybe it's a skill issue on my side idk but I felt the idea was nice (maybe also because it's the first time I encounter something like that)
The reduction definitely isn't easy, as you need to make a couple of observations to bring this task to the graph one. This greatly fits for the position of E
Put that in div4, I'll (begrudgingly) say it's just about OK. Div2, no.
ohh, we really didn't think about it, but you have opened our eyes to this problem. from now on, we won't make such mistakes again and will definitely take your opinion into consideration.
I think we can put this problem in Edu round to get more "fun".
Isn't the algorithm for directed graphs?
nah it works for both
I was not able to debug my code for finding Eulerian path, damn sed lyf :(
can u debug why my code is failing 321213790
2110D - Fewer Batteries can be solved without binary search too.
Solution is that ->
First for each index store the minimum batteries that will be required from that point so that one is able to reach the endpoint(nth node). That is done by dijisktra on the graph with opposite edge starting from node n and cost as maximum of all the nodes visited .
After that ,minimising the cost with normal dijisktra on forward graph starting with node 0 and cost as maximum of all the nodes visited.
U can look to my submission 321152832
I thought of this idea too before I realized that $$$s_i \lt t_i$$$
I used topological sort 2 times
Submission: 321166795
Complexity: O(2 * N)
The second topological sort isn't correct because you assume that in each node
vyou can have any number of batteries betweenl[v]andr[v]but this is not correct. Here is an example testcase where the solution fails.In the above example the problem is for node 3 you have
l[3] = 2r[3] = 10and you assume you can reach node 3 with 3 batteries and then walk the(3, 5)edge which has weight 3. However you can reach node 3 with either 2 or 10 batteries and nothing in between.Another thing which is interesting is that the two dijkstras solution also fails but with an output of 100, I explained why in one of the other replies.
Edit: Also, you don't need to manually toposort.
[1, 2, ..., n]is a valid toposort of the vertices since for each edge (u, v) is holds that u < v from the problem statement.in your code: "if (minos[i] > v[1] + arr[i]) continue;"
isint that wrong? you cant just prune based off of that. you could have taken some more batteries before reaching the maximum edge in that path right? how are you certain that you can eliminate that path completely for finding minimax?
This lines assures that if the path i am going in is having current batteries less than the minimum required i shouldn't go to that path i.e doesn't minimise the maximum for that because since it has less batteries and I need minimum minos[i] to be able to reach the end , so I will not be able to reach the nth node with these batteries.
You are correct, it's indeed wrong.
if (w > v[1])is also wrong for the exact same reason. Here is an example where the solution breaks.In the above example the problem is that node 3 is first reached with 2 batteries and it's assumed the edge
(3, 5)which has weight 3 can not be traversed. However node 3 can also be reached with 10 batteries and this is optimal.damn nice i wasnt able to think of the first part cuz of that had to do some fluke binary search solution with dijkstra and now my accepted solution gives mle on 31
In the first dijkstra in your submission it seems like you can pop a not fully relaxed vertex from the queue. For example:
In the above imagine the edges in the transposition graph point to the left and you start your dijkstra from
(d, 0).Since the vertices you pop from the queue aren't necessarily fully relaxed as is the case with usual dijkstra this should have a higher time complexity.
Edit: I think the best way to avoid this is to use toposort as nithish654 explained in a reply.
Are you saying to keep visited array and all , i know that's how people usually write dijkstra but I prefer my way. As much as I know it only visited each node like maximum 2 times so usually works fine and usually keep a if check to avoid that but it is not necessary to do that.
No, your dijkstra seems fine implementation-wise, I am just saying that dijkstra in general wouldn't be efficient for finding the minimum number of batteries needed to reach the final vertex since you can't guarantee that the vertex with the smallest cost in the queue is fully relaxed. In some cases this will lead to rerunning dijkstra on part of the graph multiple times (not just 2 but up to
n. The above example I gave can be tweaked so that you addato the queueO(n)times and then each time you add it you run dijkstra on the subgraph to the left ofa) The problem is roughly the same as the problem with running dijkstra on a graph where some of the edges can have negative weights (but there is no negative cycle). Dijkstra can solve that problem but with bad time complexity.Problem F could also be solved in
O(N), tho it doesn't matter much. Most things are same, except there is one more observation: - Let's say our current array isa[1] <= a[2] <= a[3]... a[N]- Then, we don't actually need to tryf(a[i], a[N]), wherea[i] <= a[N-1]/2, sincemax possible value of f(a[i], a[N]) <= min possible value of f(a[N-1], a[N]). (wherea[i] * 2 <= a[N-1])Thus, if the maximum value gets updated (let's say
xis new,yis the old one):2*y > x, obviously answer= x.We check elements in range
y/2...y. (We can push everything to a set and just iterate from the back).Well, one can see that we only process an element with a current maximum only once, thus we can just push everything to a vector and clear after each use: 321161406
In question D many solution that were accepted shouldn't have passed system testing cause now that I am trying submit those same solution again they are giving MLE on test case 31 which those solutions were not tested against in the system testing phase!
+1
My submission for D seems to be $$$\mathcal{O}(n^2)$$$ but there is no testcase to fail it. 321147941
For each node, I compute a list of intervals to describe all the possible battery counts at that node. Then computing these lists is a straightforward DP.
But for each transition between $$$u$$$ and $$$v$$$, $$$v$$$'s entire list must be checked. This motivates a hack testcase: for the first $$$\frac{n}{2}$$$ nodes, form a chain and attach each node to the middle node as well. Then for the other $$$\frac{n}{2}$$$ nodes, attach them to the middle node. The size of the middle node's list is $$$\frac{n}{2}$$$ now, and computing $$$\frac{n}{2}$$$ more transitions from that will give quadratic runtime. Here's the generator if I wasn't clear:
My final solution adds some optimization on top of the original idea, but with some work it can be defeated as well.
Is there a way to improve the worst bound for my approach?
Did anyone else get Runtime Error on test case 12 while solving E? How did you solve it?
why can dfs pass D?????? i tried many ways but not dfs till 01:30
It's really weird that my solution passed just with a
break. It does Dijkstra on $$$(idx,sum)$$$, which should be $$$O((n+m)W\log{n})$$$ in the worst case. Can anyone try to uphack it? 321125700resubmit your sol, it should give TLE/MLE now since there's test #31 and #32
Can anyone tell me why my solution is failing? It says that the jury found a solution, but I don't still can't see why my solution couldn't find the euler path, even after accounting for the euler cycle.
Problem E, -> Submission
This works too. (For D)
~~~~~
Hey for problem C my idea was at every point where there is -1 to check if the minimum element in the remaining array is smaller than or equal to current height.If yes , set that arr[i] to 0 otherwise 1. THen I later check if the given array is able to pass the array But on test 2 , number 89 this shows wrong.
Because you didn't consider that if d[i] later equals 1
bruhh.. i did consider it using the num1 array which precomputes number of compulsory 1s in a range.. still its showing WA on pretest 2
My submission: 321158954
You can see my submission: 321099783
I get your idea.. but what's wrong with mine?
Can anyone help me out in finding a teat case were my code fails
https://codeforces.me/contest/2110/submission/321134907
Try this case:
1 2 -1 1 0 2 1 1
It should output 0 1
When you hit -1, you always choose to go up, which means you always fly at the height limit, but once you hit a 1 in this state, the programme will throw an wrong -1. like: 4 -1 -1 -1 1 0 1 0 1 0 1 0 1
Could you help me see why the time complexity is qualified? I used the priorityqueue and visit arrays,problem D:321109278
c&d are too easy, though I was stumbled by c because of an imperceptible mistake, which made my rating down
However, e&f are marvelous! I love them very much
Can anyone please help me out with why my code fails in pretest 2. 321158954
I store the minimum (along with its index) of all upper limits among the limits from the end to the particular index, and from the range of current index to maximum index of the minimum upper limit i count the number of compulsory 1s using precomputed num1 array in O(1). So we only assign v[i]=1 when h+(number of compulsory 1s)<mini[i] because if we increase h when h+(number of compulsory 1s) is already >=mini[i], the answer would fail because we already exceeded the upper limit of atleast one obstacle.
My code - ~~~~~
void solve(){
int n; cin >> n; v32 d(n), num1(n+1); vv32 p(n, v32(2)), minn(n, v32(2)); forn(i, n) cin >> d[i]; forn(i, n) cin >> p[i][0] >> p[i][1]; forn(i, n) num1[i+1] = num1[i]+(d[i] == 1); minn[n-1][0] = p[n-1][1]; minn[n-1][1] = n-1; rforn(i, n-2){ minn[i] = minn[i+1]; if(p[i][1] < minn[i][0]) minn[i][0] = p[i][1], minn[i][1] = i; } int curh = 0; forn(i, n){ if(d[i] >= 0){ curh += d[i]; if(curh < p[i][0] || curh > p[i][1]){ cout << "-1\n"; return; } } else { int diff = minn[i][0]-curh, ind = minn[i][1], ct1 = num1[ind]-num1[i]; // cout << diff << " " << ind << " " << ct1 << ln; if(ct1 < diff){ d[i] = 1; curh++; } else if(ct1 == diff) d[i] = 0; else { cout << "-1\n"; return; } if(curh < p[i][0] || curh > p[i][1]){ cout << "-1\n"; return; } } } print_vec(d);} ~~~~~
I could not understand your code or explanation , can you maybe explain a bit further , I can give you a simple rundown of the solution
In problem C i applied a greedy approach where i increase the height where ever it is possible .
firstly i calculated the maximum possible height for each obstacle such that i can reach the end .
then updated my values accordingly . can someone tell where can this greedy approach fail .
My submission — 321105467 ??
Consider this test case,
3
-1 -1 -1
0 10
0 10
0 1
Here, if you increase the height in both of the first 2 turns, you'll end up missing the third gate
What if we first do this: for every ri, we set it to the minimum of all rj where j >= i.
Then, if we increase the height whenever we can, we will not miss any gate's upper limit, and we will be able to clear the lower limits of all gates.
This sounds okay but this gives me wrong answer, I am yet to figure out why.
instead of minimum of all rj you can take minimum of extra 1's that rj can bear and then you can greedily increase height. like this 321309570
I had an alternate solution to B. I essentially cut the first opening bracket (always at first position) and last closing bracket (always at last position) and then checked if it was still balanced. It seemed easier to me that messing with the balanace factor arrays.
My initial idea in D was to binary search on the answer and run dijkstra(for each node, find max number of batteries you can end up with there) but that exceeds TL. But it seems like just bfs works: https://codeforces.me/contest/2110/submission/321128976
though it gets TL verdict after removing this line:
Cutting down on parallel entries seems to help a lot, but I don't think it makes up for the cases where each d[u] can be updated a lot of times. Can anyone prove the correctness of this or uphack it?
In B I was just checking for the presence of ")(" , where does this logic fail?
(()()) would break your code as you have )( in (( )( )) but the output here should be NO as removing any open-close bracket pair still keeps it balanced as the balance array is [1, 2, 1, 2, 1, 0] Which has no 0 except the last element (the logic used in the solution)
Here is an enhanced version of the problem F.
Why is this solution incorrect: https://codeforces.me/contest/2110/submission/321230391
Can anyone give me a test case where this fails?
1
4
-1 -1 -1 1
0 2
0 2
0 2
1 2
A valid solution 0 0 0 1 exists but yours give -1
Can someone help me in fixing this? I've no clue why this won't work. I know that there is a simpler reduced version of the dijkstra based off the constraint $$$ u_i \lt v_i $$$ but I wanna know what's wrong here...
Submission
Need help
For Problem C(Racing), my solution says "wrong ans on test case: 2". I couldn't find my mistake. For which case scenario, my solution might not work properly? I am genuinely seeking help, please don't down-vote me.
Here is my submission: https://codeforces.me/contest/2110/submission/321800281
Hey, I think you are incrementing height in a not so good way. By the way, your cinn and print macros are cool. Let's consider this test case.
1
3
-1 -1 1
0 2
0 2
0 2
Here your test performs hi = 1, then 2, but at 3rd step it got stuck as now it had to increase mandatorily. So, it outputs -1 but answer do exist 0 0 1 or 0 1 1 or 1 0 1.
Hope it helps!
it seems that I found an $$$O(n+m)$$$ dp solution for D?
first, for every node, find how many batteries we need at least to reach node n. second, for every node, find the minimum count of batteries we should use to reach current node, and the maximum count of batteries we can collect before we leave current node. using these info, we can know the final answer.
is it completely correct? my accepted submission
My solution to D that passed (and still passes) is brute force and can reach O(2^sqrt(M)) for a complete graph. 321117884
I hadn't even seen that the edges go up in value in the competition, shit happens
Anyone knows why this solution for problem B (Down with Brackets) works? I basically check if the entire sequence (i.e., String $$$S$$$) has at least two balanced sequences that are concatenated.
PLZ PLZ PLZ explain me where my code is failing in C(RACING) it is giving wrong ans on test 2 in test case 474 https://codeforces.me/contest/2110/submission/322820237 .. my logic is doing all -1 1 and store indexes in stack ( dont store those indexes where doind -1 into 1 is a necessity) and when h > r .. take the indexes of stack and make it 0 .. can u read the code and tell the failure test case please
https://codeforces.me/contest/2110/submission/323352665 You didn't check that you didn't break anything before(the second loop in the fix). And I think you need to add current -1 to stack before removing from stack, or at least I understand that it works if you do it this way
how strong intuition needed to solve F
F is amazing! I can't see any of these three facts.
F is a really beautiful problem