To make the arithmetic mean be equal to exactly $$$1$$$ the sum needs to be equal to the number of elements in the array.
Let's consider $$$3$$$ cases for this problem:
1) The sum of the array equals $$$n$$$: Here the answer is $$$0$$$ since the arithmetic mean of the array is initially $$$1$$$.
2) The sum of the array is smaller than $$$n$$$: The answer is always $$$1$$$ since we can add a single integer $$$k$$$ such that $$$sum + k = n + 1$$$ is satisfied and more specifically $$$k = n - sum + 1$$$.
3) The sum of the array is greater than $$$n$$$: If we add any number apart from $$$0$$$ will add to the sum more or equal than to the number of elements. The number of $$$0$$$'s to add can be found by a loop of adding $$$0$$$'s until the number of elements is equal to the sum or by the simple formula of $$$sum-n$$$.
#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int sum = 0;
for (int i = 0;i < n; i++){
int a;
cin >> a;
sum += a;
}
if(sum < n)cout << "1\n";
else cout << sum - n << "\n";
}
}
#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin >> t;
while(t--){
int n, m, i, j;
cin >> n >> m >> i >> j;
cout << 1 << " " << 1 << " " << n << " " << m << "\n";
}
}
#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> h(n);
for (int i = 0;i < n; i++){
cin >> h[i];
}
sort(h.begin(), h.end());
if(n == 2){
cout << h[0] << " " << h[1] << "\n";
continue;
}
int pos = -1, mn = INT_MAX;
for (int i = 1;i < n; i++){
if(mn > abs(h[i] - h[i - 1])){
pos = i;
mn = abs(h[i] - h[i - 1]);
}
}
for (int i = pos;i < n; i++){
cout << h[i] << " ";
}
for(int i = 0;i < pos; i++){
cout << h[i] << " ";
}
cout << "\n";
}
}
#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin >> t;
while(t--){
int n;
cin >> n;
if(n % 2 == 1){
cout << "Bob\n";
continue;
}
int cnt = 0;
while(n % 2 == 0){
cnt++;
n /= 2;
}
if(n > 1){
cout << "Alice\n";
}else if(cnt % 2 == 0){
cout << "Alice\n";
}else cout << "Bob\n";
}
}
1537E1 - Erase and Extend (Easy Version)
#include "bits/stdc++.h"
using namespace std;
string get(string s, int k){
while((int)s.size() < k){
s = s + s;
}
while((int)s.size() > k)
s.pop_back();
return s;
}
int main()
{
int n, k;
string s;
cin >> n >> k;
cin >> s;
string pref = "";
pref += s[0];
string mn = get(pref, k);
for(int i = 1;i < n;i++){
if(s[i] > s[0])break;
pref += s[i];
mn = min(mn, get(pref, k));
}
cout << mn << "\n";
}
1537E2 - Erase and Extend (Hard Version)
We know that the final string is some prefix repeated a bunch of times. Incrementally for $$$i$$$ from $$$1$$$ to $$$n$$$ we will keep the longest among the first $$$i$$$ prefixes that gives the best answer we've seen so far.
So assume the $$$m-th$$$ prefix is currently the best and we're considering position $$$p$$$. If the $$$p-th$$$ character is greater than the corresponding character in $$$s_{1..m} \cdot (a lot)$$$ then the $$$p-th$$$ prefix and any further prefixes can't possibly give a smaller answer, so we just print the current one and finish. Otherwise all the characters before the $$$p-th$$$ are all less than or equal to the corresponding characters in $$$s_1..m \cdot (a lot)$$$, so if the $$$p-th$$$ is smaller than the corresponding we set the $$$p-th$$$ prefix as the best.
Now the interesting case is if the current character is the same as the corresponding one. Say then that $$$p = m + t$$$, by the logic of the previous paragraph we must have $$$s_{(m + 1)..(m + t)} = s_{1..t}$$$. If $$$t = m$$$ then the new prefix is just the old one twice, so set $$$p$$$ as the best prefix now. This ensures that otherwise $$$t < m$$$.
Denote $$$A = s_{1..t}$$$ and $$$B = s_{(t + 1)..m}$$$, so the string formed by the current best prefix is $$$ABABABABA...$$$ and the new one is $$$ABAABAABA...$$$ Now if $$$AB = BA$$$ then these strings are in fact the same, so set $$$m + t$$$ as the new best prefix. Otherwise we can find the first position where $$$AB$$$ and $$$BA$$$ differ, and use that to determine whether the new prefix is better. This can be done in $$$O(1)$$$ with Z function, thus giving a linear solution for the full problem.
Solution by Ari.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string s;
int n, k;
vector<int> z_func(string &s) {
int n = s.size(), L = -1, R = -1;
vector<int> z(n);
z[0] = n;
for(int i = 1; i < n; i++) {
if(i <= R)
z[i] = min(z[i - L], R - i + 1);
while(i + z[i] < n && s[i + z[i]] == s[z[i]])
z[i]++;
if(i + z[i] - 1 > R) {
L = i;
R = i + z[i] - 1;
}
}
return z;
}
void finish(int m) {
for(int i = 0; i < k; i++)
cout << s[i % m];
cout << '\n';
exit(0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k >> s;
auto z = z_func(s);
int cur = 1;
for(int i = 1; i < n; i++) {
if(s[i] > s[i % cur])
finish(cur);
if(s[i] < s[i % cur]) {
cur = i + 1;
continue;
}
int off = i - cur + 1;
if(off == cur) {
cur = i + 1;
continue;
}
if(z[off] < cur - off) {
if(cur + off + z[off] >= k) {
cur = i + 1;
continue;
}
if(s[off + z[off]] > s[z[off]])
cur = i + 1;
continue;
}
if(z[cur - off] < off) {
if(2 * cur + z[cur - off] >= k) {
cur = i + 1;
continue;
}
if(s[cur - off + z[cur - off]] < s[z[cur - off]])
cur = i + 1;
continue;
}
cur = i + 1;
}
finish(cur);
}
#include "bits/stdc++.h"
using namespace std;
const int N = 2e5 + 10;
vector<long long> adj[N];
long long s[N], n, m;
bool bipartite()
{
bool bip = true;
for(long long i = 0;i < n;i++)
s[i] = -1;
queue<long long> q;
for(long long i = 0;i < n;i++){
if(s[i] != -1)continue;
q.push(i);
s[i] = 0;
while(!q.empty()){
long long v = q.front();
q.pop();
for(long long u: adj[v]){
if(s[u] == -1){
s[u] = s[v] ^ 1;
q.push(u);
}else bip &= s[u] != s[v];
}
}
}
return bip;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
long long T;
cin >> T;
while(T--)
{
cin >> n >> m;
for(long long i = 0;i < n;i++)
adj[i].clear();
vector<long long> v(n), t(n);
long long p1 = 0, p2 = 0;
for(long long i = 0;i < n; i++){
cin >> v[i];
p1 = (p1 + abs(v[i])) % 2;
}
for (long long i = 0;i < n; i++){
cin >> t[i];
p2 = (p2 + abs(t[i])) % 2;
}
for(long long i = 0;i < m;i++){
long long a, b;
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
if(p1 != p2){
cout << "NO\n";
continue;
}
if(bipartite() == false){
cout << "YES\n";
}else{
vector<long long> c(2, 0);
for(int i = 0;i < n;i++){
c[s[i]] += v[i] - t[i];
}
if(c[0] == c[1]){
cout << "YES\n";
}else cout << "NO\n";
}
}
}
This was faster than the system testing!
Thank you!
Codeforces should rename itself as adhocforces or mathforces
Can you AC mathforces and adhocforces without writing Code?
destroyed in seconds xd
noice
We can then have another coding language Mathular/ Adhocular for this website.
top 10 responses that led to suicides XD
Horribly weak pretests for E1 and E2 :(
Not to mention long queues.
Round should be unrated!!!
MikeMirzayanov Monogon mesanu SlavicG
My guy, you haven't seen long queues, this one was no longer than 3 mintues. I saw 40 minutes long queue. Also, if you didn't think before submitting E1 or E2, it's on you. Please refrain from posting such non-sense next time.
Can someone explain why long queues mean the round might be unrated? Is it cause we'll not be able to check whether our solution has passed?
Pretty much so, but the queue has to be extreme to lead to an unrated round.
Although we regret not making stronger E tests, poor test quality isn't a reason to make the round unrated.
monogon coordination could use improvement, needs some time before can take over cf :(
weak pretests is not a problem for problem E ,because if we get the correct logic it will automatically pass. As it is not a math question no need to depend on pretests. Btw contest is really nice.
Yes bro, totally agree with you.
I used suffix array and failed system test miserably. Should have think about the special case more carefully. Interesting problems anyway. RIP my rating.
F
https://chrome.google.com/webstore/detail/cp-calendar/gnnibhojjocbjpkhhlphelmiiffodajj
What's the issue with pretests? Difficult problems are meant to challenge the coder. If the organizers give you large pretests so that you can find every corner case with ease, it would be useless.
Round is fine.
Difficult problems are indeed meant to challenge the coder. But I don't think that means you make a person think he's got the answer right when he hasn't, hence making him solve the next question when he hasn't solved the previous one correctly.
Also, I don't think giving WA on the pretests would as such help too much in finding the corner cases, its just to tell the contestant that you haven't yet got this question right and he should continue to work on it.
Round is fine though, only a minor issue which isn't enough to make it unrated.
I did fall prey to the weak pretests of E1. (Pretests passed but not accepted)
But I don't think that's enough reason for round to be unrated. Questions were good and above all, it was my fault that system tests didnt pass.
Round should unrated only if questions are super ambiguous or online judge takes too long to give verdict on submission
Adhoc Adhoc everywhere :0
I enjoyed the contest btw and I really wish you guys make more of these. Kudos (Y)
I'm such an idiot for not solving B but solving C
How accepted solutions for problem E1 rose so quickly ?
https://www.youtube.com/channel/UCm-7dkk1fHId1hy5vY5VVCQ
I was wondering the same about D too. Found why.....
This is horrible :(
Everyone should report the channel now!
I am relatively new to CF. Any idea what is going to happen? Will the round get unrated or is the plag checker capable enough to remove any plag solutions?
Plag checker usually removes people from rankings
nah man D was on gfg also, I think that's also a major reason
Hey, there are many telegram grps exist where they post solutions till E2.
No original problems, just dumb adhoc problems
F:"It is guaranteed that the graph is connected."
There seems to be no guarantee of this condition.
It says the graph is connected in the first sentence and again in the input section.
In problem E, one can prove that
So just compare the prefixes using Z function.
I have done the same. But my answer is failing for test case 43 in E1 and 93 in E2
Me too, and my fault is that I forgot to judge a sqecial situation, when a suffix of the string is also its prefix, you need to cut out the suffix to get a better answer.
Prove:That's because if you found $$$S[1,n-i+1]=S[i,n]$$$ ,it means you've already judged suffix starts from 2 to i-1, ans all of them are smaller then itself, now we judge if we cut the $$$i^{th}$$$ suffix, if we cut it, we will connect itself to it after cutting(we call it s1 ans the one that didn't been cut s2), and it can be proved that the first place that may be different is the $$${n+1}^{th}$$$ place of s2, and it is the initial string again,for s1, it is the suffix from (n mod (i-1)+1) we know that if s[x] means the suffix from x,$$$s[j(1\le j \le i)]\le s[1]$$$,so $$$s1\le s2$$$, we need to cut it.
How do you check if a suffix of the string is also its prefix? I did using set(stored all prefixes and then created suffixes one by one) but got MLE.
Catch Jiangly and ORZ%
What's the intuition behind this equivalence? After thinking for a while I was able to find a proof, but how do you come up with it in the first place?
I have solved a problem about arranging some strings, where I needed to prove the latter relation is transitive, so I discovered and remembered this equivalence.
Will you please provide the proof for the above relation?
I'm a bit late, but here is some more or less intuitive proof. We can look at strings as base-p numbers (here $$$p=26$$$, but it doesn't really matter). Obviously it's enough to compare string consisting of $$$|b|$$$ copies of $$$a$$$ with string consisting of $$$|a|$$$ copies of $$$b$$$. In other words, we want to compare \begin{gather*} \sum_{i=0}^{|b|-1}a \cdot p^{|a|i}\quad vs \quad \sum_{i=0}^{|a|-1}b \cdot p^{|b|i}\\ a \cdot \frac{p^{|a||b|} — 1}{p^{|a|} — 1} \quad vs \quad b \cdot \frac{p^{|a||b|} — 1}{p^{|b|} — 1}\\ a(p^{|b|} — 1) \quad vs \quad b(p^{|a|} — 1)\\ a\cdot p^{|b|} + b \quad vs \quad b\cdot p^{|a|} + a \end{gather*}
And the last line is just $$$a + b$$$ vs $$$b + a$$$ (here $$$+$$$ is concatenation)
I cannot understand what do you mean by $$$a.p^{|a|i}$$$ in the summation formula. Can you please elaborate more?
To concatenate numbers you need to multiply one of them by some power of base. For example, if $$$a = 123$$$ and $$$p = 10$$$, then $$$|a| = 3$$$, and \begin{gather*} 123123123 = 123\cdot 10^6 + 123 \cdot 10^3 + 123 \cdot 10^0 = \sum_{i=0}^2 123 \cdot 10^{3 i} \end{gather*}
Wow, amazing way to see strings. Thank you very much.
Thanks man! Its amazing how things simplify after you see strings as numbers with base 26.
Can you please tell what proof you came up with? Thank you.
.
For D, I just printed the answer for the first 100 natural numbers and found the solution...
Hopefully, I will become Orange today after 15 months :)
What did you do? How did you simulate each number? I believe you need to know the optimal move, but if you know that then you actually know the answer
I used dp to calculate the answer.
Let dp[i] = 1, if alice can win when n=i,
Simply, for any j which divide i,
If dp[i-j] is 0 then, dp[i] is 1.
I used this method to find answer for 100 natural numbers, then I guessed the sequence.
so you became master after all!!! gib tips
Got it, thanks
can you explain it further ?
Now you're orange. Congrats
Were you familiar that these type of questions are pattern finding or did you first try to approach the question using paper and pencil before brute force.
Generally (most of which I encountered), game theory questions can be guessed by writing the answer for some initial numbers.
thank you.
same))
I was thrilled to find I was able to do 5 questions only find they were of div3 difficulty overall nice round thanks for the contest
Woow !! fast editorial Thanks :)
Problem D: Can someone explain me the reason for "If number is odd then opponent (BOB) will always win"?
I didn't get it.
Thanks
for odd you will have divisor odd ,now if you subtract odd number from odd number you will find even number for even numbers you may refer above in editorial
If n is odd, then in the first move Alice have to choose odd divisor. and if u subtract a divisor from a number then that divisor also remains divisor of the new n.
so bob will just choose the previously taken odd divisor of alice, and since the number is odd so it will be zero after odd time of subtraction. (odd*odd=odd). and the last subtraction will be made by alice (because total number of subtraction is even),and alice will make the number zero means he is subtracting the complete number so alice will loose.
Hope you will understand thanks!!.
yes I got it, thanks!
welcome bro!
The problemset was fairly interesting.
Check out my simple greedy solution for E2. 119887467
Can you please explain the else if statement?
You can cosider the given testcase s= dbcadabc. When i=4(0 based indexing) s[i]==s[j], then we will take this character in our prefix if the next character {(i+1)th} is less than or equal to s[1], which is 'b'. If that also came to be same then we will check the next one, like in this example , s= dbcadbbc, we will compare s[6] with s[3].
I know it's a bit hard to understand, but I have tried my best :)
How did you come up with this idea ?? Is there some algorithm or practice?
I simply followed the greedy approach to get the lexicographically smallest string. But, some people are talking about some Z function, I have no idea about it.
I also followed the greedy approach but im getting runtime error, can u pls check what's rong!! heres my solution for E1 : https://codeforces.net/contest/1537/submission/130205622
Underrated
Very clean soltuion!
Great solution.. I saw many solutions but all of them talked about some Z function.. I am glad I ran over this one.. Thanks alot
nice solution
Thank you for making this problem soo simpler to understand.
One point I would like to mention is: In second condition we don't need to take modulo, as j will always be smaller than i and even if j exceeds (last+1), substring starting from s[0] will be equal to substring starting from s[last+1].
link to solution
Yeah, you are right, there is no need to take the modulo.
Problem statement was quite clear.awasome explanation.
contest was really good but a little bit easy than other div2 rounds
E1 Test 43 should have been a pretest.
Problem D is amazing!
For E2, I have a simpler solution that, I believe, implicitly uses the Z-function concept, however is not at all a prerequisite to understand. Also, I’m not 100% sure about correctness; feel free to hack in that case.
The editorial for E1 establishes the fact that the optimal answer is merely a repetition of a prefix. We will check which prefix length is optimal to be repeated, starting from $$$1$$$ to $$$N$$$. Let the current optimal length of prefix be $$$len$$$.
The casework is similar to what is described in the editorial for E2:
If $$$s_i > s_{i\, \%\, len}$$$ (0-based indexing here), the prefix should not be extended any further; no prefix beyond this point is optimal, and it is optimal to keep repeating our current optimal prefix. So we simply break.
If $$$s_i < s_{i\, \%\, len}$$$, it is more optimal to extend our prefix up to this point; hence we do it, i.e. set our optimal prefix length to $$$i + 1$$$. This needs some more explanation, so bear with me for the next couple of lines.
There’s also the more interesting case of $$$s_i = s_{i\, \%\, len}$$$, but before that, let’s establish an invariant: So far in the algorithm, the string made by the repetition of the prefix, and the substring $$$s[0:i]$$$; both are equal. How? Well, we break when $$$s_i > s_{i\, \%\, len}$$$, and in case of $$$s_i < s_{i\, \%\, len}$$$, we simply extend our prefix up to here, making it equal.
Let’s deal with the $$$s_i = s_{i\, \%\, len}$$$ case now: Here, we cannot decide at this moment, whether repeating the current prefix or extending the prefix up to this point is more optimal. Hence, we just proceed ahead.
Further along the line, if $$$s_i > s_{i\, \%\, len}$$$, we realize that repeating our current prefix is indeed the most optimal, and we need not check further, just like in the previous scenario. Hence we break.
However, if $$$s_i < s_{i\, \%\, len}$$$, we realize our prefix repetition was non-optimal; how do we go back, i.e. change our prefix now? That’s where our invariant comes in. Our prefix repetition string was equal to the substring $$$s[0:i - 1]$$$, and becomes unequal at index $$$i$$$; hence extending our prefix length simply means changing the prefix to $$$s[0:i]$$$.
This helps us maintain an $$$O(N)$$$ solution. 119889474
Your code is nice and simple. But didnt understand your casework for the case when si = si%len (in your code, you are not dealing with this case separately as well).
You say "Here, we cannot decide at this moment, whether repeating the current prefix or extending the prefix up to this point is more optimal. Hence, we just proceed ahead.", but you are never comparing these two possibilities in further execution. You are just assuming that current answer will be better until you find the next mismatch.
For example, your third last paragraph says that you are breaking the loop if you found si>si%len, but this just tells you that the retained prefix is a better answer than current index, however you are still not comparing with the other possibility which you ignored previously. What if you would have achieved a better answer using that prefix?
Thank you for pointing this out. I believe my write-up is still incomplete, and the $$$s_i = s_{i\, \%\, len}$$$ case requires another explanation. Let me rewrite this particular case.
When $$$s_i = s_{i\, \%\, len}$$$, I now claim that maintaining our original prefix is always more than, or equally optimal to changing to this prefix.
Let the current optimal prefix be $$$x$$$. The prefix we are currently considering will be of the form $$$x + y$$$. We basically need to compare the strings $$$(xxxxxx…)$$$ and $$$(xyxyxy…)$$$.
The first mismatch happens at the second place ($$$x$$$ and $$$y$$$), so let’s check there. Let’s consider the second $$$x$$$ in the first string. The corresponding substring in the second string will be $$$y + pref_{len(x) - len(y)}(x)$$$ (some prefix of $$$x$$$). $$$x$$$ can also be broken down into $$$pref_{len(y)}(x) + suf_{len(x) - len(y)}(x)$$$.
From our invariant, we have $$$y = pref_{len(y)}(x)$$$ (we have only encountered $$$s_i = s_{i\, \%\, len}$$$ since the last update). Hence, we are simply left with proving that $$$pref_{len(x) - len(y)}(x) >= suf_{len(x) - len(y)}(x)$$$ to prove that our current prefix repetition is optimal.
Let’s assume $$$pref_{len(x) - len(y)}(x) < suf_{len(x) - len(y)}(x)$$$. This means there exists an $$$i$$$ where $$$pref(x)[i] < suf(x)[i]$$$. This can actually be avoided by repeating a prefix smaller than our current optimal one, but this shows that a smaller prefix length yields a more optimal answer, which violates the invariant of our algorithm. Hence, this case is impossible. (I think this can be proved formally, but for now, I can’t; not enough brain-cells remaining.)
Hence, in our algorithm, we make no update to the optimal prefix length, and proceed forward. This should make the solution more complete, if not fully.
Yes, now your solution is complete :) I`ll also suggest the authors of the editorial to mention that in the si = si%len case, its safe to assume sticking to the current prefix is a better choice than switching.
Can you please prove or explain these two things:
From the third paragraph, "Hence, we are simply left with proving that $$$pref_{len(x)−len(y)}(x)>=suf_{len(x)−len(y)}(x)$$$ to prove that our current prefix repetition is optimal." Why does this imply our current prefix repetition is optimal? I think in the case of equality $$$(pref_{len(x)−len(y)}(x) = suf_{len(x)−len(y)}(x))$$$, we also need to check further by comparing the string $$$xxxxx...$$$ starting at the third $$$x$$$ with the string $$$suf_{len(y)}(x) + yxyxy...$$$
From the last paragraph, "This means there exists an $$$i$$$ where $$$pref(x)[i]<suf(x)[i]$$$. This can actually be avoided by repeating a prefix smaller than our current optimal one, but this shows that a smaller prefix length yields a more optimal answer, which violates the invariant of our algorithm." If $$$len(y) > i$$$ (0-based indexing), we can repeat the prefix of length $$$len(y)$$$. However when $$$len(y) \le i$$$, I couldn't find any prefix of $$$x$$$ to repeat that yields a more optimal answer.
Thanks mesanu SlavicG for such a great round . Today I got my best rank .
I am getting a runtime error for the solution of 1537C - Challenging Cliffs. Will anyone please help me. Here is my solution submission:
Thanks in advance!
Your value of mini is — 9e7 + 9999999. It should be 9e8 + 99999999 :)
or just use INT_MAX
In order to avoid this kind of mistake, I suggest initializing mini as the difference between the first two elements of your sorted array, as n is at least 2, and just do the loop from i=1
When you brute-force problem D for small N and spot the pattern :
I finished E1 10 seconds before the round finished. Wasn't able to submit :(
To get difficulty n−1, we need mk to be the shortest mountain and mk+1 to be the tallest mountain. This will only happen if n=2.
Regarding the Problem C tutorial, This can also happen for n>2 when all elements are the same, isn't it?What does mk mean?
I see a lot of people getting relatively low positive delta (even negative delta) because of the cheaters. This is so unfair!
Don't worry mike will kick out the cheaters by tomorrow.
In problem E, can someone please prove why it's optimal to keep duplicating a prefix? The editorial's proof isn't clear to me.
Think like this, might be helpful. Without loss of generality in the first step you have to double some prefix (after deleting some characters from the end). As these are the only two operations available. So initially you want to double something which gives you maximum advantage i.e it is at least smaller than the initial string given to you, in fact, you will select the very first occurrence of the prefix that when doubled is smaller than the original string given. So in brute force double every prefix and check if it is smaller than the original string. When you find one break. now suppose you have selected prefix P and now you are getting PP after doubling. Now you perform the same step as above i.e finding some prefix of PP which when doubled gives smaller string than PP. And I can assure you , you will not find any such prefix in the new string PP which will give you advantage as in earlier case (Just work it out). So you are left with only option of repeating P, as this will at least not give you lexicographically bigger string. Hope this helps.
I couldn't prove either of these.
let s="abca" k=10, P="abc" then PP= "abcabc" and PPPP= "abcabcabcabc" .But from PP if we take PP'="abcab" then PPPP'= "abcababcab" which is smaller than PPPP.Your proof fails here.Correct me if I am wrong.
Why will you choose initially abc? Choose initially P="a", that will give smallest lexicographic string.
Nice round! Easy version of D. I got bamboozled because of it.
Codeforces 726 Div2:E1 solutions implementations: so innovative these guys : ) (not even in variables in some solutions and getting +100 or so)! 119889554 119889070 119884298 119890054 PS: check their other solutions similarities in every aspect! Please,Strict actions should be there. MikeMirzayanov (sorry for disturbing)
Mass-copying
frustrated with so many cheaters among indian college students
such a binary person
Has anyone managed to pass E2 using binary search and hashing. I know in that 2s TL it won't pass as it will be nlog(n) and is giving TLE. But now I saw binary search in the tags. So just curious to know has anyone passed using binary search.
119900135 one of my friends solution.
Thanks, man. I was calculating the power inside binary search which was creating an extra log factor, precalculating power fixed the issue.
Can you explain your binary search based solution please?
sure, Actually, I am simulating the Z function using binary search as I don't know Z function :(
First precalculating the hash of the string and the prefix hashes. Now if for each index i I want to find out the first index where the prefix from 0 to i-1 and string starting at i differ.
To do that I am using binary search. Before the first mismatch of both strings, both the hash values will be same , using this fact in binary search to find the first mismatch character and checking whether it is smaller or larger than that position in the prefix.
Two questions:
Is your hashing collision free?
Can you explain why are you doing this "Now if for each index i I want to find out the first index where the prefix from 0 to i-1 and string starting at i differ"
Well technically no hash function can be collision free I guess. But the probability of collision is very very less I believe.
Also for this "Now if for each index i I want to find out the first index where the prefix from 0 to i-1 and string starting at i differ". As far as I know Z function does exactly this, i.e it returns the longest prefix where s and the string starting at i match. And secondly I need to know this to know whether doubling this prefix will give me better answer or not, by comparing the first mismatch.
https://chrome.google.com/webstore/detail/cp-calendar/gnnibhojjocbjpkhhlphelmiiffodajj
Can someone explain the thought process/idea behind D? I am finding it difficult to grasp the editorial.
if n is odd, for eg 15, so their factors are 1,3,5,15(that it's all odd) you can see if we subtract any divisor D, the number will be even, but it cannot be a number of the form 2^k, because
so this implies if the starting n is odd, then Alice will make it even, and similarly, Bob will make again the number odd since a prime number is either 2 or the odd number, so Alice always gets the odd number, and hence Alice will lose.
if n is even and not in the form of 2^K, so similarly Alice will always make it odd and so, Alice will win.
Now, if n is of the form 2^K, then k times the game will be played, if both play it optimally, otherwise the one who don't half it will always lose, for eg 16 = 1, 2, 4, 8, 16, if Alice chooses 2 then it becomes 14 and it is the even number (not in the form of 2^K), so Bob will win. So, both try to half it at every step, so if k is odd then Bob will win otherwise Alice.
Yeah, understood it. Thanks :')
can you pls explain why this condition always holds "so n-D, cannot be in the form of 2^K." ?
Numbers in the form of 2^k have only even divisors, but here, a number n has an odd divisor D.
I`m seeing solutions for E2 where people are not dealing with the case si = si%len at all (going by the editorial, that part is important as well).
Can anyone explain how these solutions are working?
Example: Here
Just a small detail and boom, lost 2 problems :(
I don't really get how my solution to problem E2 works lol. First of all, I only chose the shortest prefix which is lexicographically smaller than the whole string. Moreover, I found this first prefix naively by comparing char by char after compressing same consecutive characters. (So the string "aabcccb" gives the array {2, 1, 1, 3, 2, 1}. Each cell is the length of the jump to first different char). I don't understand why it is correct and why I don't get TLE. If someone could explain it would be cool. Solution: https://codeforces.net/contest/1537/submission/119930955
The sample code solution for 1537C — Challenging Cliffs seems wrong, or not?
It'd produce $$$m_{k+1}, m_{k+2}, ..., m_{n}, m_1, m_2, ... m_k$$$, which is not what is described in the tutorial.
Or have I misunderstood anything?
E1/E2 should contain multitests and every short(length<=4) string should checked the pretests :(
System testing was slower than internet Explorer. XD!
Problem F is nice.
Tried to cover all the observations in my solution video :) Explanation Link
I think just a KMP-like algorithm can solve E2 by greedy.
Maybe there is a problem which used a conclusion pretty like problem F(in Mandarin) You can see the conclusion there(also in Mandarin)(maybe u can use google translate :))
For E2: At i-th position, I just compared the i-th prefix with the n-th prefix and got AC 119938778
Loved your solution
I guess problem D was the most interesting one rest were pretty easy!
MikeMirzayanov I think the rating points given for this contest needs to be re-evaluated, as I didn't solve any problems, with -3 penalty, still I got +37 for this contest whereas all others who solved one or more problems got negative points, pls take a look.
Extra points for the first six contests.
My Simple greedy Solution for E2 119941150
Does anyone come up with answer for D without bashing a bunch (like first 50) of small case? It wasn’t until I write a code to look at first 2000 answers that I spot the $$$2^k$$$ exception. I want to know the intuition behind this pattern if someone come up with it without bruteforce pattern to look.
I tried to find some strategy or some sort of invariant which Alice can use to win. I wrote $$$n$$$ as $$$n = 2^k N$$$. Alice can give Bob $$$N(2^k - 1)$$$ which has all odd divisors and Alice is guaranteed to get an even number again. Now only exceptions are when $$$k = 0$$$ or $$$N = 1$$$.
wonderful solution B,i can't realize this solution in contest so that i write 84 line code to ac...
include<bits/stdc++.h>
using namespace std;
define ll long long
int main() { ll t; cin>>t;
} can anyone tell me where am i wrong in this code?? this a solution of c
You forgot cin >> n;
UPD. In the first loop, where you read the array, you have cin >> a[n]; instead of cin >> a[i];
insight I used for E1/E2:
code for reference link
problem D was same as this leetcode problem: link
But LeetCode problem gives $$$n \le 10^3$$$, whereas the Codeforces edition has $$$n \le 10^9$$$. This makes the problem drastically harder because a naive memoization/recursion will work on the LeetCode, but not the Codeforces rendition.
Help!!! Why my solution to problem E2 failing at case like 'baaaaaaaaaa...'. I precomputed the LPS and used it to compare the characters. Checker cmnt is also not showing any difference.(https://codeforces.net/contest/1537/submission/119930117)
this might help: https://codeforces.net/contest/1537/submission/120387463
thnx man, that helped me. The problem was actually in the lps function.
I'm glad that it helped!
I had a different solution for D. Calculate the prime factorisation of $$$n$$$.
Let it be, $$$n=p_1^{k_1}\cdot\ p_2^{k_2}\cdots\ p_m^{k_m}$$$
Let $$$S=(p_1-1)*k_1+(p_2-1)*k_2+\cdots +(p_m-1)*k_m$$$
Now we can reduce $$$n$$$ until it becomes some $$$p_i$$$ or $$$1$$$.
Then possible moves players can make are: $$$S -p_i $$$ for some $$$p_i$$$ in prime factorisation of $$$n$$$ .
Check if any of these possible moves are odd, then there is always winning state for Alice otherwise Bob will win.
Let $$$n=2.2.3.3.5$$$
Then you can reduce $$$n$$$ to either $$$2,3$$$ or $$$5$$$.
Let's say you reduce $$$n$$$ to $$$2$$$.
So total moves can be:$$$1+2+2+4=9$$$
Think it like $$$(2.2.3.3)$$$ times $$$5$$$ so to remove $$$5$$$, you need $$$4$$$ moves since after $$$1$$$ move it becomes $$$(2.2.3.3)$$$ times $$$4$$$... and so on.
Thefeore total moves are :$$$4+2+2+1=9$$$
Same goes with other prime factors.
So total moves players can make are: $$$9,8,6$$$ if reduced to $$$2,3,5$$$ respectively
Now if we have odd moves there is always a winning state for $$$Alice$$$ since, $$$Alice$$$ starts the game else $$$Bob$$$ will win.
Solution:https://codeforces.net/contest/1537/submission/119883715
That's a nice approach to the problem. But as $$$\sum(p-1)$$$ is even $$$\forall p\ ( p \in prime \land p \neq 2)$$$, we only need to check power of $$$2$$$. For an odd number of moves either $$$n$$$ must have an odd divisor or power of $$$2$$$ must be even. I think this then reduces to the same as the editorial proof.
Yes, you are correct! Nice observation!
https://codeforces.net/contest/1537/submission/119973061 this shouldn't pass right? or some proof or logic explains why it passed?
Why 2-pointers is not in the tags for E2 ? submission . EDIT: Now 2-pointer is there.
This round was easy compared to previous div2 rounds
How to solve problem F if
It is NOT guaranteed that the graph is connected
. Then do we have to check for each connected component separately??Huh?
You have a connected undirected graph made of n nodes and m edges
. It literally says it's connected.No, I'm saying what is if the problem said that its not necessary that graph is connected then how to solve that problem !!
Then check for each connected component.
Really poor Editorial for F. You just stated a bunch of necessary conditions and didnot bother to prove that they are sufficient. can mesanu or anyone explain why sums of both colors being equal in the bipartite case is sufficient ?
For Bipartite:
Let's suppose two colors are black and white. We can solve for black by adding using the edge connected to those nodes. With this, we also get that we change white by that amount, so the sum in white is the desired sum.
Now, suppose some node is not working yet. Then, it must be in white and there must be another node that still doesn't work. Find those two, then we find any path connecting those. This path will have an even length (because we traverse from white to white) thus we can do something like +a, -a, +a, ..., -a. This operation would increase the first node by a, reduce the endpoint by a, and do not affect the rest. Thus, we reduce down one white node. Doing this until no node (that doesn't match) left is possible since the sum in white is already the sum we want after we doing black. So, we are done.
For the sake of completion, let me explain the case of not bipartite as well.
We can do the same coloring scheme, and we can increase or decrease the sum of white color to match that. Then, we delete those out and left with a spanning tree which is bipartite. Thus, we can follow the same algorithm to solve those nodes colored in white.
Can u explain the case for not bipartite again?? What are u deleting to get a spanning tree??
Suppose we get a (non-bipartie) graph G, we can find one spanning tree called T. Then, we color black-ans-white corresponds to T. Since G is not bipartite, there exists node connecting same color. WLOG, it’s white. Then, we can solve for black in the same manner, and change sum-value of white using that node. Finally, we focus back to T and use only edge inside T, we would get same scenario as I described in bipartite case.
E1 editorial not clear. the proof part
someone help me pls
Nice problem, not very difficult but make me cry. qwq
anyone got the binary search solution for e2? i'm not quite familiar with that z-function (i'm planning to learn it soon, but it would be nice to know different solutions)
anybody give me the link of video solutin of e2 problem ,i learned about the z function but i did'nt understand the solution and editorial please help me
In problem F, when the graph is bipartite, I can understand that when the condition "sum1 equals sum2" not holds, the answer is No. But I can't understand why answer is Yes when the condition holds.
I got a greedy algorithm on E1 problem, and I failed test 5. Can you tell me why I was wrong? Thank you! My solution: https://codeforces.net/contest/1537/submission/119877862
try 4 8 cacb
for index i, just checking if it is greater than s[0] or not and then replacing rest of the part is not enough. Even if(s[i]==s[0]),check if(s[i+1]>s[1]) and so on. If s[i+j]>s[j] such s[i+j']==[s[j'], 0<=j'<j then also s can be repeated from i onwards. Consider the case
correct ans is 'dcbdcb' not dcbdcd
I see. Thank you !
I don't understand the proof in the E1. "Let's relax the requirement so you have a position in the string and each time you either return to the beginning or advance to the next character." What is meant by above ?
This is the easiest "Two pointer approach" for E1 and E2.
119946716
In Question C if input is 7 1 1 1 2 2 2 2 the max clifs possible if 3 while program will give 1
My Solution of E2 (and E1 as well)
https://codeforces.net/contest/1537/submission/120335720
I greedily pick the best prefix. Though I read the editorial for this problem I am not sure why the code implementation was so long.
PS: MY PURPOSE OF POSTING THE CODE LINK IS THAT MAYBE SOMEONE CAN PINPOINT IF I HAVE WRONG LOGIC OR CODE AS other COMMENTS MENTION WEAK TEST CASES FOR THE PROBLEM.
For People Who are stuck/Confused on Z function https://www.geeksforgeeks.org/z-algorithm-linear-time-pattern-searching-algorithm/
For E2 :
Let's say we have three pointers A, B, and C. Where C represents the best prefix of the string which when repeated and then shaved off from the end to reach a length k would give the correct answer. A is the pointer in the best string, that we have found until now, and B is the pointer to the current character in the string we need to compare with A.
Now, let's start with A = 0, and C = 0, and B = 1, which means that a string containing only the first element is the best answer until now, and we need to compare s[A], and s[B] now.
There can be three conditions:
$$$s[A] < s[B]$$$ : In this case, since the newer character is more than the first element, we should reject it. Why? Let's say we have "dbcae", when you have A=0, and B=4. Now in this case 'd'<'e', so we will reject 'e' and declare the current best as the answer, which in this case would be "dbca". Now, if we repeat it, let's say for k=8, then it would become "dbcadbca". But in the case, we took in the 'e', it would be "dbcaedbc", which is lexicographically bigger. So no matter if anything smaller than this is present in the string, the answer can not be made better. So we end our loop here, and just print the answer.
$$$s[A] > s[B]$$$ : In this case, the newer character is actually better than the original character. So the answer is actually better off using the newer longer string, instead of the older one. So in this case, C is set to B (the new best), A is reset to zero, and B is increased by one.
$$$s[A]==s[B]$$$ : In this case, we don't know whether we are going to get something better, or not. So if we have "dbcbdbca", and A=0,B=4,C=3 then you have s[A]=s[B]. Now this repetition can actually lead to something that is better than repeating the original answer. Like for example, if we move forward, s[3]>s[7], which means that C=7 is the correct answer,instead of C=3. As "dbcbdbca" is better than "dbcbdbcb" (if k=8). So in this case, we just simply increase A, and B. Just a little corner case though : if A has reached C (the former best), it would mean that the original string has just repeated itself, then there is nothing more to compare. The B value is actually a better choice, so C=B, A=0 and B++ is done.
This loop is followed until B reaches n.
Here is a submission using this approach : submission
I am unable to figure out the issue with my problem F submission. I am simply following 3 steps:
I am trying to find the bug for the last 2 hours but no luck so far. Please help.
mesanu
maybe you have an issue with bipartite checking? thonk
Just now, I submitted this for this straightforward bipartite problem, and it got accepted w/o any change in logic
if(sum % 2 == 1)
— issue was here. Ifsum
is odd and negative thensum % 2
will be-1
, not1
Problem D is really amazing and its tutorial is great!
I know I'm late on this, but I just noticed that problem D is identical to a problem from the MIT Primes 2019 G5: https://math.mit.edu/research/highschool/primes/materials/2019/entpro19.pdf.
I know that doesn't mean much since the problem statement is generic enough to show up in multiple locations, but it's something interesting to say the least :D
Intuition behind problem F:
Think of three nodes in a graph which have edges between them a -> b -> c.
Now consider how we can manipulate values,
So now mentally divide the points into two groups A and B, such that each node in a group is at odd distance from all the other points in that group. This is nothing but a bipartite graph.
So (1) basically allows to increase the total values of both A and B by any common value and (2) basically allows us to rearrange values however we like in one group of nodes in bipartite graph.
So if the graph is two-colorable, just check if the total delta between actual and required values is equal in both A and B. If the graph is not two colorable, that means we can also rearrange values between A and B => just check if total delta between actual and required values is even. (Since we can only change total delta by 2 * x at any operation.)