5

Editorial of Codeforces Round 860 (Div. 2)

 1 year ago
source link: http://codeforces.com/blog/entry/114208
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
By sevlll777, history, 7 days ago, translation, In English

Thank you all for participating, I hope you enjoyed the problems! You can rate the problems of the round in the corresponding spoilers.

1798A - Showstopper

Hint 1
Hint 2
Tutorial
Solution
Rate the problem

1798B - Three Sevens

Hint 1
Hint 2
Tutorial
Solution
Rate the problem

1798C - Candy Store

Hint 1
Hint 1.1
Hint 1.2
Hint 2
Tutorial
Solution
Rate the problem

1798D - Shocking Arrangement

Hint 1
Hint 1.1
Hint 2
Tutorial
Solution
Rate the problem

1798E - Multitest Generator

Hint 1
Hint 2
Hint 2.1
Hint 3
Tutorial
Solution
Rate the problem

1798F - Gifts from Grandfather Ahmed

Hint 1
Hint 2
Hint 3
Hint 3.1
Hint 4
Tutorial
Solution
Rate the problem

2 days ago, # |

Shows me , posted 5 days ago, fastest editorial ??

  • 2 days ago, # ^ |

    It shows time when blog was created as a draft, not when it was published

    • 2 days ago, # ^ |

      Great contest, thanks :)

    • 10 hours ago, # ^ |

      thanks i was confused

2 days ago, # |

Oh, a tutorial with Python code, how avant garde

2 days ago, # |

E can be passed by brute forcing all possible positions to change. Not sure if its provable but I couldn't think of any hack cases when coding it. https://codeforces.com/contest/1798/submission/199283535

  • 2 days ago, # ^ |

    Rev. 2  

    +3

    This case can hack it.

    100000

    1 49999 1 1 1 1 ...... 49999 1

2 days ago, # |

Really great round, problems till D felt pretty solvable. Although managed to solve only 2 but had fun.

2 days ago, # |

Fastest Editorial but I wanna unsolve first...

2 days ago, # |

Rev. 3  

+4

In Problem D:

Can anyone tell me the ans of test case:

1 2 3 -30 -20

According to my understanding, the ans should be No, as sum of abs(-30 + -20) > max(3) — min(-30), but many of the passed solutions are outputing Yes

Edit: Got it, forgot to read the very first line of question

  • 2 days ago, # ^ |

    The sum of the array needs to be 0

  • 2 days ago, # ^ |

    a1 + a2 + a3 + ... + an = 0 this does not hold in your case.

2 days ago, # |

WoW! The editorial dropped faster than my rating :)

2 days ago, # |

WOW! Lightning fast editorial

  • 42 hours ago, # ^ |

    WOW! Lightning fast editorial

2 days ago, # |

Rev. 6  

+12

Problem B is very similar to Kahn's algorithm.

code
  • 2 days ago, # ^ |

    can you provide B code in c++

2 days ago, # |

Rev. 2  

+18

You can solve C by abusing the fact that LCM grows fast too.

Greedily try to make segments of the same cost. Naive is to check if you can make the cost for the current range equal to for every , but you only need to do the check if the value of LCM changes between to . If it doesn't change you already know it's possible for the range . So you just need to check if you can include in the range. 199293298

  • 2 days ago, # ^ |

    Rev. 3  

    0

    Can you tell me What I am doing wrong 199314288? Doing something the same.

    • 2 days ago, # ^ |

      Take a look at Ticket 16782 from CF Stress for a counter example.

2 days ago, # |

Did you not include a pretest with n=3e5 in D on purpose?

  • 45 hours ago, # ^ |

    Also he did not include a pretest with n=2e5 in C on purpose.So my C and D are all hacked haha.

    • 42 hours ago, # ^ |

      Also no pretest with 50000 on B, causing my rank to drop from 2700 to 7000.

2 days ago, # |

The system test of problem E is too weak. My brute force solution passed it, and it can be hacked by a very simple testcase. Please be more careful for preparing testcases.

Link:199296293

2 days ago, # |

This was kind of a hope greedy works contest imo.

In both c and d i was like "i hope this covers all cases, let's submit". I don't really like this kind of problems

  • 2 days ago, # ^ |

    That's so true man I did the same with C then I could not think about the solution of D so with 5 minutes remaining just coded up whatever came to mind and it got accepted with 15 seconds remaining hehe.

    • 2 days ago, # ^ |

      True, turns out that in d you can just assign any valid value and repeat for each element of the array and it's guaranteed to find the solution

    • 31 hour(s) ago, # ^ |

      niceee, good to know people with 1600 rating are also HOPING for solutions to cover all cases , not just me

2 days ago, # |

Please explain C someone ???

2 days ago, # |

Rev. 2  

0

In C, according to tutorial if we solve for prefixes like:

{[a1, a2, a3] [a4, a5, a6] [a7, a8, a9, a10]} have price tags {c1, c2, c3} and c1 becomes equal to c3. the solution should fail, correct me where I'm wrong.

Edit: I read the question wrong

2 days ago, # |

Rev. 4  

+33

Problem D was easier than Problem C... Is it?

  • 35 hours ago, # ^ |

    Yes, I also felt it. I regretted solving C before D.

2 days ago, # |

Rev. 2  

+55

(For problem F) I had never heard of the Erdős--Ginzburg--Ziv theorem but I guessed that it had to be true by the fact that none of the samples had "-1" as an answer.

For those curious, an elementary proof is in the second response here.

  • 10 hours ago, # ^ |

    Why that proof need to reduce the original problem to the case when n = prime?

    Does any step using the property that is a prime?

    • 8 hours ago, # ^ |

      Yes, because it assumes a-b is relatively prime with p when a != b

      • 33 minutes ago, # ^ |

        Oh, I got it. Thanks.

2 days ago, # |

In C, I really thought as long as the biggest bi (i.e max(b1,b2...bn) could divide the GCD, it should be fine. It took me like an hour after the contest ended to figure out why this wouldn't work.

Love it when my brain turns off when I need it.

2 days ago, # |

Did not participate in this round but surely not a well set round. You expect Div2C and Div2D to have a certain difference in difficulty. The D problem surely didn't deserve to be a Div2D.

2 days ago, # |

Rev. 3  

0

My Solution for problem C

Code

2 days ago, # |

Like these hints.Thanks.

2 days ago, # |

easy and cool up to problem D. However, can't solve problem E. I hope I will see a color change.

2 days ago, # |

Rev. 2  

0

another opportunity to feel ashamed....!!![ ]

2 days ago, # |

Rev. 2  

+2

In C problem , wrote the code of LCM wrong. Didnt check as i was taking GCD and LCM for granted. Can't handle more negative. Wanna die :(

Negative delta loading ...

2 days ago, # |

Thanks for the problems! Pretty interesting and balanced set.

By the way, it seems we can't view the reference solutions.

  • 2 days ago, # ^ |

    Rev. 2  

    +8

    lol, indeed, thanks for pointing it out. fun fact is that I copied template from editorial of previous round, and it is impossible to see reference solution in that editorial too, and nobody said that before. that is pretty much proves, that this references are useless, but I'll update them soon.

2 days ago, # |

How would you do D, if the sum wasn't 0?

  • 46 hours ago, # ^ |

    Basically the same idea of considering prefix sums works except you have to be a bit more careful. If all the numbers are nonnegative or nonpositive it's clearly impossible. Now, without loss of generality assume the total sum is nonnegative. Let mx be the max element and mn be the minimum element. If the total sum is >= mx — mn, then of course the task is impossible no matter how you reorder the array, otherwise it's possible.

    First, place all the 0s in the array at the front. Then, it is possible to add elements so that the prefix sums are always in [0, mx — mn). Specifically, if the current sum is in [0, -mn), you add a positive element, otherwise if it is in [-mn, mx — mn), you add a negative element.

    This strategy ensure the prefix sum will always be in [0, mx — mn) until we run out of positive elements or negative elements. Then, since the total sum is in [0, mx — mn), adding the remaining positive or negative elements will still keep the prefix sum within [0, mx — mn).

2 days ago, # |

Rev. 2  

0

<0000000000000000000>

2 days ago, # |

Rev. 2  

0

how to solve the problem C if the types can be shuffled?

2 days ago, # |

Appreciate that the solution has hints. Helps with upsolving problems beyond our range!

2 days ago, # |

About the D Problem.

max1≤l≤r≤n|al+al+1+…+ar|

Is there a way to minimize this formula?

2 days ago, # |

Would there be any efficient algorithm for Problem C when also allow reordering of the packs? Like for 20 3 6 2 14 5 20 7 21 2 we would still need 2 tags but we cannot use the prefix argument.

2 days ago, # |

For B, you can choose the elements that appear the least later on in the future and it also works.

46 hours ago, # |

The solution link in the problem F is not working. Hope it will be fixed soon.

43 hours ago, # |

I think the testcases of problem B is weak ? My nearly brute force solution 199278375 passed (1387ms) ,and you can easily find a testcase to hack my code.

  • 42 hours ago, # ^ |

    Which part is brute force? Learn meaning of it first xD. It is greedy and since everyone can take at most one place, your solution is correct. Getting 1387 ms. because for every test case (5e4), you're clearing your arrays with size 5e5.

    • 41 hour(s) ago, # ^ |

      Thanks. I should learn more.

43 hours ago, # |

gcd(a1⋅b1,…,an⋅bn) is divided by lcm(b1,…,bn) can someone prove this?

  • 42 hours ago, # ^ |

    Let be . Now let's try to price each type of candy with . For each , if cost of one pack is and one candy costs , one pack will contain candies. So must be divisible by . If we reorder that, must be divisible by . Since is divisible by , Each contains as a divisor.

42 hours ago, # |

Rev. 2  

0

why is my submission on problem c showing tle in test case 4. its a O(n) solution https://codeforces.com/contest/1798/submission/199363574

edit: i found my mistake in worst case it was going upto n^2 . thank you

42 hours ago, # |

Really great round

41 hour(s) ago, # |

Great round and a great editorial indeed. If anyone wants a video editorial for these problems (for Problem A, Problem B, Problem C, Problem D). You can watch this here — video editorial

40 hours ago, # |

Rev. 3  

-13

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include<algorithm>
#include <stdint.h>


int arr[1010][1010];
int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    long long int a[n+1],b[n+1];
	    for(int i=1;i<=n;i++){
	        cin>>a[i]>>b[i];
	    }
	    int count=1;long long int prev=a[1]*b[1]; long long int prev_fac=a[1];
	    for(int i=2;i<=n;i++){
	        long long int p1=(a[i]*b[i])/(__gcd(a[i]*b[i],prev));
	        long long int p2=(prev)/(__gcd(a[i]*b[i],prev));
	        if(a[i]%p1==0 && prev_fac%p2==0 ){
	            prev=__gcd(prev,a[i]*b[i]);
	            prev_fac=__gcd(prev_fac,a[i]);
	        }
	       else{
	           count+=1;
	           prev=a[i]*b[i];
	           prev_fac=a[i];
	       }
	        
	        
	    }
	    cout<<count<<endl;
}
}

Can anyone point out the mistake in logic for C?

37 hours ago, # |

the solution for F can't be viewed.
Also if the complexity for single si is n^3(8000000) and the restriction is 1s, how can this avoid getting TLE?

  • 37 hours ago, # ^ |

    Rev. 3  

    +3

    I pasted my code in editorial. For a more understandable and better written code, I recommend to look into jiangly's submissions (Top1 of this round).

    On practise, this is very very fast (it can be even improved to , where , with bitset), a lot of solutions fits into 50ms, and also there is Python solution that fits under 200ms.

  • 34 hours ago, # ^ |

    is just which is super fast for 1 second on Codeforces' servers.

    • 33 hours ago, # ^ |

      I didn't notice that the sum of is no greater than 200. I thought for each there is a O() dp and the total complexity will reach O()...
      can be done in 1s is pretty fast though

37 hours ago, # |

E is a beautiful problem

31 hour(s) ago, # |

In Problem C I don't understand why using GCD and LCM. can anyone explain it in detail? I really appreciate any help you can provide.

  • 30 hours ago, # ^ |

    Rev. 2  

    +10

    let the cost of each pack of candies be . So, it must satisfy that for each .

    Thus, All must divide which means must divide .

    Now, is a divisor of . let, .

    for each .

    for each .

    must be an integer which means must divide .

    Thus, must divide .

    • 10 hours ago, # ^ |

      You are the God Sir. Believe me You explained it so clearly . My whole lifetime i will not forget this(I am not exaggerating.)

      I Wish every tutorial are like this. Thanks a lot sir.

    • 8 hours ago, # ^ |

      Nice explanation.

    • 6 hours ago, # ^ |

      Rev. 2  

      0

      Is there any proof for these statements or material for reading:-

      1. Thus, All Bi must divide C which means lcm(B1,B2,...,Bn) must divide C.
      2. Thus, C must divide gcd(B1×A1,B2×A2,...,Bn×An).
      Please tell me, I am really confused.

31 hour(s) ago, # |

My contest went bad. But the overall quality of the problems are really good. I really appreciate the tutorial format too :)

27 hours ago, # |

For Problem F, the DP can be instead be done in after a recent preprint. See submission 199466476 (assuming I implemented it correctly).

Obviously, this is completely unnecessary.

19 minutes ago, # |

Rev. 2  

0

Ok, its 2 days after round, I decided to write a little postscriptum, which can answer some questions and show some insights of problemsetting/testing.

Firstly: thanks everyone who wrote comment with positive feedback about the problems, it's a big pleasure to see it, really. Second: apologize to everyone who got FST and negative delta because of it, pretests quality on problems B, C, D was poor, I'll be more careful with this next time.

Why D is easier than C, is it intended?
Story of problem A
Story of problem B
Story of problem C
Story of problem D
Story of problem E
Story of problem F

Forgive my non-prefect English, but I believe text is still understandable despite my mistakes. Thanks everyone who read that!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK