Thank you for participating, we hope you enjoyed the problems! We kindly ask you to rate each of the round's problems in the corresponding spoiler in order to improve the quality of future contests.
You can also check video editorials of problems B and C on ak2006 Youtube channel.
All problems were prepared by Alexdat2000 with the help of coauthors.
1634A - Reverse and Concatenate
Idea: sevlll777
Hint 1
What property will the string have after the first operation (regardless of the type of operation)?
Hint 2
What happens if you apply an operation to a palindrome?
Tutorial
Tutorial is loading...
Solution
q = int(input())
for _ in range(q):
n, k = map(int, input().split())
s = input()
if s == s[::-1] or k == 0:
print(1)
else:
print(2)
1634B - Fortune Telling
Idea: crazyilian and antontrygubO_o
Hint 1
Can you figure out which of your friends can't get the number $$$y$$$ regardless of the order of operations? The answer to the problem would be the other person, since the jury guarantees that exactly one of your friends could get it.
Hint 2
What do the numbers $$$a \oplus b$$$ and $$$a + b$$$ have in common for any $$$a, b$$$?
Hint 3
What do all the numbers that can be obtained by all combinations of operations starting with $$$x$$$ have in common? Does the set of these numbers intersect with the set of numbers that can be obtained starting with $$$x + 3$$$?
Tutorial
Tutorial is loading...
Solution
def main():
n, x, y = map(int, input().split())
a = list(map(int, input().split()))
if (sum(a) + x + y) % 2 == 0:
print('Alice')
else:
print('Bob')
for _ in range(int(input())):
main()
1634C - OKEA
Idea: sevlll777
Hint 1
If $$$k > 1$$$, for which $$$n$$$ is there no solution?
Hint 1.1
For which $$$n$$$ is there no solution when $$$k = 2$$$? Does the same rule of thumb hold for other $$$k > 1$$$?
Hint 2
Note that the array $$$[1, 3, 5, 7, 9, ...]$$$ satisfies the requirements (that is, the arithmetic mean of each subsegment is an integer) because the sum of the first $$$X$$$ elements of this array is $$$X^2$$$. Try to generalize this special case and find other arrays that satisfy the conditions.
Tutorial
Tutorial is loading...
Solution
def solve():
n, k = map(int, input().split())
if k == 1:
print("YES")
for i in range(1, n + 1):
print(i)
return
if n % 2 == 1:
print("NO")
return
print("YES")
for i in range(1, n + 1):
print(*[i for i in range(i, n * k + 1, n)])
for _ in range(int(input())):
solve()
1634D - Finding Zero
Idea: sevlll777
Hint 1
The problem can be reworded as follows: find $$$n - 2$$$ indices that definitely can't contain a zero.
Hint 2
Solve the problem for $$$n = 4$$$.
Hint 2.1
For $$$n = 4$$$, denote by $$$\bar{i}$$$ the answer to the query for the three indices except $$$i$$$. Knowing $$$\bar{1}$$$, $$$\bar{2}$$$, $$$\bar{3}$$$, and $$$\bar{4}$$$, can you find two indexes that don't contain zero for certain? Use the fact that there is exactly one zero in the array.
Hint 3
Solve the problem for even $$$n$$$ using the solution for $$$n = 4$$$.
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define len(x) (int) (x).size()
using namespace std;
int get(const vector <int>& x) {
cout << "? " << x[0] + 1 << " " << x[1] + 1 << " " << x[2] + 1 << endl;
int ans;
cin >> ans;
return ans;
}
signed main() {
ios_base::sync_with_stdio();
cin.tie(nullptr);
int t;
cin >> t;
while(t --> 0) {
int n;
cin >> n;
pair <int, int> possible = {0, 1};
for (int i = 2; i < n - 1; i += 2) {
vector <pair <int, int>> ch(4);
vector <int> now = {possible.first, possible.second, i, i + 1};
for (int j = 0; j < 4; j++) {
vector <int> x = now;
x.erase(x.begin() + j);
ch[j] = {get(x), now[j]};
}
sort(all(ch));
possible = {ch[0].second, ch[1].second};
}
if (n % 2 == 1) {
int other = 0;
while (possible.first == other || possible.second == other) {
other++;
}
vector <pair <int, int>> ch(4);
vector <int> now = {possible.first, possible.second, n - 1, other};
for (int j = 0; j < 4; j++) {
vector <int> x = now;
x.erase(x.begin() + j);
ch[j] = {get(x), now[j]};
}
sort(all(ch));
possible = {ch[0].second, ch[1].second};
}
cout << "! " << possible.first + 1 << " " << possible.second + 1 << endl;
}
return 0;
}
1634E - Fair Share
Idea: sevlll777
Hint 1
In what cases is the answer definitely NO?
Hint 2
The lengths of all arrays are even numbers, and each number appears an even number of times in total. There are suspiciously many even numbers...
Hint 3
An Euler circuit is a circuit that passes through all edges of a graph exactly once (or a union of several circuits if the graph is disconnected). It turns out that an Euler circuit exists if and only if the degree of each vertex is even.
Hint 4
Construct some graph such that the degree of each of its vertices is even, find a directed Eulerian circuit in it, and reconstruct the answer to the problem using this circuit. What do the edges of such graph correspond to?
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define len(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define endl "\n"
using namespace std;
const int N = 4e5 + 100, H = 2e5 + 50;
vector <pair <int, int>> g[N];
string ans[N];
int pos[N];
void dfs(int v) {
if (pos[v] == 0) {
ans[v] = string(len(g[v]), 'L');
}
while (pos[v] < len(g[v])) {
auto [i, ind] = g[v][pos[v]];
if (i == -1) {
pos[v]++;
continue;
}
g[i][ind].first = -1, g[v][pos[v]].first = -1;
if (v < H) {
ans[v][pos[v]] = 'R';
}
pos[v]++;
dfs(i);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin >> m;
map <int, int> ind, cnt;
int fre_ind = 0;
vector <vector <int>> nums(m);
for (int i = 0; i < m; i++) {
int n;
cin >> n;
for (int _ = 0; _ < n; _++) {
int x;
cin >> x;
if (!ind.count(x)) {
ind[x] = fre_ind++;
}
x = ind[x];
cnt[x]++;
nums[i].push_back(x);
g[i].emplace_back(x + H, len(g[x + H]));
g[x + H].emplace_back(i, len(g[i]) - 1);
}
}
for (auto [num, cn] : cnt) {
if (cn % 2 == 1) {
cout << "NO" << endl;
return 0;
}
}
for (int i = 0; i < N; i++) {
dfs(i);
}
cout << "YES" << endl;
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
}
1634F - Fibonacci Additions
Idea: Mangooste
Hint 1
Let $$$C_i = A_i - B_i$$$. Perform all operations on the array $$$C$$$.
Hint 2
Nevermind Fibonacci and suppose the operation was to increase the elements of a given subsegment by a given constant. Can you solve this problem without using heavy data structures like segment tree?
Hint 3
Generalize the idea from hint 2 using the characteristic property of Fibonacci numbers ($$$F_i = F_{i-1} + F_{i-2}$$$).
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define len(x) (int) (x).size()
#define endl "\n"
#define int long long
using namespace std;
const int N = 3e5 + 100;
int MOD;
int fib[N];
vector <int> unfib;
int notzero = 0;
void upd(int ind, int delta) {
notzero -= (unfib[ind] != 0);
unfib[ind] += delta;
if (unfib[ind] >= MOD) {
unfib[ind] -= MOD;
};
notzero += (unfib[ind] != 0);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
cin >> n >> q >> MOD;
fib[0] = fib[1] = 1;
for (int i = 2; i < N; i++) {
fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;
}
vector <int> delta(n);
for (int& i : delta) {
cin >> i;
}
for (int i = 0; i < n; i++) {
int x;
cin >> x;
delta[i] = (delta[i] - x + MOD) % MOD;
}
unfib.resize(n);
unfib[0] = delta[0];
if (len(unfib) >= 2) {
unfib[1] = (delta[1] - delta[0] + MOD) % MOD;
}
for (int i = 2; i < n; i++) {
unfib[i] = ((long long) delta[i] - delta[i - 1] - delta[i - 2] + MOD * 2) % MOD;
}
for (int i = 0; i < n; i++) {
notzero += (unfib[i] != 0);
}
while (q--) {
char c;
int l, r;
cin >> c >> l >> r;
if (c == 'A') {
upd(l - 1, 1);
if (r != n) {
upd(r, MOD - fib[r - l + 1]);
}
if (r < n - 1) {
upd(r + 1, MOD - fib[r - l]);
}
} else {
upd(l - 1, MOD - 1);
if (r != n) {
upd(r, fib[r - l + 1]);
}
if (r < n - 1) {
upd(r + 1, fib[r - l]);
}
}
if (!notzero) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
Thanks for fast editorial!
Problem B is the best problem I have seen in a long time!
I think that people disagree with you
I agree. B and C probably should've been flipped but that says nothing about the quality of B. I think it was a great problem.
Your profile picture is more than enough for distraction
She's Tzuyu Chou from Twice if you're curious.
For me, B is one of the best Div2 problems in 2022 so far. I want more of these problems.
Absolutely agree
Yep..... It was one of Most interesting one!!
I don't know if I'd say I like problem B. I will say that I audibly laughed out loud when I realized the solution to B.
I agree!!
its a mastapiece!!
Lightning-fast editorial
fast editorial!
Thanks for the fast tutorial
https://codeforces.net/problemset/problem/446/C
This problem has a bit of a similar statement, but the solution and the idea are very different.
What a great problem set! Thank you !!
B is nasty
E and F are great!I like them!
My solution to problem A is hacked
oh no ! ! !
i hope you will recover from this very soon . . .
this is
so
sad . . .. . . ..
Do you know why your solution was hacked? Seems ok to me.
No, I even do not know the test case in which my code is not working
1 2 1 ab -> your solution:1, actual: 2 (abba, baab)
Thanks
Great Problem E & F!They're the best problems I've seen in div2 :)
thanks for providing hints!!!
I have a different solution to F (I'm not sure if it is correct):
Let c[i]=a[i]-b[i]
You can maintain a tag "(A[i],B[i])" for each i . means c[i],c[i+1],c[i+2]...c[n] need to be added a fib sequence starting with (A[i],B[i]).Two tags can be merged easily .
Every operations can be written as two tags in position l and r+1.
If you want to know the exact sequence , you need to add the tag to c[i] for every 1,2,3...n.But if there is a position where c[i]≠0 , you can stop immediately .
So because of the randomness of the Fibonacci sequence , I guess it is O(n).
[submission:https://codeforces.net/contest/1634/submission/145427395]
Why there are so many FSTs
I think they maybe forget to judge (k==1) in problem A. :(
In Korea, there is a similar problem with F. It was a great round, thanks.
https://www.acmicpc.net/problem/20305
I like Editorial With Hints
Congratulations, the author of this round. You scared AI away on behalf of humans.
They didn't compete at all!
The problems are so good, I thought, although they will give me a negtive rating change.
This is the first time I get FST :D I struggled to understand the problem and missed that case
What does FST mean? Thank you
Failed system testing
Like Frozen said,It means that i passed the pretests during the contest, but i failed the main tests after the contests and my answer is not accepted
Here is a more casework-y solution for D using at most $$$2n-4$$$ queries (can be trivially improved to $$$2n-5$$$):
First of all, query all triples of the form $$$(1, 2, x)$$$. Store the values of $$$x$$$ that yield the maximal answer.
This position $$$x$$$ must either be the sole zero, or the sole maximum of the array!!
Now query all triples of form $$$(1, x, y)$$$. If they all yield same result, then $$$1$$$ or $$$x$$$ is the zero, otherwise one value of $$$y$$$ must yield the maximum. In that case, one among $$$x$$$ or that value of $$$y$$$ must be zero.
$$$2n-4$$$ queries in all.
In this case, either the first 2 elements contain a zero, or all non-zero elements are equal and maximum in the rest of the array. We can use queries of the form $$$(k, k+1, 1)$$$ to verify which case it is. If the second case, then the value of $$$(k, k+1)$$$ giving largest answer in this step must contain the zero.
$$$2n-5$$$ queries.
Choose any $$$2$$$ element yielding the maximum. They must either be both equal to maximum of the array, or one must be maximum and the other zero. We can query all triples containing these two. If all answers are same, then one among these is the zero, otherwise, there must be a unique third index yielding the maximum answers among all triples queried in this step. That index must be the zero.
$$$2n-4$$$ queries.
Damn I was really close. Only missed the last condition :')
I cant figure out, why this solution is giving
Idleness limit exceeded on pretest 1
— https://codeforces.net/contest/1634/submission/145464144You have
Therefore if
i == k1 || i + 1 == k1
you don't ask a query but still wait for a result.exactly the same thing I was trying, would have got AC only if I didn't give up so early :(,
here is my code for atmost 2n-4 queries but using almost no case work
For B, I just took everything modulo 2 and checked if the result could be obtained by adding Alice's number to the elements of the array modulo 2. I can't believe it worked. But I felt B was a lot harder than normal
My solution to D within $$$2n-5$$$ queries: 145449958
Can you tell me, what's wrong in my code Thanks in advance!
Update: Accepted ! :)
Problem F really should be swapped to D. It only had less than 100 AC because it's the last problem.
I still can't understand problem B
If A[i] is odd, then regardless of which operation they choose, their number will change parity. If A[i] is even, then regardless of which operation they choose, their number will stay the same parity. x and x+3 are different parity, so regardless of what operations they choose, they will remain different parity. Check the parity of x compared to the parity of y after going through k odd numbers.
Since XOR and SUM(or addition) have same effect on parity
odd + odd = even
even + even = even
odd + even = odd
Hence after performing all the operations on input x there must be some parity of the result that should be same as parity of given output. Say after performing all the operations on x we get the result as odd number so output y given in the question must also be odd hence (sum(a) + x + y)%2 == 0 . And Why is it sufficient to check just the parity? Because it's given in the problem that answer always exist and parity of (x + 3) is different.
In this problem, if the answer is not Alice, it must be Bob (Jury guarantees). Vice versa.
The + and $$$\oplus$$$ operations have one thing in common: the parity of the res of A + B or A $$$\oplus$$$ B will be opposite with A only if B is an odd number. Visually:
So we can count the odd numbers in the input array. If the number of odd numbers is an odd number, the parity of the final result will be opposite to the starting.
If the number of odd numbers is odd, then the parity of the starting number and ending number must be different. So if $$$x$$$ and $$$y$$$ have the same parity, it's impossible to let Alice win. Then the answer will be Bob.
On the contrary, if the number of odd numbers is even, the parity of the starting number and ending number must be the same. So if $$$x$$$ and $$$y$$$ have different parity, it's also impossible to let Alice win. Therefore, the answer is also Bob.
For other conditions, Alice will win.
Problem F's Time Limit is 1s,to be honest,I think it is meaningless,boring and FUCKING.My solution is $$$O(n)$$$ but I used "cin" to read a char.During the last 10 sec I submitted it.Then I got TLE on 7?
And what is the fact? Some solutions about $$$O(nlogn)$$$ even $$$O(n\sqrt{n})$$$ passed the systemtests, ridiculously ,some solutions about $$$O(n)$$$ failed the systemtests or failed the pretests.
Don't you think you had the order reversed??
Your account has no submissions in the past two weeks, so evidently, you're outing yourself for competing with an alt in this round. That also makes it impossible for anyone to look at your submission and figure out what was going on.
But besides that, I'm not sure why it's the authors' fault that you chose a slow I/O method. My solution (145428158) passes comfortably using cin with the standard line of I/O optimizations.
In addition, your solution uses absurd amount of extra % operations, that are very slow when modulo is not constant.
Nowdays solutions usually requires fast io to work in time because everyone uses it. More people will send slow solutions but just with fast io and get AC if tls were oriented on slow io solutions. The fact that nlogn and nsqrtn were accepted is sad, but we can't do anything about that because these solutions are vety optimized, and we want people not struggle TOO much with linear solutions, so we can't make tl even lower. Anyway, we have pyhton solutions that works under 1 second, so cpp solutions should feel more than comfortable.
(Also, don't talk too much that you use fakes, it's kinda bannable)
Even a relatively unoptimized solution passes in 280ms, which is very comfortable:
145479475
Definitely!Just now I submitted a code with "cin" and sentences,such as "ios::sync_with_stdio(false)",to read,and then I got TLE on test 9. 145534508
Then I change nothing in my code but "cin" to an inline reading function "read()".Guess what?I got AC! 145534597
Both my code run an algorithm about O(n) and the first submission failed because of READING!
Now not really, the first submission didn't fail because of reading, it failed because of a combination of slow reading and slow query handling. You needlessly use 10 modulo operations by a non-constant modulo per query, which is pretty damn slow.
Thanks for advice bro!!I'll rethink about the code and optimize the algorithm.
I think the problem which can't make the code with right complexity get AC is stupid.
My solution to D in around $$$1.5n$$$ queries 145469547
Video editorial for anyone wanting looking
For problem F:
What is the motivation for the construction of the auxiliary array D?
Is there any solution which utilizes the fact that $$$F_n = \mathbf{A}^n$$$, where $$$\mathbf{A}$$$ is the matrix $$$[[1,1], [1,0]]$$$? Then, the problem would be like adding a bunch of geometric series, which seems maybe possible idk.
My motivation: the difficult part of this problem is clearly performing range Fibonacci updates, so our first order of business is to simplify the queries. One common way of simplifying range queries is to transform the array in order to turn range queries into point queries (consider, for example, the standard trick of storing the difference array instead of the original array in order to handle range updates/point queries using a BIT or standard segtree). When we perform standard range additions, the change in $$$x_n - x_{n-1}$$$ is zero for points in the middle of the array; in this case, we see that for points in the middle of the update, the change in $$$x_n - x_{n-1} - x_{n-2}$$$ is zero. Therefore, after performing the given transformation, the range updates influence only a few points of the array, which is what we hoped to achieve.
Thanks for the reply — I get it now.
I bow-down before my master who came up with problem B.
Why in problem D , constraints were given for n^2 time complexity as the problem can be easily solved in O(n).
Python i/o
swap(B,C)
For the pretest 3
667765307 0 150058801 880433548
of D, why my solution gave answer4 2
locally, but4 3
on codeforces? (Sorry for my poor English)There are 6 if conditions when eliminating non-zero elements. There's a typo in 2 of your if conditions, the third and fourth one. (You seem to have accidentally swapped the
v.push_back(...)
line of these 2 blocks).The idea is: if a query is equal to mx, it must contains zero, so we just push_back the common ids two queries have, not the themselves id.
If problem B was as follows : Alice starts with x and Bob starts with x+1 it woulda been much more solvable coz then one would find easily an answer to the question "What does x and x+1 mean or contribute to the solution" I don't see why the authors chose x+3 over x+1 maybe I missed something that makes it crucial to the problem
I think they chose x+3 in order to make the solution not so obvious.
why would they want to make it more solvable you have to do this thing....
We had 3 options for this number: $$$x + 1$$$, $$$x + 3$$$ and $$$x^2 - 1$$$. Third one tricks you with $$$x^2-1 = (x-1)*(x+1)$$$, so we decided it'll be too hard. We thought that with $$$x+1$$$ problem becomes easier than we want, because it forces you to think about a parity. So finally we decided to pick $$$x+3$$$ as "the golden mean". After contest I can say, that it was better to put $$$x+1$$$ instead of $$$x+3$$$, but we really didn't expect that problem B will be that hard, sorry for this.
In problem C, I can't understand why this arrangement is not Correct!!,
n = 2, k = 3,
1 2 6
3 4 5
Please Help!!
1,1,2 have mean of 1.5
Oh yes!!, I thought that the whole row average has to be integer
l=1 r=2 i=1 (1+2)/2 is not an integer
Thank you!!
Can someone explain problem B?
(x+y)%2 == (x xor y)%2 so whatever you do doesn't matter at the beginning alice and bob have numbers with different parity so at the end also they will have numbers with different parity hence only one of these can be winner
Was AI participating?
No I guess
I didn't manage to solve B but for me it's the best problem I've ever seen on codeforces. It's so simple when you know the solution. I feel extremely stupid now.
If on B we modify the fact that Bob starts with x+3 and lets say he will start with x+z ,is this problem solvable under these constraints? for z odd is the same problem, but i am interested in z even.
Can anyone tell me how you came up with a solution for E? Are there any other similar problems that use Euler circuit in a similar way?
I was able to came up with max flow solution but it was too slow. I also now how to find Euler circuit. Just have no idea how some could connect that algorithm with problem E Thx in advance
Thank you for writing editorial in this format. It helps beginners like us when hint is given instead of direct solution.
Loved B problem, so simple but did not got during contest
Solution for [problem D]:-(https://codeforces.net/problemset/problem/1634/D)
Idea:-
Disclaimer:- min and max here is index of min and max element. I am not claiming index at min to be min and max to be max but instead saying that one of them is for sure min and max.
Think of a situation, where we are able to find the max and min elements of the subarray that we have already traversed and we have the difference diff = element at max — element at min. So traversing to the next element will make changes to the current diff only if next element is smaller than min or greater than max.
We are going to update ** max , min , and diff** according to that and finally max and min is going to be our answer. (i.e if the new element is making changes to diff then update or otherwise skip)
So First, I find min and max of the first four elements.(Took 6 queries).
Second, I am iterating from the 5th element onwards and firing two query for each element
i.e ? min, "any element that is not min or max", ith element
again if the value we are getting is greater then our current diff value then we update accordingly.
Queries :- 2*n-2
Here is my submission:- Code
Hey, I had the same idea but didn't get it to work during the contest. I have thought about it and am starting to feel convinced that this cannot always work i.e. it not possible to find the max and min of the first four elements reliably. For this, consider the first 4 numbers to be 1, 1, 2, 2. Note that there are exactly 4 possible queries on these 4 numbers (each query leaving out a different number). Moreover, each query must contain a 1 and a 2 which means that each query returns 1. So no matter how your code uses the information of your 6 queries (each of them would return 1), there must be a permutation of 1, 1, 2, 2 such that your min and max both point to 1s (or both point to 2s).
I personally failed because of this issue on test 5. Since you passed all tests I am wondering whether my argument has some flaw. Or maybe not all permutations were actually checked in test 5?
That is why there is solve2 function asking for two further query. Index 1, 2, 3 ,4 Value 1, 1, 2, 2
q1-> 1 , 2 , 3
q2-> 1 , 2 , 4
q3-> 3 , 4 , 1
q4-> 3, 4 , 2
Now for each query, I will get my diff = 1 So, let's select first two queries as deciding one now since in {(1,2,3)and(1,2,4)} first two elements are in both so I am assuming that 1 and 2 is probably going to be my answer but now I will ask query two more query to verify it:-
q5-> 3 , 4 , 1
q5-> 3 , 4 , 2
now if the results are still the same then I can for sure say that one of my min or max is coming from 3 or 4. so now my updated answer will be {3,1} or {3,2}.
I wrote the solve2 function for solving this issue only. You can dry run and can check by yourself.
Thanks for your answer.
First note that your queries 5, 6 are redundant i.e. they don't give you more information than you already had.
Now, based on your example and explanation I would think that your code would fail if the sequence were 1, 2, 1, 2 or 2, 1, 1, 2. Note that all queries would yield exactly the same result even though the input order changed. In particular, your code would have to decide exactly the same on either {3, 1} or {3, 2} to be the max/min pair. But this would be wrong here i.e. {3, 1} would be wrong for 1, 2, 1, 2 and {3, 2} would be wrong for 2, 1, 1, 2.
However, test 5 checks both these cases and you pass them. So something in your code makes it work despite not having correctly found the min/max pair in the first 4 numbers.
Thanks for pointing it out.
Seems like they don't have a check for case 1,2,1,2 otherwise my code is not going to pass it.
Thanks, for making me think harder into this. I checked it once more, the fact i.e there is one and only one zero leads my code to run correctly.
Look that the code is giving {1,3} as its min-max value for {1,2,1,2} ohkk that is not correct but even after that, it is for sure is the condition when zero is not in our first four combinations. but as we move further whenever we encounter zero it will lead our diff to be maximum of all and update once of min or max to its index. And hence zero is always going to be in our min or max.
You can also think in this way that our first six operations is not for finding min or max but for capturing index of zero, if it is there in the first four combination. And if it is not then it is going to be captured in our further iteration.
Now, Hope this will help. Again thanks.
Can anyone please explain the approach for Problem E ??
There is a typo in editorial of Problem D -- $$$\overline{d} = c - a = c$$$, not $$$c - b$$$
You're right, will fix
Can anyone explain the solution to hint 2 of Problem F ?
Make new array $$$D_i = C_i - C_{i - 1}$$$ (also $$$D_1 = C_1$$$). Than adding $$$x$$$ to segment is changing 2 values in $$$D$$$. And $$$C$$$ consists only of zeroes = $$$D$$$ consists only of zeroes.
I didn't solve B but when I looked at the solution I kind of went "ooooooohh, that's really cool" Thank you, very cool
The n<=1000 constraint in task D was really misleading...
We had to lower the constraints due to, uh, Windows. Input-output is terribly slow on Win32, and if C/C++ programmers have those magic lines for speeding up I/O a bit, Python guys (as well as some other languages) have nothing like that. We wanted Python solutions to pass too, hence a lower constraint.
I have a sexy solution for problem D. It uses only ceil(3/2*n) queries.
Here it is: 145493474.
Can anybody hack it?
god E is unbelievable!!!
For problem D: Finding Zeroes: For every 4 queries that the editorial makes, you can actually do it in 3. Here's a gist of the idea.
First, we need to understand how the editorial eliminates 2 non-zero numbers amongst the given 4 numbers, $$$(x_1, x_2, x_3, x_4)$$$. WLOG, assume that $$$x_1 \leq x_2 \leq x_3 \leq x_4$$$. As given in the sample example, you query for
If you consider $$$x_i$$$ as points on the number line, then each query gives you the length of the interval. Now, notice:
Hence, we have the approach of the editorial. Query all 4 cyclic triplets, find the maximum value, find another occurrence of it, take the intersection and eliminate 2 numbers which aren't in the intersection.
Now, how to reduce this to 3 queries? We know that the maximal result has to repeat at least once. So, if we just make the first 3 queries, either the maximal result has already been repeated, in which case we simply take the intersection, or the maximal result has not yet been repeated, in that case, we know what the result of the last query is going to be (the maximal result itself), so we can avoid making the query :)
Number of queries used: For every 4 queries in the editorial, we use 3. Hence, for even $$$N$$$, we have:
For odd N
Thank you. This is helpful.
Problem B is such a nice problem and when I realize the solution of it I was so surprised!I laughed out at the same time. P.S:More B-like problem pls!
This contest is for Alphacode with E&F which is not flexible enough.
B was really amazing problem i didn't know about that concept yet but now i got that.
Problem D is really a great problem (
BTW, Why can't I use fwrite in Problem D even using fflush(stdout) at the end of every output? (It get ILEed at the first testcase)
I don't see any submissions for D from your account, huh?
https://codeforces.net/contest/1634/submission/145507436
My ILE-ed submission
The problem isn't with writing, it's with reading. You use
fread(bufr,1,MXBUF,stdin)
which blocks until it reads at leastMXBUF
bytes or until EOF, none of which happens when the interactor inputs $$$t$$$ and $$$n$$$.In case someone is interested, Video Solutions for A-D
Can problem E be solved using bipartite matching?
Yes, for each row, connect (i,2*j) and (i,2*j+1), for 0<=j for each distinct value, connect the 2*i th position and the 2*i+1 th position of this value's occurrences.
If every distinct value's frequency is even, we can see that there is no odd length cycle in this undirected graph that we just constructed.
Why? If 2 nodes are linked, either they are sitting next to each other on the array, or their occurrences as values are next to each other. Our construction allows at most 2 edges for each (i,j), and these 2 edges are of different types. If odd length cycle occurs, then it contradicts our construction.
Since there is no odd length cycle, the graph is bipartite, and thus can be coloured into equal number of black and white nodes.
The final colouring will be our answer.
Problem E is beauty. Loved it. <3
Hi I am facing trouble in problem E 145451100 it is giving WA on Test case 3 I cannot figure out why? I have followed a different approach. Please help Thank u in advance
Your approach seems to be wrong here is the simple test:
AI after reading quotes in the Problems:
A minor quibble on the tutorial for problem E, but I think an important one nonetheless:
Is it necessarily an Eulerian circuit? To my mind, rather, it is a series of circuits (possibly just one, in which case yes, Eulerian) which start and finish at the same end-point, rather than necessarily being an Eulerian circuit of the whole graph. You could consider each circuit an Eulerian circuit of its connected component.
Consider the following example:
Now label the numbers 1 to 4 as vertices 1, 2, 3 and 4, and the arrays as vertices 5, 6, 7, 8.
The two required circuits are
5 -> 1 -> 6 -> 2 -> 5
and7 -> 3 -> 8 -> 4 -> 7
. Neither is an Eulerian circuit of the graph. Instead they are distinct circuits which, together, traverse every edge.Yes, you're right. Also this remark is present in hint 3.
My take on Problem B: Reason why + and ^ have the same effect on the last bit is that a+b = a^b+(a&b)<<1, so we are sure that the last bit in (a&b)<<1 is 0. If we add 0 to any number it doesn't change so for the last bit we can ignore adding (a&b)<<1 and simply take xor of all values of a with x and x+3, the last 2 bits in x and x+3 will always be different, so exactly one of them will give the same parity as y and that is our answer.
This is a good round. But I think there is a big difficulty gap between Problem C and Problem D.
D should be swapped with F.
A bold claim! I think the order was probably correct. F is easy to understand but tough to have the right idea as seen through the low solve numbers.
For problem C, I think the simpler and crucial observation is that any A.P, as long as the common difference is even, is divisible by n (no. of elements in A.P).
Since,
Sn = n/2 (2 *a + (n-1)*d), n = no. of terms in A.P, d = common difference
To Rewrite, Sn = n * (a + (n-1)*d/2 )
Clearly, if 'd' is even then the sum is divisible by 'n', which translates to the number of shelves being even in the problem.
And now, it's easy to see that as long as the number of shelves are even, a solution will exist. And a simple solution is the A.P with common difference = n
There is a simpler solution for D. Consider fixing the first 2 numbers. Iterate over all n-2 possibilities for the last number and take the index with the maximum result (if all results are the same we can be assured that the 0 is one of the first 2 numbers). Now we know that the last number is definitely either the maximum element or the 0 element. Next take the second number and iterate over n-2 possibilities for it and choose the one that maximizes it (if all of the results are the same then the first or last number is definitely the 0). Once you have the index that produces the maximum result the second number or the last number is definitely the 0.
I don't think you are right(or I just can't get your idea).What about this example? A array a[5]={1,1,0,2,2}.If you iterate over all n-2 possibilities for the last number,you will find that all result are the same ,but 0 isn't one of the first 2 numbers.
Ok yea I forgot about this case. I’m pretty sure it still works tho if u ignore the first thing I said about terminating early if last number query never changes.
You are right.Just not rigorous in the first time.In fact,I have the same thinking with you.But because of this example,I failed to do it by myself.
Because problem B has a "dp" tag involved in it, Can someone tell, how dp can be applied to solve it?
I love how in B and D is used alternative thinking: define the answer by knowing which ones are not answer at all. Thanks for the creators of such an amazing contest!
I think AI can't solve any problem in this contest.
how can I think of the Difference of F
Finally got my segment tree accepted in F, but I had to push it very hard. It's also (in my opinion) a lot harder to code than the official solution, so I don't see any reason to punish this one. Which makes me curious, what was the motivation behind the time constraints in F?
Another approach for problem E without using eulerian circuit:
If some number occurs odd number of times, it is obvious that no solution exists.
We view each position as a vertex, and add edges between them in the following manner:
Consider each array, add an edge between the $$$2i+1$$$-th position and the $$$2i+2$$$-th position, then it forms a perfect matching.
Consider each type of number, add an edge between the $$$2i+1$$$-th occurrence and the $$$2i+2$$$-th occurrence, then it forms another perfect matching if each number occurs even number of times.
Since the union of two perfect matching is a bipartite graph, we can put one side of this graph on the left and the other side of this graph on the right.
It is (nearly) the best div.2 D I have ever met.
I wrote a solution to it in chinese link
It seems to be more difficult than the official solution :-(
Cool problems!)
I really loved the problems this time around. Since I mocked this contest, I felt solving with hints make the learning process x100 times better as I get some help but not too much and can solve the problem still kind of by myself. Finding zero was a really elegant interactive problem, and problems B. and C. were amazing. Thank you for the great practice!
Honestly I feel like the F problem is genius and really smart
Problem E is pretty similar to IMO 2020, Problem 3
I have a solution of $$$D$$$ which requires max $$$2n-3$$$ queries
B is insane. I was only able to come up with a naive dp solution and I thought that editorial solution would be some crazy formula. Btwn what if for some k, x and x + k has same parity which is also equal to parity of y? Is there a hard version of this problem?