Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int grid[45][45];
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n, m;
cin >> n >> m;
int max_i = 0, max_j = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
if (grid[i][j] > grid[max_i][max_j])
max_i = i, max_j = j;
}
int h = max(max_i+1, n-max_i);
int w = max(max_j+1, m-max_j);
cout << h * w << '\n';
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
if (n % 2 == 1) {
cout << "Mike\n";
continue;
}
int smallest = 0;
for (int i = 0; i < n; ++i)
if (a[i] < a[smallest])
smallest = i;
if (smallest % 2 == 0) cout << "Joe\n";
else cout << "Mike\n";
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 1010
int grid[N][N], mn[N][N], mx[N][N];
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n, m;
cin >> n >> m;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
cin >> grid[i][j];
mn[0][0] = mx[0][0] = grid[0][0];
for(int i = 1; i < n; ++i)
mx[i][0] = mn[i][0] = mx[i - 1][0] + grid[i][0];
for(int j = 1; j < m; ++j)
mx[0][j] = mn[0][j] = mx[0][j - 1] + grid[0][j];
for(int i = 1; i < n; ++i)
for(int j = 1; j < m; ++j) {
mx[i][j] = max(mx[i - 1][j], mx[i][j - 1]) + grid[i][j];
mn[i][j] = min(mn[i - 1][j], mn[i][j - 1]) + grid[i][j];
}
if(mx[n - 1][m - 1] % 2 || mn[n - 1][m - 1] > 0 || mx[n - 1][m - 1] < 0)
cout << "NO\n";
else
cout << "YES\n";
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 2010
vector<int> tree[N];
int dfs(int i, int p) {
int sm = 0, z = 0;
for (int j : tree[i]) if (j != p) {
int x = dfs(j, i);
sm += x;
if (x == 0)
z++;
}
return sm + max(0, z - 1);
}
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
if (n == 1)
cout << "0\n";
else {
int ans = n;
for (int i = 1; i <= n; ++i)
ans = min(ans, 1 + dfs(i, i));
cout << ans << '\n';
}
for (int i = 1; i <= n; ++i)
tree[i].clear();
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 200010
vector<int> tree[N];
int dfs(int i, int p) {
int sm = 0, z = 0;
for (int j : tree[i]) if (j != p) {
int x = dfs(j, i);
sm += x;
if (x == 0)
z++;
}
return sm + max(0, z - 1);
}
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
int max_deg = 0;
for (int i = 1; i <= n; ++i)
max_deg = max(max_deg, (int)tree[i].size());
if (max_deg == 0)
cout << "0\n";
else if (max_deg < 3)
cout << "1\n";
else {
for (int i = 1; i <= n; ++i)
if (tree[i].size() >= 3) {
cout << dfs(i, i) << '\n';
break;
}
}
for (int i = 1; i <= n; ++i)
tree[i].clear();
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 600010
vector<pair<int, int>> graph[N];
vector<int> lst;
int ans[2][N];
bool used[N], usedEdges[N];
void dfs(int i) {
lst.push_back(i);
if (!used[i]) {
used[i] = true;
for (pair<int, int> p : graph[i])
if (!usedEdges[p.second]) {
usedEdges[p.second] = true;
dfs(p.first);
lst.push_back(i);
}
}
}
int main() {
int n; cin >> n;
string ptop(n, 'U'), pbot(n, 'D');
string mtop(n, 'U'), mbot(n, 'D');
for (int i = 0; i < n; ++i) {
int u, v;
cin >> u >> v;
graph[u].emplace_back(v, i);
graph[v].emplace_back(u, i);
}
int idx = 0;
for (int i = 1; i <= 2 * n; ++i) if (!used[i]) {
dfs(i);
lst.pop_back();
int k = lst.size() / 2;
if (k == 1) {
cout << "-1\n";
exit(0);
}
for (int j = 0; j < k; ++j) {
ans[0][j + idx] = lst[j];
ans[1][j + idx] = lst[2 * k - 1 - j];
}
for (int j = 0; j < k - 1; j += 2)
ptop[j + idx] = pbot[j + idx] = 'L', ptop[j + 1 + idx] = pbot[j + 1 + idx] = 'R';
for (int j = 1; j < k - 1; j += 2)
mtop[j + idx] = mbot[j + idx] = 'L', mtop[j + 1 + idx] = mbot[j + 1 + idx] = 'R';
lst.clear();
idx += k;
}
cout << "2 " << n << '\n';
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < n; ++j)
cout << ans[i][j] << ' ';
cout << '\n';
}
cout << ptop << '\n' << pbot << '\n';
cout << mtop << '\n' << mbot << '\n';
}
С sucks
You sucks
Your grammar sucks.
Yes, example test case for B sucks so I got many WA on pretest 2 and problem С also sucks. And I am trying to hit -100 contribution, So downvote me.
Why do I get upvotes, I need downvote!!!.
Alright let's click the upvote...
Good reverse psychology lol
no it's actually helpful
Thanks for the fast editorial!
Fix picture for C please
Statement of Problem A was incomprehensible. These days, I feel that in cf contests it's not the problem that's hard but the way it's written which makes it harder to understand :/ Had to blindly solve it using the given test cases
I agree. I anticipate the description of A as short as AtCoder problems.
NarrationForces
Unclear Ad-Hoc based GridForces
same as problem D
problem A was mad to read tbh
Let $$$f[i][j][k]$$$ be whether there is a path whose end point is $$$(i, j)$$$ and the sum on the path is $$$k$$$.
We can use
bitset
to calculate this DP with a $$$O\left(\dfrac{nm(n + m)}{64}\right)$$$ complexity.Submission: 161091846
I am not able to get why is the time complexity divided by 64. Would be great if you could explain it.
It's because of use of the bitset. I am not sure but I think each position contributes only 1 bit to the space complexity and not the space required for storing 64-bit integer (size of long long)
The size of the bitset is roughly $$$\dfrac{(m+n-1)}2$$$. On 32 bit systems, bit operations on bitsets take $$$O(\dfrac{n}{32})$$$, where $$$n$$$ is the size of the bitset. This means each operation takes about $$$O(\dfrac{m+n}{64})$$$. Then, the loop makes it $$$O(\dfrac{nm(n+m)}{64})$$$.
what is this line doing ?
Same way with me and my submission is 161081121
If the editorial for D doesn't make sense, you can check out this one over here on page 17.
Thanks. Appreciated (I mistakenly downvoted you and now I can't change it.)
[submission:161072909]Why is it wrong?
because k is initialized by a[x][y], that means k equals 0 ( k=a[x][y]; )
but when all the values in the grid are negative the result will be incorrect.
Thanks
There is a solution that does not require any observation in D2 from D1 — just re-root the tree.|
First, calculate the answer for any root.
If you want to change u to v, push all changes into a stack, recalc dp[u] to what it was before you added v, then recalc v as if it is the root. Then, do it recursively for all children son such that son != u. After you finished, roll back the changes to dp. It takes O(1) to roll back, O(1) to recalc, so it is still O(n)
I solved it in the same way (sadly after contest).
If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints.
Divison 2
If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).
Hello, it generated a wrong testcase for problem C. Ticket We can only have 1 or -1 in the grid, but it showing a testcase with 2.
Hi, the generated testcase is actually correct.
The input format is the same as the problem, so
means that you have one testcase, where the number of rows is 1, number of columns is 2, and the elements are
-1 1
. (Here, 2 is the dimension of the matrix, not the matrix element).Oh ok. Sorry!
Can anyone tell the answer to the test case:
according to cf stress test it should be joe but according to me winner will be mike as in 2nd iteration 2nd element will be already 0 that was joes pile.
Here the winner is Mike as you said
I am really dumb there was a typo in finding the minimum value in my code and due to that i was not able to get ac on b and wasted a lot of time and i suddenly noticed it now.
If anyone was curious, you can solve C with the bitset dp: https://codeforces.net/contest/1695/submission/161121586
what a ironical name!
Doesn't creating a dp vector will take O(n * m * (MAXN + 1)).and this solution should get tle?? Plz explain
I have some doubt about the solution to problem A. Consider following case:
Based on the solution, the answer is 12. But if we take a subrectangle with $$$(h, w) = (4, 3)$$$ and upper-left corner placing on $$$(1, 1)$$$, the maximum value inside is 21, which is not the global maximum value(30). So if Joe chooses this subrectangle, Michael will lose.
I think the answer should be 16. Am I missing something?
Michael can instead take a subrectangle with $$$(h,w) = (3,4)$$$. this includes the global maximum wherever the subrectangle is.
Oh, I completely forgot Michael was the one who chooses h, w values... Thanks!
I'm not gonna argue about the quality of the problems (there's already much argument going on), however I think the pretests for A was pretty much garbage
161068768 a solution which scans from -32768 got through pretests. this means that pretests didnt even have a testcase with less than -32768 as minimum
161081758 a weird solution, and I dont understand how it even went through the pretests
161078731 what even is this
deepmind practicing
Is there any way of using dfs in problem C?i can make a graph for possible path from a cell and apply dfs to check whether the sum is zero or not?
In the worst case(when n,m = 1000), there would be 1998C999(1998 Choose 999) different paths from 1,1 to n,m. (This because we need to make 999 right turns and 999 down turns, so total 1998 turns. Just need to select 999 out of these for a right turn)
Which would exceed the time limit easily.
Thanks for your nice explanation. Got it!
I have done this contest with a really bad result and I think I need to say something. As a habbit, I always initialize the max = -999999999 and min = 999999999, my brain thinks 999999999 > 1e9 and actually I don't understand why I has not had any problems with this before. As you can easily guess, in this contest, I had problems with A.Subrectangle Guess and B.Circle Game. I should have solved A and B in under 15 minutes; I passed pre-tests of problem A, and I never think it will be terrible for me, until now. If I recieved WA in problem A, I would soon find the mistake(max = -999999999), because the idea is clear, I would not have to check the idea, I could concentrate on the code and find my mistake, I just lost 50 points, and next I would easily solve problem B and have over 1h30m to think promblem C or D1. But no, I recieved WA on test 3 in problem B, and although I could prove my idea is correct, I still spent the whole time to find the mistkae in my idea, until some last mintues, I found the mistake in my code, and I passed B on exactly the last minute, and did not have enough time to submit A. As a result, I did not pass the main test of problem A. Anyway, thank you for this contest, if I don't fail in this contest, maybe I will fail in the contests which are more important to me because of this mistake.
instead initialize max = INT_MIN, min = INT_MAX no issue of thinking numbers
based opinion
The tutorial doesn't have any hints and I don't want to see the solutions directly
Can someone give me some hints for problem E?
In what situtaion, the answer is -1?
For hint 1, the answer is "when there is a connected block with only 1 edge".
Otherwise, the puzzle grid alaways could be constructed with a 2*n martix.
dfs/eulerian.
In problem D, you make your queries first and then you get the distance values, altogether... I'm writing this because I somehow missed/overlooked that crucial point while reading the problem. I guess some others may have unknowingly made the same mistake.
0-1 BFS also works for C with a deque.
Can you share your implementation?
You can see in his submission. here
can u pls explain the implementation using 0-1 BFS
Can someone please explain why 161082582 got accepted but 161062638 did not in prob A UPD: got it
Hey jdurie!
Personally, I feel that the problem statements were a bit unnecessarily lengthy, and thus difficult to understand (especially A). Not much problem with sample test cases (just speaking for myself).
Overall, I really liked the contest though, and thank you for the fast editorial. Trying to upsolve a few questions now!
I don't agree at all, problem A was very clear and sample test cases for B were just horrible.
Problem B sample tests were horrible. It was very easy to get them correct. For example, I submited 8 times before getting AC.
nice solutions
Can someone tell the proof written for Problem C in the editorial in simpler words stating that we can get the sum as 0 if the maximum and minimum path sum ending at (n,m) cover's 0 if the max_sum >= 0 >= min_sum
Basically they are saying that we can change the path that gave minimum sum to the path that gave maximum sum and on each step of changing that , sum will change by 2,0,-2 .
Since we know that number of cells in any path = n+m-1 (which is even)
Hence both min_sum and max_sum is also even ,
and we are changing min_sum to max_sum by +-2 , so we will definitely hit 0 given that 0 is in the range of [min_sum,max_sum]
I guess in problem C's solution the condition for no path should be 0>minnm or maxnm<0 and not 0<minnm or maxnm<0. Please fix ig this is a typo.
it's correct only, 0<minm means minm is positive
Very understandable and clear editorial. Thank you! :)
I have not yet learned much, but i keep seeing dp in the discussions C and D. Can anyone explain what is dp ?
Can anyone explain the solution for E? Don't really understand why the order of dfs matters.
I would explain it in a slightly different way, which is similar to the editorial's solution.
For any dominoes $$$(a,\,b)$$$ we have, add an edge between node $$$a$$$ and $$$b$$$. Merging the two solutions, we found out that these dominoes form cycles. Therefore we may think about finding the Eulerian Cycle of the graph obtained by two copies of the dominoes. If we successfully found it, the solution is the odd edges and the even ones of the Eulerian Cycles. However, these cycles we found may not guarantee that two dominoes from the different copies have different parity. It can be solved by letting the same dominoes from the different copies be adjacent in the adjacent lists and all the adjacent lists have the same order of edges. Note this is the same we do in the editorial's solution. For each cycle, if the length is $$$2$$$ then there must be no solution, otherwise, we arrange them in a $$$2 \times \frac{\text{length}}{2}$$$ grid. It is always possible since each connected component contains an even number of edges.
The proof part of problem C is brilliant and that's what made the problem a bit hard in my opinion.
I was unsure before submitting my solution and got gently surprised when it got accepted.
Come on, I didn't even try to implement O(n^3) solution, since I thought it would be too slow. Turns out that it is under 1s even in java..
Why not just set $$$\sum n\times m \leq 10^7$$$ or else everyone will pass with $$$O(\dfrac{nm(n+m)}{w})$$$???
see this : https://codeforces.net/contest/1695/submission/161085118
I thought of this kind of way to solve in square time complexity, but i still decided to write the code above. However, you can just make the constraints much bigger, and maybe there will be most people coding the tutorial's solution.
I don't think this solution should be hacked. At least it showed an important way to improve the time complexity($$$O(\frac{1}{\omega})$$$). In my point of view, various solutions are also important and inspiring.
For problem D1 Div2, in the editorial, can anyone explain the benefit of having this condition: we can assume that either v or some vertex not in the subtree of v has already been queried while calculating the answer.
Also D2 Div2, why is this statement true: The way we compute values in the DFS ensures that at least degree[root]−1≥2 subtrees of the root will have at least one query.
I think Problem A has a subtle detail and would like to share it here.
We are not choosing a minimum area such that any rectangle >= that area covers the
maxval
.Instead, we are choosing a minimum pair (h, w) such that any h*w rectangle covers the
maxval
, and by saying "minimum pair" I mean the pairs are ordered by the areas that they form.Those two are different! For example, consider the following case.
If we follow the standard answer we get the pair (h, w) = (3, 3) such that any 3*3 rectangle covers 99. But it is not "a minimum area such that any area >= 9 covers 99", for example you can consider the following area.
It has an area 10 >= 9 but does not cover 99.
It's not minimum area, it's minimum value for h and minimum value for w.
Though my rating dropped after the contest, I think it's a pretty good round.
Because it made me realize my poor ability in dp on tree :)
That's a good example of positive mindset. I see a lotta people perform bad and start trolling the authors, problems or Codeforces itself.
Why the timelimit of E is 8s?
Hey everyone! Can someone please explain the solution for problem C? I do not see how does this proof in the editorial conclude there exists a zero path. Thanks in advance!
Statement from editorial I am confused about:
It's just like the intermediate value theorem.
got an FST on div2 A :(
In D2, how does querying any vertex with degree >= 3 will ensure the answer to be minimum. Why don't we need to do it for all vertices with degree >= 3?
The $$$ans[v]$$$ can be understood as "the answer of an subtree with root $$$v$$$ if there are at least one query vertex not in this subtree." The condition that the degree of top most vertex $$$\ge 3$$$ is to guarantee for all the subtree, there are a least one query point not in it.
The Solution Given for "C", does this kind of algo have some name(belong some where like div & conq etc) or just plain problem solving
Intermediate Value Theorem
Other IMVT problem: https://www.codechef.com/problems/B01T
Thanks bhai!
I thought the intermediate value theorem was for continuous valued functions only? This is why I'm still not convinced with the proof for C. How can we say that just because
Can we prove that for every value between min and max there exists a path whose sum equals that value?
The conditions which you're assuming is not sufficient. You are missing the change at every steps. Change at every step is 2, -2, 0. Previously it was negative even number and later positive even number and everytime there is a change of 2, -2, 0. So you'll get 0 for sure.
Don't we have to prove that it's always possible to find a path with change +2 or -2?
Its already proven in the editorial bro.
The idea is that suppose you already have a path to achieve the max sum, and a path to achieve a minimum sum; the claim is that any sum (of the same parity) in between the max and min are achievable.
Now, why should this hold true? The intuition here is that because paths only go right and down, you can convert the two paths between each other by a multiple operations of flipping a "turn" (i.e. if a turn goes right then down, make it go down then right). In particular, we note that each time we flip a turn, it can change the sum by at most absolute value 2. This happens when we step on a -1 instead of a 1 (or vice versa) as a result of flipping. (Note that a single flipping operation changes exactly one cell on the path.)
did anyone know why D2 only dfs a vertex more than two sub is ok, but not dfs all vertex which has more than two sub. i think the answer didn't say clearly
Problem C was a good one.
For problem D, what is the smallest k(the worst situation in any possible hidden-X with an optimal strategy) if we are able to use online query, instead of quering all the node at once?
Are they the same?
If not the same,what is the best optimal strategy?
Let's try the following reasoning — root the tree at u — that is our first ask. For every ask concerning two sons, we can answer no as long as there are some other possibilities. This is very similar to our offline dp — we cannot toss out more than all sons by one query. So I think that the answer is the same.
thanks!
For Problem-D2, why is it that performing our greedy DFS for any of the nodes with degree greater than 3 works?
Problem D is a known concept in graph theory called Metric dimension: https://en.wikipedia.org/wiki/Metric_dimension_(graph_theory)#Trees
D has appeared in previous competitions: https://atcoder.jp/contests/apc001/tasks/apc001_e
can anyone help me with memoising C
BTW i think it can't be
HOW TO SOLVE C USING BFS
Nice proof of Problem C
Can I get all the test samples for question C? Thanks.