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

Автор Karavaiev, история, 4 года назад, перевод, По-русски

1388A - Капитан Флинт и набор матросов

Идея: Denisov

Разбор
Решение (Karavaev1101)

1388B - Капитан Флинт и дальнее плаванье

Идея: Karavaiev

Разбор
Решение (Karavaev1101)

1388C - Дядя Богдан и настроение страны

Идея: Karavaiev

Разбор
Решение (Karavaev1101)

1388D - Капитан Флинт и сокровище

Идея: Denisov

Разбор
Решение (Denisov)
Решение (Karavaev1101)

1388E - Дядя Богдан и Проекции

Идея: perekopskiy

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

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

Thanks for the quick editorial!

»
4 года назад, # |
Rev. 4   Проголосовать: нравится +207 Проголосовать: не нравится

Feel free to downvote, but this is my honest opinion; Personally, I think that the grammar of problem C made it abit annoying to understand.

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

    Agreed. If not for the notes, I probably would have wasted a lot of time understanding the statement.

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

      Yup, I don't mean to complain, but statements similar to these felt somewhat distracting-> "Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood."

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

    was writing " was made it a bit annoying" on purpose? If so, it's funny. If not, it's sad XD

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

    Lengthy background but interesting problems.

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

    I agree. you know most of the participants mother language isn't english and they can't get the statement easily, when you make the statement hard to understand, it will be really hard for them and sometimes they just skip the problem and lose it points because they can't understand the statement, and that will give them a really bad point at the contest. so please take more care for the statements.

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

      Yes, I totally agree with you. As I am not a native English speaker or a native Russian speaker(I am Chinese), for some problems the statement is very difficult to understand(QAQ) and extremely complicated, although the statement itself is quite easy.

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

Lol I was in bad mood after reading the long statement of C. Missed it by one case

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

For problem D--can anyone please explain or give a test case where my solution is going wrong. The testcases in problem are too big to debug.

Here is the link to my submission.

I am bascially making a directed graph where an edge from u to v denotes that operation on u-th element must be done before v-th element of the array a. Then I am running a dfs on transpose of this graph and updating all elements(suppose v-th and w-th index element needs to be operated before u-th element, then updating a[u]=a[v]+a[w]) and adding those to answer while maintaing this order.

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

    Your solution fails on that test case:

    test
    Answer

    Your solution works that way because after doing some operations $$$a_{b_i}$$$ can become positive and you can improve other $$$a_j$$$ using that $$$a_{b_i}$$$

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

Would anybody help me with Question D?
My submission --> 88521832.
Approach: I made a 2D array of row size n to see whether there is any index that must be ahead than index i(0<=i<n). And then I just simulated for indices based on the array to determine the answer.
Variables: a and b are input array, c is array to determine which indices should come prior index i. array d is for storing the answer array. ans is calculated based on d. s is just counter to see all n indices are in d. every array is 0-indexed.

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

    I'm sorry that my English is not very well.

    There's a situation in the process of Adding $$$a_i$$$ to $$$a_{b_i}$$$ that it may change $$$a_{b_i}$$$ from negative to positive.In this situation,if $$$b_{a_{b_i}}$$$ doesn't equal to $$$-1$$$,it should be also ahead some than some index.But it seems that you didn't manage to do this.

    Note that there's a principle that no matter what $$$b_i$$$ is,we should always put the index with $$$a_i > 0$$$ ahead of the index with $$$a_i < 0$$$.

    there's a sample that your algorithm makes mistakes:

    Input

    3

    -2 -1 4

    -1 1 2

    Output

    8

    3 2 1

    But your output is:

    5

    1 3 2

    Hope my reply could help you.

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

Video solution of the whole problemset

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

Why don`t topological sort works in D?88526038

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

It's my 2nd contest.I solved a no. problem.But still no rating given.How long does it take to get ratings after a contest is finished?

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

In my humble opinion problem set was amazing, but problem B was made a lot obvious by the example input, it could have been swapped with problem A if problem A constraints were made a bit tighter and brute force was not allowed.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится -13 Проголосовать: не нравится

    Hey I think prob A was very easy...

    I mean just look at the code, it's basic brute force...

    ll n;
    cin >> n;
    if(n<=30) cout << "NO" << endl;
    else
    {
    	if((n-30)==6 || (n-30)==10 || (n-30)==14)
    	{
    		cout << "YES" << endl;
    		cout << 6 << " " << 10 << " " << 15 << " " << (n-31) << endl;
    	}
    	else
    	{
    		cout << "YES" << endl;
    		cout << 6 << " " << 10 << " " << 14 << " " << n-30 << endl;
    	}
    }
    

    ``

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

      Yeah isn't B also a relatively basic observation.

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

        Yeah sure but I missed a very important line NOTE resulting in my WA twice and then I was able to get it right...XD

        Because of that I missed problem C.

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

          hello, in problem B if I have n = 5 then the number must be 9 9 9 9 9

          • in binary : 1001 1001 1001 1001 1001

          • but after removing last 5 digits then it become

          • 1001 1001 1001 100- ----

          • which makes it a 4 digit number, so making that 5 digit number I need to make it all 0s :

          • 1001 1001 1001 1000 0000

          • but this doesn't the case.

          • Can anyone help me with that ?

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

            Why do you think 0's binary is considered 0000? Why not 0000000000000 or anything else? Answer this and you'll get your answer as well.

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

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

what kind of error is it "wrong output format Unexpected end of file — int32 expected" ? i did not see before . 88520911

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

    The grader was expecting more numbers(int32) in the output, but your solution did not print them. Check if you are printing the required output in the specified format. You can check my submission for more clarity.

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

Great round, guys!

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

In problem C How can (av+hv)/2 count the number of people in a good mode?

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

    suppose X are the people that were happy at city A and Y were the people that were not happy at the same city. Then X+Y = total citizens that visited the city(total citizens in subtree of that city). X-Y = (happy-sad) given for city. Add both equations and get the value of X which is same as described in the editorial.Hope you understand.

    Edit- by happy i mean good mood as in problem statement.

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

    Honestly I didn't solve it like that, but I'll explain.
    Think that moods are G good and Y bad, in a city with P people. Do you agree that P == G + |Y|? Do you agree that H = G — |Y| ?
    Then P + H = G + |Y| + G — |Y| = 2G, and we are done.

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

    let gv be the number of people in a good mood that passes through node v, and bv the number of people in a bad mood. With that we can write the following system of equations: gv-bv = hv and gv+bv = av. If we sum the both equations we get 2gv = av + hv and thus gv = (ah+hv)/2

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

Me to the second question :: WHy you do this to me????? :(

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

https://codeforces.net/contest/1388/submission/88510199 whats wrong with this submission for C?

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

I don't understand, in problem E — isn't there an easier way to calculate the projections? I don't know what the Convext Hull Trick is, but I mean, you have the angle and you have the height, that should be enough. Is the problem the fact that there could be error in the calculation and you will get a wrong result?

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

    It's a problem that you can't project every segment for every angle because it takes too much time, so we need to find the rightmost and leftmost projections quickly

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

    If you calculate the projection of $$$O(n)$$$ vectors for each possible angle (there are $$$O(n^2)$$$ of them) the complexity becomes $$$O(n^3)$$$.

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

      Ahhhh never mind I completely missed something. I thought the "no go" zones make a full range [theta1,theta2], and then you are left with only 2 angles to check. I understand now that there could be many options.

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

      By merging overlapping angle segments, it is possible to pass the time limit via this brute force method.

      88545255

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

Here I am generating a ton of primes and trying to do sliding window 3 sum with prefix sum to solve 2A and still couldn't. :(

RIP rating. I'm sad.

  • »
    »
    4 года назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

    I also had a sad story but it turned out ok.
    At first I read the problem as "multiple" instead of "sum". I solved it though xD
    You just need the prime factorization and you need to pair up 3 pairs of different primes. (8 * 3 * 5 * 7 ==>> 2*3, 2*3, 2*5). (That made me 15 minutes behind :| )

    Edit: I need to start making it a priority to look at the god damn sample cases to verify I read the problem correctly.

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

My video solutions to problems A, B, C, D.

Enjoy watching.

https://youtu.be/0ExktAwScLc

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

Didn't got AC at C be cause I was using python. Rewrote it in C++ and got AC. Don't use python in recursion problems...

  • »
    »
    4 года назад, # ^ |
    Rev. 3   Проголосовать: нравится +10 Проголосовать: не нравится

    Look at my solution, I use this "boostrap" decorator that someone posted a while back (I use it all the time and it always works), that replaces the recursion.

    It looks like this:

    The bootstrap decorator
    My DFS

    Just notice —

    • To make the recursive call, you have to use yield.

    • When you call recursively, you can't do 'if yield dfs(..)' or stuff like that, you have to first save the value with 'x = yield dfs(...)'.

    • When you want to return something, you have to use yield (i.e., 'yield 5').

    • At the end of the function, if you return nothing, you have to use 'yield' with nothing after. THIS IS THE MOST IMPORTANT POINT. if you forget this you will spend eternity debugging.

    Thank me later.

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

      Can we implement it in c++

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

        Well, you don't need it in c++... 10**5 recursion works fine for you.

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

          I just wanna try, how to replace the *args thing what is that??

          • »
            »
            »
            »
            »
            »
            4 года назад, # ^ |
              Проголосовать: нравится -11 Проголосовать: не нравится

            Lol you will literally not be able to do that, You have to implement a generator and I have no idea how you could do that in cpp.

            There's a reason python exists. One of those reasons is that you can do stuff like this in less than 1000 lines of code.

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

Can someone help me find the missing case here in my submission of problem C? I am unable to find the corner case here:My Submission

Thanks in advance

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

I had the same idea as the tutorial for problem no C, don't know where I messed up. :(

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

My friend who got into coding tried CF for first time and he received RE on both of his solutions, while locally it worked. As I'm not familiar with python, can somebody explain what is wrong? Thanks

submissions: https://codeforces.net/contest/1388/submission/88484222, https://codeforces.net/contest/1388/submission/88506487

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

    'input' doesn't work like that. First of all, input is a function, so it should be 'input()'.
    Second of all, it only reads one line.
    To get a line with an int you do int(input()).
    To get a list you do [int(x) for x in input().strip().split()] or list(map(int,input().strip().split())) or some other way.

»
4 года назад, # |
Rev. 3   Проголосовать: нравится +3 Проголосовать: не нравится

Can anybody please tell me where am I going wrong. Thanks in advance here B, G is no.of people in a bad and good mood respectively

Code

Update : I am dumb

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

for problem c, i checked if(abs(h[i])>count[i] || count[i]%2 != abs(h[i])%2 ) then its not possible where count[i] = number of people visiting that node and h[i] = happiness value of i node.the second condition ensures that for a node with count = 5,the possible hvalues are -5,-3,-1,1,3,5 only.can anyone tell me why is it wrong? https://codeforces.net/contest/1388/submission/88539137

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

    You have only checked the upper limit i.e. abs(h[i])>count[i]. There will be a lower limit as well. suppose there are 2 cities i and j with i being the parent of j. now, let count be the number of people visiting j. Then let x be the the number of people with good mood and y be people with bad mood. Clearly , x+y=count and x-y = h[i].From here you can get x and y. Now , for the city i, the range would be [x-y,x+y].Because, There must be atleast x people with good mood. If you want the sol , refer to — 88508137

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

Can someone please explain why in Problem D we reverse the second array ( the array with -ve elements ).

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

For problem E, the leftmost/rightmost segments can be handled in a similar manner as the "feasible angles" approach, by sequentially "trimming" the ranges where each of the $$$n$$$ segments is the leftmost/rightmost one.

Code (sorry for too few newlines)

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

Can anyone please tell me? In solution of C, how g[v]=(a[v]+h[v])/2 ?

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

    g[v](which is total good mood persons passes through vertex v)...................... calculate it through this given happiness index and no. of people passes. now the happiness index will be (acc. to question) (g[v]-(a[v]-g[v])),where (a[v]-g[v]) is sad people passes. hope u got it

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

    let $$$b_v$$$ be the number of people who visited city $$$v$$$ in a bad mood then $$$h_v = g_v - b_v,\,a_v = g_v + b_v$$$, and $$$\frac{a_v + h_v}{2} = \frac{g_v + b_v + g_v - b_v}{2} = g_v$$$

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

I tried C with a little different approach. I calculated the number of people who travelled through a particular node with dfs and then applied bfs to check if the sum of unhappy people in children should be more than or equal to the number of unhappy people who travelled through the ancestor. I cannot figure out my error. A little help will go a long way. Code

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

    It is not not necessary that sum of unhappy people in children should be more than or equal to the number of unhappy people who travelled through the ancestor because some of the unhappy people might live in the ancestor city.

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

Thanks for the tutorial. I really need this to improve myself.

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

ok, during contest my solution for B was correct but now its showing TLE but if i submit it again it got accepted what is happening, here are both submissions TLE submission AC submission

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

    I am not sure but it has something to do with dynamic strings you are using, in first submission you code might have encounter memory issues. like you are making a string of size (maxN). when ever you push a char into it. string needs a contiguous free space to instert if it is not there then string will move the whole string to a place where memory is available. and this operation takes O(n) time to execute.

    that means complexity of your code could have crossed $$$N^2$$$ in this way on multiple occassions.

    look at this submission : https://codeforces.net/contest/1388/submission/88556412

    code

    in this code i reserved a contiguous memory space of N for my string at very first. and look it only took 30ms to execute while your code did same in ~1000ms.

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

      yeah but time limit was 2 sec. i get the point that it is O(N*N) because i am coping the string every time like s=s+'9' every time s is copied here in right side of operator. and also after the contest same code at test case 7 run in around 600ms and the other TLE solution(same code during contest) run for >2000ms thats what i am asking i mean why is this happening just to be safe from next time. and btw thanks for reply.

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

        so s = s + '9'; this statement can run in either $$$O(1)$$$ time or $$$O(n)$$$ time. if memory is available it will run $$$O(1)$$$ time, but if mmemory not available in contiguous manner in memory this statement will take $$$O(n)$$$ as i said it will copy the whole string to a place in memory where is avalable.

        so it is not truly $$$O(n^2)$$$.

        it depends on the collsions (when contiguous memory is not available and compiler will perform $$$O(n)$$$ operation.) and your code at the time of contest got more collisions than when you ran it after contest.

        it might have something to do with may be that memory is under more demand during a contest in comparison to after contest.

        if your code's string get assigned a memory place from which a contiguous space which required is available in that your code will run in 30ms.

        If you want to be safe from next time then becareful while using dynamic memory allocations. and reserver memory before hand as much required in one statement. like most common mistakes like these are using unordered_map. link. https://www.geeksforgeeks.org/unordered_map-reserve-in-c-stl/

        i have a friend who uses char arrays instead of string to be extra safe.

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

Nice problems.. Short solutions... quick editorial.. :)

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

This link is my submission to question C of yesterday's contest. Could someone please help by pointing out where am I going wrong?

Thanks in advance

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

In Problem C, you guys gave the necessary conditions with simple proofs, but what is the proof that these conditions are sufficient??

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

For problem C i checked for each node whether the number of happy people in that city is less than or equal to its parent node.Can someone tell why this approach is giving wrong answer?

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

    You should check sum of happy people in all children of a parent is less than or equal to happy people of parent as happy people cannot increase.

    But in your approach happy people can increase.

    Take example A is parent and B,C,D are children. Happy at A=6, Happy at B=5, Happy at C=5, Happy at D=5. In this case your all conditions are satisfied and answer should be yes according to you but answer is no.

    In this situation you see that happy people at parent is 6. But moving ahead happy people increases it becomes 5*3=15 at children. It means bad mood people are decreasing that is not the answer to this question.

    That is why your answer is giving wrong answer.

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

      Got it.Thank you for helping.

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

        Yeah its fine bro. I also did similar mistake hence i thought to reply as its a learning platform and we can learn only by helping others and taking help from others.

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

https://codeforces.net/contest/1388/submission/88555647

can anyone help me with this solution for problem D?

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

C question was awesome, it helped me revise all concepts of dfs.

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

For question D, the dfs solution given in the editorial doesn't always work, since the starting point is random with straightforward dfs.

It seems to fail for —

3
1 -5 6
-1 3 2

expected — 9 given — 8

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

    no it is not.

    we are only starting from the leaves. and that is the logic.

    Explain how is your expected result is 9?

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

      The editorial solution (dfs) doesn't seem to enforce the condition that we should start from leaves. Like in the example that I have given, I ran it and the output was 8. But if we enforce the condition, it would have been 9.

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

        i don't see how you are getting 9..?

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

        because your test case is too small here are possible answers to you test case

        1 2 3 = -3

        1 3 2 = 8

        2 1 3 = -3

        2 3 1 = -3

        3 1 2 = 8

        3 2 1 = 8

        there is no 9.

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

    It seems that test

    test

    is incorrect because we have cycle $$$2, 3,2$$$ and the addition constraint says that the graph doesn't contain any cycle.

    In my solutition starting point can be any vertex, but I create graph with different from the editorial edges: from $$$b_i$$$ to $$$i$$$. The solution of Karavaiev is written in the same way as editorial.

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

I have joke for question C but I am not in good mood to tell. :)

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

In problem B, the number we are expected to find should be as small as possible, and r, as big as possible. So if the input is say 11, should the answer not be 99999999800 instead of 99999999888?

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

    by adding 0's at last you are not agreeing with the maximum possible value for r , You can do better with adding 8 instead !Try it out on paper

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

    Remember that each digit will be converted to binary first and only then we remove N binary digits from the resulting string.

    So the idea is to maximize the number of binary digits, that's why we only use number 8 and 9 in our answer, because they are the only decimal digits that, when converted to binary, have 4 digits (1000 and 1001, respectively).

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

Can someone help me out with the Problem C, here is my solution, I used the similar idea as described in the tutorial, but used a different approach for implementaion.

My approach

I dont know where I messed up. Can someone please help. My submission

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

So many contests in the contest list but no div3 :(

I am eagerly waiting for a div3 after so many difficult contests. Just to gain some confidence :)

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

can someone suggest a test case where my solution fails 88565587

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

In problem C, if I am not incorrect, $$$to_1, to_2, …, to_k$$$ for city $$$v$$$ are the cities belonging to the subtree of the node which city $$$v$$$ belongs to. So, the third condition states that among such cities, sum of number of happy people entering each city must be less than or equal to the number of happy people entering the city $$$v$$$. Can someone please explain this condition? What I think is that for the happy people getting off at some city $$$to_i$$$, they will be counted every time they visit a city which is on their way home. So how is this repetition avoided?

  • »
    »
    4 года назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

    Well, I have another approach if anyone is interested.

    we will have two arrays one of positive count(person who ended up happy in subtree) and one of a negative_count(person who ended up sad in subtree)

    apply dfs

    condition
    adjust

    When You are at a node then sum the number of positive_count and negative_count in the subtree. check the condition (it is possible to get the required happiness at this node) and if it is possible then adjust the value and update the positive and negative count at that node.

    code 88577713

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

    Exactly, it didn't make sense to me,too

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

      It's only for immediate children. They are not considering all the children in the subtree.

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

    Nobody cares what a pupil thinks, but i'll drop it anyway. I say you are correct, we are counting duplicates as well, but here's my thinking. Consider a number of happy people in node i. Now from node i, consider we can move down to three nodes, i+1,i+2,i+3. Now, the child nodes will have a "subtree sum" of their own. now assume this to be some s1,s2,s3. Now, how did we get these many people in those subtrees? It is only because they travelled through the parents node, i. So now we can see that when you add the sum of good people from all three subtrees, to the parent node, It should be strictly greater than the sum of the individual subtree sum of their chidlren. (since that node must have 0 or more happy people) so s>=s1+s2+s3.

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

Can someone please help why am I getting a WA on problem D on test-7. Link to my submission Thanks in advance :)

UPD- I got the mistake.

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

If anyone need Detail Explanation(not a video tutorial) of C Here

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

Can anyone explain the solution of E in tutorial? I see most of the solution use trisection search. Is the tutorial also use the trisection search? Forgive my terrible understanding of the code in tutorial.

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

In Problem A: It is said to print "4 different positive integers", but why one of those 4 positive integers can not be zero ('0')? Where is the range is mentioned? Please help me to understand the constraint. Thank you.

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

Auto comment: topic has been updated by Karavaiev (previous revision, new revision, compare).

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

C was such a good problem even though I participated virtually and solved it 10 minutes after the contest. I did not read the question properly and missed the point that once the person becomes sad cannot become happy again.

It would have been better (easier) C if this was not the condition.

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

WHAT SHOULD I DO NOT TO GET RATING IN CONTEST?

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

hello, in problem B if I have n = 5 then the number must be 9 9 9 9 9

  • in binary : 1001 1001 1001 1001 1001

  • but after removing last 5 digits then it become

  • 1001 1001 1001 100- ----

  • which makes it a 4 digit number, so making that 5 digit number I need to make it all 0s :

  • 1001 1001 1001 1000 0000

  • but this doesn't the case.

  • Can anyone help me with that ?

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

    How will the last digit be Zero. zero has a single digit representation in Binary.

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

      okay but why should be 8 ?

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

        could you elaborate a bit .

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

          because we want a number with 4 bit binary representation. That way, our number will be maximum. We have only two options for this. 8 or 9.

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

            why not nine ? I got that 0 cannot be the answer but not getting that how 8 (1000) fixes in the answer ? can you please explain the above example n = 5?

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

              Why not nine? because 99...8 is smaller than 99...9 Read the problem again, They both give same answer unless n%4=0 for n=5
              1001 1001 1001 1001 1001
              Why not your logic?
              1001 1001 1001 100,1 1001
              You're gonna remove everything after the comma. So if you're gonna remove it anyway, you might as well make it as small as possible.
              1001 1001 1001 100,0 1000
              Notice that in this way, all digits are still 4 bit and it is also smaller than all 9's

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

I have an alternate solution for problem D . First transform the array b into a forest of directed trees with all edges away from the root by drawing edges from vertex x to b[x] when b[x]!=-1 (the nodes with b[x]= -1 will be the roots of the respective trees of the forest). Now, run a dfs from the root of each tree and for every node x, calculate a value say sum[x] such that sum[x] is equal to (sum of max(sum[z],0) for all children z of x) plus a[x]. The sum of sum[x] for all nodes of the forest is the required answer.

Also construct an auxiliary graph such that if sum[x]>0 draw a directed edge from x to b[x], otherwise draw an edge from b[x] to x(provided b[x]!=-1) . Sort the vertices in this graph topologically to get the permutation. 88585416

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

I solved C a bit differently. I used the name $$$liv$$$ instead of $$$p$$$ to indicate resident number of a node. For every node $$$v$$$, I maintained a range $$$[L_v, R_v]$$$ in which it's happiness index must lie. Thus, $$$L_v\leq h_v\leq R_v$$$ For a leaf, the range is $$$[-liv_v, liv_v]$$$.

Now, a node $$$v$$$ returns a different range for the residents of $$$v$$$ who started the journey from $$$par_v$$$. Notice that this range will be $$$[h_v, R_v]$$$, because if happiness in $$$v$$$ is $$$h_v$$$, then it was at least $$$h_v$$$ on its parent, with the possibility that it might be as high as $$$R_v$$$ ( Mind that one's mood gets bad only on the road AND the unhappy people never improve their mood ). Also, $$$R_v-h_v$$$ has to be even, by the definition of happiness index.

For an intermediate node $$$u$$$ and $$$v$$$ $$$\epsilon$$$ $$$children(u)$$$, $$$[L_u, R_u] = [-liv_u+\sum_{}L_v, liv_u+\sum_{}R_v]$$$. Because the lower limit for $$$u$$$ can be as low as "everyone's" lower limit, including itself. Same goes for the upper limit. Return the range $$$[h_u, R_u]$$$ with appropriate checking as mentioned above. if you get a valid range at the root, it's ok. Otherwise the machine has error and print NO.

My submission 88504884

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

For problem C, can someone provide a test case where my solution fails.

I have added appropriate comments to my code for better readability.

Please help!

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

    I think your definition of $$$a$$$ and $$$b$$$ in $$$dfs2$$$ function is wrong. In $$$pto_v$$$, you have basically the number of people that have ever passed node $$$v$$$, right? Then what does pto[p]+hp[p] even mean?

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

      pto[p] is what you have said. hp[p] is the given happiness of the city (i.e given as input).

      Even the tutorial has this formula. I cannot understand what is wrong with pto[p]+hp[p].

      Can you please elucidate a bit more?

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

        I see. Your code comment was misleading. $$$a$$$ and $$$b$$$ were $$$2$$$ times of happy and sad people respectively, though it will eventually be true a little later. Anyways, in that case, I'd want to ask you, what does your dfs2 actually do? What I think is that it returns how many happy people have reached some node. In that case, you should return $$$a$$$, not $$$a+sum$$$ ! The $$$sum$$$ is needed to check any discrepancy at the children, but it shouldn't be added. In fact, I just submitted your code with return a; in dfs2 and got AC. Maybe you were "too lucky" to get past this many testcases.

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

          Yes, I forgot to mention that a and b are twice of happy and sad people respectively (usual stupidity that I do, sorry)

          I was not able to enforce the necessary conditions in dfs1, so I wrote dfs2 to declutter my code.

          Extremely grateful to you for going through my code and pointing the error. Now, I understand what was wrong with my code.

          Thanks a lot again.

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

Help please Problem D https://codeforces.net/contest/1388/submission/88590473 checkler log is showing sequence have duplicates.

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

    In the 'else' in the 'while', you need to lower the indegree of brr[ind] (not only inside the if, but also in the else).

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

Can somebody explain me if there were cycles in D, how to calculate the maximum value in the cycle after removing all non cyclic elements? (in O(n))

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

Congratulations to adedalic for amazing coordination of this round!

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

Please help me clarify my doubt:

In the second question (B) since we are asked to output the smallest value of x such that r is maximized after removing last n digits, so why the answer is not like this if n = 5 then x minimum x can be 99980 but the correct output is 99988 the value of k will be the same in both cases k = 100110011001100 and 99980 less than 99988 then why the answer is 99988. please explain to me if I am wrong or the question is cause i was stuck on second problem and then i lost hope and stopped attempting further.

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

Hi, Would anybody help me with Question C?

This is my submission. https://codeforces.net/contest/1388/submission/88520244

My way of approaching the problem is the same as the editorial except I calculate number of bad people instead of the number of good people.

My submission is wrong at the 27th input of the 3rd Test Case.

Thanks for the help.

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

    Try this test case 1

    3 14 
    2 4 8 
    14 4 8 
    1 2 
    1 3
    

    The answer is "NO" Because the total sum of good people cities 2 and 3 is 12.

    amount of good people in city 1 is 8.

    the statement says that we can't increase the number of good people.

    (srry if my english is not so good)

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

      My submission is "YES". But I think the answer is "YES" also

      Because happiness in city 1 is 14 and there is 14 people visit city 1. so number of good people = (14+14)/2 = 14 people. The number of good people leaving city 1 is 12 (14 — 2(people staying in city 1)). The total sum of good people in cities 2 and 3 is 12 which is (=) the number of good people leaving city 1. So the number of good people did not increase.

      Do I have any misunderstanding on the problem? Thanks for your reply and kindness.

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

        Try this test

        2 5
        2 3
        -1 3
        1 2
        

        Answer must be no. 3 bad and 2 good people at vertex 1. h[1] = -1, OK. 3 bad people will visit vertex 2. h[2] must be -3. Therefor answer is NO

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

          ok Yes, that's it. I am wrong because I ignore all the vertex that has 1 connected vertex when I consider the 3rd Criteria. (All the leaf of the tree are of the size 1) But I forget that vertex 1 can be size of 1 too.

          Thanks a lot for your help and kindness.

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

88635193 Can you explain why my code fails? my contest submission was -- 88482149, I know in the contest submission I did a calc error but the idea of my solution was the same, but after rectifying the calc error it's still showing wrong answer. Though it's written if there are multiple answers we can print any of them.

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

    for condition a==44 you have used 13 as one of the number which is incorrect because 13 is itself a prime number.

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

      Yes I thought about it but in the question says

      Captain Flint guessed an integer n and asked you: can you represent it as the sum of 4 different positive integers where at least 3 of them should be nearly prime.

      where they said 3 of them should be nearly prime rest can be a distinct integer apart from these 3. Maybe I got the question wrong but the above lines I directly copied from the question.

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

    It literally tells you "wrong answer Sum of numbers is not equal to n (test case 4)". Test case 4 is n=36.

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

      88635193 my solution was 15 10 6 5. where 3 of them are nearly prime 15,10 and 6

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

        Do you know what "Sum of numbers is not equal to n" means?

        Edit: now I see that n=36 is fixed in that code; you should actually submit it to the correct problem.

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

        first of all this is your code for 1st task and submission on 2nd task. you are seeing jury's answer as your answer.Your answer is the participant's output

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

hello, i am a newbie in coding and I didn't understand how problem D is connected to graphs. Can anyone tell me what's the idea/algorithm behind this question. Should I be knowing something before trying this question??

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

    the values of b array were forming a chain(that is b was connected to the index in a and further the value of b in the new index was connected to some other index of a) and to solve such chaining question we can use graphs.

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

Could someone explain why does the following code for problem C gives WA ? I am not able to find any substantial difference between this code and the official solution's code.. In particular, it will be nice to have someone give me a test-case where my code fails. Also please do let me know if anything is unclear in my code; I will explain what I intend to do using that particular instruction.

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

    Can someone explain me what's the difference in the logic of my submission please? I have been trying to figure it out for a while but am not able to find any difference, so could someone tell me please ? In particular, my code seems to fail on the 27th test case of the 3rd test, but I am unable to view this, so could someone at least tell me what this test case is ?

    Thanks in advance!

    UPD: I found the mistake — a really stupid one. Instead of keeping sum <= good[v], I kept sum<= peo[v], a big overlook!

    Sorry for pestering anyone who intended to help me.

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

how can i find answers of all the proplems ?(sorry if my English is bad)

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

I think in the editorial for problem B it should read "at most 4*p-3" instead of "at least 4*p-3"

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

For problem C, shouldn't this give a wrong answer?? I didn't check if number of people in good mood in a city are non-negative.

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

What's wrong with my solution? I used BFS two times.

1) Operate on the nodes that are +ve or become +ve while traversal, from top to bottom. Mark them.

2) Operate on the remaining negative nodes. From bottom to top

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

can anyone check and help me understand why memory limit is exceeded in my solution for problem C 89311627

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

In 1388C — Uncle Bogdan and Country Happiness, consider the 3rd condition:

gto1+gto2+…+gtok ≤ gv, where to1, to2, …, tok are the cities where the resident can move out of the city v on the way home.

Shouldn't we compare gto1+gto2+…+gtok with number of happy (in good mood) people leaving the city v instead of comparing gto1+gto2+…+gtok with gv (number of people in a good mood who visit the city v = number of people in good mood passing through city v + number of people in a good mood who live in city v), to ensure no one becomes happy during the journey.

»
4 года назад, # |
Rev. 3   Проголосовать: нравится +5 Проголосовать: не нравится

Nice problems except for the long essays here

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

I have solved problem D using the concept of topological sort order in DAG. You may have a look at the tutorial that I have written for this problem in my blog, Link.

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

Problem D: 279808093 — is causing a WA7 due to wrong output format Unexpected end of file — int32 expected.

It worked fine for larger inputs in Tests 4, 5 and 6. But for some reason fails on 7, could someone help me debug the code?