cry's blog

By cry, 13 months ago, In English

2148A - Sublime Sequence

Solution (written by cry)
Code (C++) (MikeMirzayanov)

2148B - Lasers

Solution (written by cry)
Code (C++) (MikeMirzayanov)

2148C - Pacer

Solution (written by cry)
Code (C++) (litfan)

2148D - Destruction of the Dandelion Fields

Solution (written by cry)
Code (C++) (AksLolCoding)

2148E - Split

Solution (written by cry)
Code (C++) (kwant)

2148F - Gravity Falls

Solution (written by cry)
Code 1 (C++) (cry)
Code 2 (Python) (Vladosiya)

2148G - Farmer John's Last Wish

Solution (written by avighankc)
Solution 2 (written by Jteh)
Code (C++) (Jteh)
  • Vote: I like it
  • +74
  • Vote: I do not like it

| Write comment?
»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Fast as light

»
12 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

Fast editorial! (please like im farming)

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I am a noob, I can't understand the rules does it mean that if I haven't participated in 5 previous rounds and solved a Question I won't get rated?

»
12 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

Problem E was a beautiful problem for a div 4.

Enjoyed the round.

»
12 months ago, hide # |
 
Vote: I like it -18 Vote: I do not like it

Problem-F: How it works step by step:

Read all the lists Keep track of their lengths. Start building the final list Look at all lists that still have numbers left. From the remaining numbers in each list, pick the list whose numbers are smallest when compared from the current position. Add numbers from that list Take all remaining numbers from that list and add them to the final list. Mark that list as used so we don’t pick it again. Repeat Keep repeating until all lists are used or we have added all numbers. Print the final list This is your combined list that is as small as possible in order. Key Idea: Always pick the next list whose remaining numbers make the smallest sequence possible. Once picked, take all remaining numbers from that list. This is called a greedy approach.

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

B was easy but the problem statement was tricky to understand.

  • »
    »
    12 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    i took 45 mins for B and another 45 mins for A,C,D,E.

    • »
      »
      »
      12 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      bro just look at the image in the statement that clears everything tho it took me 12 minutes to guess

      • »
        »
        »
        »
        12 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        It's really based on luck, bro. No offense I'm just saying.

        B messed me up too(I was watching the contest) but through A — E I solved at first glance.

        Same with F but I am not sure if it works(I mean i'm sure in my head but edge cases can always appear outta nowhere ya know)

»
12 months ago, hide # |
Rev. 3  
Vote: I like it +1 Vote: I do not like it

Got problem F accepted 10 minutes after the round got completed :( Still ENJOYED the contest :D

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Fast editorial thanks! E and F was awwsome

»
12 months ago, hide # |
Rev. 5  
Vote: I like it +1 Vote: I do not like it

F possible to solve with $$$O(\sum k)$$$ time

Oops, my solution wrong. But anyway this time complexity exist — read: https://codeforces.me/blog/entry/146112?#comment-1308911

»
12 months ago, hide # |
Rev. 4  
Vote: I like it -20 Vote: I do not like it

Fast editorial fr, fr.

orz to The coder, The myth, The legend, cry

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Is anyone else having the same messed up rankings as me? AK, penalty time 217, 10th in common standings (but not shown), 69th in friend standings

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +4 Vote: I do not like it

Wrong interpretation for a solution for F, nvm. :)

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Interesting idea! So clear, and also have "one pass through the data" vibe!OwO

  • »
    »
    12 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    what if we don't check the unique minimum case? will it time fail directly?

    and even if we do check it suppose there are 2 identical arrays(minimums) so leading to no unique element case. In this scenario isn't this method leading to a (mx*n) time algorithm? is that fine? (could you please explain how it runs fine?)

    my similar solution also passed that didn't check for unique case but kept track of when a minimum array ends(since it gives more options) however i am not able to analyse the worst case complexity. During the contest i assumed it will exceed TL. 338529461

    • »
      »
      »
      12 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      This is true. Nice catch.

      The current implementation runs in $$$\mathcal{O}(n \cdot mx)$$$, but it can be improved to $$$\mathcal{O}\left(\sum k\right)$$$ by storing, for each column, only the indices of arrays that actually have an element there. This way every element is processed at most once.

      In the given code, however, $$$mx$$$ and $$$n$$$ are dependent — the worst case occurs when all arrays are identical (same size and same values). Even then, the bound is still manageable, since $$$\sum k = n^2$$$ which means that $$$n = \sqrt(\sum k))$$$ yeilds to $$$\mathcal{O}(\sum k * \sqrt(\sum k))$$$

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

By the way there's a typo in the editorial for E:

"There is can only be one way that all multisets can contain must the exact same elements"

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

how does the order not matter in E? like the order must matter right?

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi guys!

This was my first contest using c++, can somebody please explain to me why did my code output 33? https://codeforces.me/contest/2148/submission/338397788

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Very cool bug, lol. You are getting 33 because scanf is expecting the memory address where to store the int read from the stdin. Since you have not provided any, it just ends up using the register value that was in %rdi earlier. This is undefined behavior, but here it happens to be the address of the variable n. So, all new values are read to n actually. And, since the last value in test case 2 was 32, you have n + m = 32 + 1 = 33. For test case 1, the last value and the original n value both happen to be 1 so it somehow works.

    For quick fix you can replace your scanf lines with scanf("%d", &x);. We are reading new values to x because we have no use for x and these new values. Ideally, you would declare a new int and then read in that.

    Read https://usaco.guide/general/input-output for C++ specific I/O guide.

»
12 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Can anyone hack my solution, don't know why it passed even though it is O(k^2logn) for F:

https://codeforces.me/contest/2148/submission/338467466

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +4 Vote: I do not like it

For problem G, it suffices to only check prime divisors, which lowers the time complexity a lot if you precompute those.

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    can you explain why prime divisors only is enough

    • »
      »
      »
      12 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      If a non-prime number divides some amount of values, then all of its prime divisors will divide at least as many values as it, so prime numbers are always optimal.

      • »
        »
        »
        »
        12 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        But how does this explain drops? $$$[4,2,1]$$$ for example: yeah, it is covered by just $$$2$$$ but the drop is not represented because $$$4$$$ never exists?

        • »
          »
          »
          »
          »
          12 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          Whenever the gcd drops, lets say it is divided by some number x, all numbers before the drop are now going to be divisible by x when divided by the new gcd, hence it is sufficient to update the counts of all prime divisors of x to be the size of the current considered prefix.

          See submission for details: 338721463

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    powers of primes

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Correct me if i'm wrong, but i don't think it works, here's a counter tc for prime divisors

    1
    2
    4 2
    
    • »
      »
      »
      12 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      it works. the output will be 0 1. We're not just checking the prime factors but rather the powers of each prime factor. Check my submission

      • »
        »
        »
        »
        12 months ago, hide # ^ |
        Rev. 2  
        Vote: I like it +3 Vote: I do not like it

        Thanks, i understood how it works from your code, here's a cleaner code that can run faster without maps (small change on editorial's code)

  • »
    »
    12 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    You also would need to check between two powers of a prime divisor, in case: 125 25 5 5 5

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Super fast editorial... Thanks for the contest.loved problem F and happy to solve in the contest

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I know this is not the place to ask such question, but still: is there anyway I can bypass "crawling" block, so that I will be able to continue hacking submissions?

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Apparently there's none; the only way to get unblocked is to just wait. From my experience, you sometimes get unblocked after a few hours, and sometimes blocking persists for more than 10 hours. So yeah, I don't know...

    I agree that it's silly to block someone who eagerly tries to hack solutions in the open hacking phase. Also the warning text is a bit too scary and unhelpful.

»
12 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it
»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For some random array is the No. of times does the prefix gcd change almost O(1) ?

i.e # of times [g != __gcd(g, a[i])] this condition occurs found to be O(1) for large random array?

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

too short F with python slices. i think it can be hacked

338490282

»
12 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Problem G can be done very easily by just going through each number. Say, gcd till now is g and after adding current number, gcd becomes g', then there are only two cases —

  1. g' == g: In this case, if current element is not part of longest subarray, then answer does not change. If current element is part of longest subarray, i.e. it affects our answer, then g' has to be one of the divisors of current element. Hence ans = max(ans, F[d]), where 'd' is any divisor of current number > g as g'> g and F[] is cumulative frequency of divisors till now.

  2. g'< g: this is best case, we already found best subarray till the last element & current element decreases the gcd which satisfies the problem condition, that's what the problem is asking for!

[submission:https://codeforces.me/contest/2148/submission/338531912]

»
12 months ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

Problem G can be solved in $$$O(n\log n+\sum_{i=1}^{n}\tau(a_i)). $$$ My submission: 338537949

For each element, we must increment the count of each of its divisors by 1. Then, the problem boils down to finding the maximum count of a divisor such that it is not equal to the number of elements processed so far. Naively, this can be done with a multiset, but the heavy log factor TLEs. So, we can instead build a "buffer" which holds all elements with count equal to the number of elements processed so far (essentially, all elements in the buffer are divisors of the gcd so far). If the current element we process is divisible by some number x in the buffer, x remains. If not, we remove it (it does not divide the gcd anymore). The answer for the prefix is then the maximum of the count of every divisor not in the buffer.

»
12 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

I believe my solution for Problem F is $$$\mathcal{O}(\sum k_i)$$$. It's just about maintaining all the possible candidates for the current element. Here is The link.

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You mean F?

    • »
      »
      »
      12 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Oh sorry, changed now.

      • »
        »
        »
        »
        12 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        Umm... isn't there some logarithmic cost for maintaining the valid array pool (you named them candidate)? I didn't understand fully since it's in python, but I believe i implemented a similar solve where for each column, I go through all the "valid" rows that have the minimum element for current index. Makes it $$$O(\sum k \cdot \log k)$$$ for me. Again, not 100% sure for your code, that's why asking.

        • »
          »
          »
          »
          »
          12 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          There are nothing in my code that would cause a $$$\log$$$. You just need to iterate over the whole structure and it doesn't need to be sorted.

          • »
            »
            »
            »
            »
            »
            12 months ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it

            I believe I understand now what you did. I'm essentially doing the same thing, but didn't realize that I could get rid of the $$$\log$$$ factor. Thanks.

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone tell me why is my code failing for F..

https://codeforces.me/contest/2148/submission/338427667

any failing testcases will be appreciated

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can my G be hacked? ( https://codeforces.me/contest/2148/submission/338510007 )?

Uses segtrees to find most occurring prime factor, and gets TLE with segtrees of size 2e5. Realised 1e5 works a bit later and got AC with that, but still seems slightly iffy imo.

»
12 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

E is much easier than F,i think F is a good problem! btw I solve A-E so slowly because of the bad network I took half of time to load the problem and used m1.cf.com to submit lol

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

These questions are very challenging(For Div4)! My friend's F code has been hacked, haha.

»
12 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Man, what the heck! My Python code for E got a TLE on test case 10 during post-contest system testing. It has the same exact logic and time complexity as the editorial's C++ solution, which feels so unfair! :((

»
12 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Does anyone can tell me why my code is wrong on problem F? I can't figure it out.

Also, why is the indentation of the code so strange when editing, and how can I adjust the tab space?

#include <bits/stdc++.h>

#define int long long

using namespace std;

using vi = vector<int>;

int t, n;

signed main() {
	cin >> t;

	while (t -- ) {
		cin >> n;
		vector<vi> arrs(n);

		int mx = 0;
		for (int i = 0; i < n; i ++ ) {
			int k;
			cin >> k;
			mx = max(mx, k);
			vi a(k);
			for (int j = 0; j < k; j ++ ) cin >> a[j];
			arrs[i] = a;
		}
		
		sort(arrs.begin(), arrs.end(), [](const vi& a, const vi& b) {
			int mn = min(a.size(), b.size());
			for (int j = 0; j < mn; j ++ )
				if (a[j] != b[j])
					return a[j] > b[j];

			return a.size() < b.size();
		});
		
		vi res(mx);
		int pos = -1;
		for (int i = n - 1; i >= 0; --i) {
			const vi& a = arrs[i];
			int ed = a.size() - 1;
			if (pos >= ed) continue;
			for (int j = pos + 1; j <= ed; j ++ ) res[j] = a[j];
			pos = max(pos, ed);
			if (pos >= mx - 1) break;
		}

		for (auto x : res) cout << x << " " ;
		puts("");
	}

	return 0;
}
»
12 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

the test case #20 of problem F n=2 but it has only 1 following line? Am I missing something?

»
12 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

how the f*ck did i fst my code is literally fkn logically equivilant to the edi like wtf ts pmo sm like ok fine i have fst i dont care, but i literally do not see a single thing wrong with my code and i am not debugging 300 lines of shit to figure ts out. i literally asked like 4 masters to check my code and i still got fst great fucking vegatables ok go downvote me now

k srry for crashout ig

anyways uh can someone tell me whats wrong lol (i seriously got no idea)

»
12 months ago, hide # |
 
Vote: I like it -11 Vote: I do not like it

There is a typo in Problem F:- - Then, ki space-separated integers ai1,ai2,…,aiki follow (1≤aij≤2⋅105). it should be aik instead of aij in the bracket

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can Somebody explain the editorial of F(Gravity Falls),in simple language, I am having difficulty understanding that

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Basically, you can stack the arrays in any order and the arrays must be left-aligned (A longer array on top of a shorter one is also ok). Then, elements in each array will fall down until it touches another element (In other words, no elements are "floating"). Finally, get the elements at the bottom of the stack from left to right and form an array. Let the array be a, your task is to find the lexicographically smallest a.

»
12 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

I solved F by O(sum(k) + n)

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hello, my solution 338490913 was flagged for coincidence with another user’s code. I do not know the other participant at all and did not share or receive any solutions anywhere (I compile my solutions offline). I solved the problem independently. The similar part of the code is generic, which I use snippets for, and the core logic seems to be analogous. Please review this case manually. Thanks!

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

omaralimm Sorry for tagging you, i saw one of your comment say that you like the Problem G, that is why i am tagging to get some quick reply, so here is my confusion, do you have any idea??

I am quite confused in solution 2 of problem G, why the time complexity goes to NlogN + N(cube root (N))?? Is there any formal proof?? It is quite of confusing to see NlogN term and even the cube root term!

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I can prove it.

    Firstly, let's get all the divisors of numbers up to $$$N$$$ using sieve like method before we start solving the testcases.

    That will be $$$O(NlogN)$$$, which comes from this equation — $$$n/1 + n/2 + ... n/n = n*log(n)$$$.

    And the number of divisors of a number $$$n$$$ is $$$cbrt(n)$$$.

    So, we do precomputation with a complexity of $$$O(NlogN)$$$ and solving the testcases with a complexity of $$$O(NcbrtN)$$$.

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem G can be solved with complexity $$$O(n\log A)$$$, where $$$A$$$ is the maximum element.

Let's improve the Solution 2 in the tutorial:

Suppose $$$g_k=gcd(a_1,\cdots,a_k)$$$ and $$$g_{k+1}=gcd(a_1,\cdots,a_{k+1})$$$. If $$$g_k \gt g_{k+1}$$$, then there will be a prime $$$p$$$ satisfying $$$g_{k+1}p\mid g_k$$$.

Let $$$m$$$ be the maximum integer that satisfies $$$p^m\mid g_{k+1}$$$ but $$$p^{m+1}\not\mid g_{k+1}$$$, then we have $$$p^{m+1}\mid g_k$$$. Thus, we only need to try divisors in the form of powers of prime numbers.

Since the number of divisors in this form for a number $$$x$$$ is at most $$$\log x$$$, we get the complexity at the beginning.

My submission: 339058812

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hello, I want to know that why this code for problem G (using segment tree) https://codeforces.me/contest/2148/submission/339214294

is NOT GIVING TLE

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem G is very interesting

»
12 months ago, hide # |
Rev. 5  
Vote: I like it 0 Vote: I do not like it

Is there a code implementation for solution 1 to problem G?

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I am not getting, for, 2148E — Split, I thought i need to all possible subarray of a and need to check if this is awesome or not. But the solution code directly returning 0 if the whole subarray is not awesome. My question is why not considering other subarrays? Also I am not getting this line: "there is some way to place items outside the subarray such that all multisets contain must the exact same elements (ignoring ordering)." what do you mean by "placing items outside"? help please!!! [user:cry][user:kwant]

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For Problem 2148E — Split: Reason behind this line in the editorial: return void(cout << 0 << endl); For a subarray [l, r] to be awesome: ∀v, in[v] x k <= cnt[v] But if some cnt[v] is not divisible by k, then we can never achieve: cnt[v] = x x k

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

the normal solution for lasers question?

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Isn't the expected solution for second testcase for 2148D - Destruction of the Dandelion Fields wrong? Shouldn't it be 12 instead?

cry can you respond to it please.

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

how is E nlogn in editorial solution seems n^2

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

to be honest, for E author's code is too hard for beginners... this is my solution very easy to understand #383158117 or https://codeforces.me/contest/2148/submission/383158117