Блог пользователя Um_nik

Автор Um_nik, история, 8 лет назад, По-русски

I want to apologize once more for queue problems. It has also aggravated some tight ML/TL issues which probably would be not so big otherwise. I hope you enjoyed the problems nevertheless.

Tutorial is loading...

38799778

Tutorial is loading...

38799800 — logs
38799811 — case analisys

Tutorial is loading...

38799824

Tutorial is loading...

38799831

Tutorial is loading...

38799840

I hope that Petr is not mad at me for my joke.

Tutorial is loading...

38799850
38799853 — completely different solution with complexity O(6n / 2)

Tutorial is loading...

38799873

Tutorial is loading...

38799881

I hope that PrinceOfPersia is not mad at me for my joke.

Tutorial is loading...

38799887

Разбор задач Codeforces Round 485 (Div. 1)
Разбор задач Codeforces Round 485 (Div. 2)
  • Проголосовать: нравится
  • +161
  • Проголосовать: не нравится

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
 It is clear that we will run DFS exactly once for each connected component of original graph.

Can u explain this?

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +14 Проголосовать: не нравится

    Let's look at strongly connected components of new graph. Each connected component of old graph lies inside one strongly connected component of new graph, and you can't go from one such strongly connected component to another. So we will run DFS from each that strongly connected component once.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -27 Проголосовать: не нравится

Hihi Quang Dien brought me here.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

In Div1 B, isn´t it easier just to transform one permutation into the other and count the number of steps it takes?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

38770498

I can't understand this solution. Can someone explain this? Thanks.

  • »
    »
    8 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +4 Проголосовать: не нравится

    Because it's a wrong solution, this is counter-test. True answer is "Um_nik", but this solution returns "Petr".

    The authors of the problem did not assume a probabilistic solution, so there are no tests against them.

    • »
      »
      »
      8 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      You are right. But this solution can get Accepted. Emmm... Your test case is randomly generated data ? Maybe this solution based in some randomly theory?

      • »
        »
        »
        »
        8 лет назад, скрыть # ^ |
         
        Проголосовать: нравится +24 Проголосовать: не нравится

        Well, yes, this solution uses that tests are randomly generated and it is correct for big n. However for n = 1000 + eps it has not very good probability of being correct. But I had very few (4 to be precise) tests with small n and it happened to be correct on all 4.

      • »
        »
        »
        »
        8 лет назад, скрыть # ^ |
        ← Rev. 3  
        Проголосовать: нравится -6 Проголосовать: не нравится

        This test is specifically directed against a probabilistic solution.

        For example and fun, I can generated tests when this solution will be Accepted:

        srand(123456);
        cout << (rand() % 1 ? "Petr" : "Um_nik");
        

        But this will not be a complete set of tests for this problem.

        You can use this method of solution if you do not need to solve the problem completely (on some olympiads give a few points per each test).

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

D Getting TLE in D could anyone Help?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

 Look here: 38736740 And what I should do with that?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится -9 Проголосовать: не нравится

can someone give python code for binary fft

»
8 лет назад, скрыть # |
← Rev. 9  
Проголосовать: нравится 0 Проголосовать: не нравится

For Div2B, during the contest I came up with this approach:

For any two positive integer numbers a and b:

ab = ba if (a = b), else

ab < ba if (a = 1), else

ab > ba if (a < b).

But got WA on pretest 5.

Today, I checked for the test I failed in and saw it was (2 4), then treated this test, but got WA on this test (2 3), then treated this and got AC.

Is really this last approach is correct for all a and b, or it just passed the tests?

BTW, it was easy to come up with this last approach because (2 4) and (2 3) are easy to think about.

38781062

.............

UPD: Sorry, I have just seen that what I wrote above had been discussed in the editorial. I apologize for my fault.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I did exactly what the tutorial says in div2C, and during the contest as well, but it gets TLE on test 11. Tried searching for similar solutions in python 2 but no one AC, can someone tell me what I got wrong?

38782761

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Why are some problem names written in russian instead of english in english version of editorial ??

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +30 Проголосовать: не нравится

I would like to just note that problems B from Div2 and D from Div1 have a lot in common. Solution to Div2B basically tells you how optimal sum looks like in Div1D ;). I don't know if it is intentional, but if not then it is funny coincidence.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -23 Проголосовать: не нравится

;

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Why did Div1B set a lower bound on n?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This is my solution for Div2 E 38777827

I am counting the number of swaps needed to obtain the permutation in O(nlogn)

how to calculate the number of cycles in permutation in O(n).

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

May someone please tell, why did my short solution got TLE to the problem Div1-B? Submission link. Is it because of distance function?

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain how i get AC by usingthis solution on problem Div2-D

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For div1D, 3^i always looks like 100000....000 (with i 0s) in base 3. So, why don't we just convert n (or n/2, or n/4) to base 3 (In O(LlogL)) and compare it with 3^i (in base 3)?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

38793607 Getting TLE in problem D from Div. 2. Not sure what I'm doing wrong here, could anyone help?

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    In the bfs Don't push a node unless their cost will be updated and update the cost

    • »
      »
      »
      8 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks! I made this change and got AC. But I don't understand one thing:

      Earlier I was pushing the nodes irrespective of whether the cost would be updated; I was performing this check at the time of popping and stopping the BFS from a popped node if the cost is not to be updated. So basically in the BFS in my earlier submission, all the nodes were being operated on 2 times instead of 1 time (first time when it's pushed onto queue and second time when it's popped and I check for cost update). So this would be an optimization, right? The time complexity is the same? Please correct me if I'm wrong. Thanks again!

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How to solve Div2 E with mergesort ?

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Merge sort here is used to calculate the number of inversions of a permutation to find the permutation's parity.

    Suppose your permutation is 4 3 2 1.

    Divide it into two parts. 4 3 and 2 1.

    Now, what are the number of inversions created due to 2? (number of pairs where a[i] > a[j] and i < j and a[j] = 2)

    It's the number of elements larger than 2 in 4 3.

    You can easily find that in the merge process of merge sort. When you are inserting 2 during a merge, the number elements left in the other half of the array is the number of elements larger than 2, isn't it?

    So, for 1, when you have divided original array into 2 parts, you are adding 2 to number of inversions.

    When you further divide 2 1 into 2 halves i.e. 2 and 1, you are adding another 1 to number of inversions.

    This is just a basic gist, you can find details by googling about finding number of inversions using merge sort.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I had used the logarithmic approach as discussed in the editorial but I am getting the wrong answer for x=y (where x and y are very big) but it is working fine on my local system. I am not able to figure out the exact reason why it is behaving differently on CF judge and my local system. Can someone please help me out to understand it?

Thanks in advance.

my solution

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Links to authors submissions are added.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

For Div.1 E,can somebody elaborate the fenwick tree on euler tour tree or introduce some tutorial on it? How does it calculate the sum of on path to root in O(logn) time? Thanks a lot!

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +37 Проголосовать: не нравится

    keep l[x] = starting time of x in euler tour, r[x] = ending time

    If you want to set value of node x = val, then with fenwick you modify l[x] to val, and r[x] to -val.

    Now say you have 2 nodes x, y and x is ancestor of y

    The range of query will be l[x], l[y]. Every node on path from x to y will appear Exactly once on this path. And nodes that aren't on path appear twice, so they cancel out by the way we set values in fenwik tree.

    So basically sum from root to x is query on (1, l[x])

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +19 Проголосовать: не нравится

    Also we can do that without fenwick tree at all. Just do dfs and keep sums to the root in the global array. In this case for each query we need to calculate sum sumx + sumy - sumlca(x, y) - sumparent(lca(x, y)). That will be solution.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone provide links to more problems like Div1-B,Div2-E.. Thanks :)

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can any one help me with solving Three displays problem using dynamic programming(DP)?

The states for the DP I visualised are current display label(1..N) and number of displays(1..3). For each display label I minimise and choose the best cost of one display, the best cost of two displays and the best cost of three displays.

I'm getting Wrong Answer. The case I'm missing is where I don't get the best cost of three display at any display labels. So now I'm kind of feeling stuck on how to re use the sub-problem's solution here? Can anyone help me with formulating the DP ?

My submission link contains my DP idea.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +13 Проголосовать: не нравится

please explain O(6^(n/2)) approach for AND graph. thanks

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I am getting TLE for Div2D in Java even though I used the same algo as the editorial. Someone suggested that using ArrayList in graphs in Java is inefficient. Does anyone has links to some articles that explains the alternative well?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Dear Um_nik,

Please explain your second solution to F with complexity O(6^(n/2))

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Continuously getting TLE in problem Div2D with Java solution . I dont understand whats wrong.

(http://http://codeforces.me/contest/987/submission/38820851)

If anyone can help :(

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится
  1. In problem C And Graph, why do we have to consider the second class of edge (x,2)-->(x|(1<<k),2)?
  1. In case of third class of edge (~x,2)-->(x,1) , the AND of (2^n-1-x)&x is zero , so we can keep this edge, but x&(x|(1<<k)) is non zero so why keep this edge,though this edge might be useful in leading to some y such that x&y=0, how does it actually work?
  1. Also, many submissions I saw used transition from x--> x^(1<<k) if kth bit is set, where x^(1<<k) is the subset of x if I'm not wrong(I read it in SOS DP). What is the difference between going from a. x--> x^1<<k if kth bit is set and b. x--> x|1<<k if kth bit is not set, can someone please explain me the whole thing correctly im too much confused. Thanks;)
»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What is the O(s(m + n)) solution to D ?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Inspite of the controversy surrounding this contest over the delayed queue length and the tight memory limits, I actually enjoyed this contest quite a lot ! I loved the problems and 3 of them inspired me to write blog posts.

Here is the repository of all my solutions to problems of this contest, along with explanations.

All my blog posts have some more exposition. Please read them and get back to me with any doubts or corrections. :)

I have written a blog post about B here.

I have written a blog post about E here.

I have written a blog post about F here.

(All the problems were quite beautiful :) )

Note — I have not written a blog post about C, but the repository of all my solutions also contains an O(n log n) solutions to C with segment trees. It's quite easy to understand if you know how to count the number of inversions using a segment tree. It's the same idea. First we sort elements by font size, and keep track of every elements cost and position. Then, one by one, we insert the elements in ascending order of font size into a minimum tree for cost. When we insert element x, all elements with font size smaller than x have already been inserted, so to know the smallest cost, we simply query the tree from (1, position(x) — 1).

We do the same thing for the right too, except now we insert the elements, one by one, from the largest font size. This time when we insert x, we have already put all elements with a font size greater than font size(x). So to get the minimum cost, we query the tree from (position(x) + 1, n).

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain update complexity in 1E(Prince's Problem).

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +21 Проголосовать: не нравится

(in div1B)

Why the generated tests have to be generated randomly!?

I mean hacks were disabled and we're guaranteed that input is random, even though the official solution does not rely on randomness of the input!

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can I get a hint for solving Div2-D in O(s(m+n))?

  • »
    »
    8 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +1 Проголосовать: не нравится

    Hi~

    Also I don't know the optimum solution, but here is my thought. Hope it help

    When we run BFS for one specific town, we don't need to record all information of k goods. Once get s goods, we get the answer. This is to say, no need to use the sort stuff.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem F — Oppa Funcan Style Remastered, why can't we use the solution of m >= 3 for m = 2? It seems to fit inside the TL. Am I missing something?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +19 Проголосовать: не нравится

Author's solution for div1E contained a typo (which made it incorrect, of course). There were no tests against this bug in my testset, so even two other solutions didn't help to find it. Now author's solution should be fixed and some stronger tests added. I apologize for this mistake and want to thank Death_Scythe who found the bug.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

In problem Div2 E, I don't quite get the part of parity of 3n or parity of 7n + 1. Can someone help me with this?

»
8 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Can Div2 C be solved in O(nlog(n)) time. If yes, how?

»
8 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится -10 Проголосовать: не нравится

Troll solution for a troll problem Div1B
Link to AC soln

int n, p = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
    scan(x);
    if (x == i)p++;
  }
  if (p >= 3 or n <= 5) cout << "Petr";
  else cout << "Um_nik";

(Expected number of fixed points are very different in both approaches)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Can we solve div2 C in O(N log N) ????

»
4 года назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

We can use Segment trees to solve 987C in O(nlogn) time. 174709413

»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

for Div1,D,python can offer a quite straight forward solution;no need for you to write fft yourselfmy answer

»
3 месяца назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Insane necropost but I passed this 387204316 on problem B and it's funny enough to share haha.

I had 10 minutes left in virtual and couldn't be bothered to think so my first instinct was to cheese it. I guessed that the former shuffling method would leave a lot of fixed points. Conveniently, the expected number (of untouched indices, which actually underestimates fixed points) is actually meaningfully different for n = 10^6. So comparing to both expected values will differentiate in expectation.

It does not, however, provide a meaningful differentiation for n = 1000 hahaha (and definitely not n = 5). I did not think about this. This code will get the sample wrong (I hardcoded it), and there are few cases for n = 1000. On these, I simply won the 50/50.

Given that there's a real solution to the problem, it seems it used "randomized generation of the test cases" purely as a flavortext gimmick. Conveniently it helped me :)