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

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

977A - Неправильное вычитание

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

977B - Двуграмма

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

977C - Меньшие или равные

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

977D - Подели на три, умножь на два

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

977E - Компоненты-циклы

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

977F - Последовательная подпоследовательность

Разбор
Решение (Vovuh)
Разбор задач Codeforces Round 479 (Div. 3)
  • Проголосовать: нравится
  • +54
  • Проголосовать: не нравится

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

Автокомментарии прекрасны.

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

Can someone tell me why my submission for C got TLE? I only used a sort and some if statements.

http://mirror.codeforces.com/contest/977/submission/37947136

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

Thanks for the contest!!!, it was a great contest to learn!!!

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

Nice problem 977D - Divide by three, multiply by two, my solution 37939642 runs in O(n2). I make the graph and then run topological order.

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

I tried this for problem D.

Take a boolean array check[n] and mark all as false. Then I tried making chains. Take the first element from array given that is false and mark it as true and this will start a new chain. Then for each other false element in array check if it can be added to the chain started by this element, either by appending it to the beginning or to the end. If yes mark this as true too. For beginning, check if the new element divided by 3 is equal to first element in chain or if new element multiplied by 2 is equal to first element in the chain. Similarly we do for the end.

However, I couldn't figure out how to decide a way to print all these chains in a valid manner. Is there any way to do figure this part out.

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

    You may try using double ended queue (deque in C++) to store the chain itself:

    When you decide to make a new chain starting at some element x create new deque and put your x into it.

    Methods push_front, pop_front, push_back, pop_back allow you to append and remove elements to your current chain from the beginning and from the end in O(1).

    When the chain is completely formed you may iterate through deque (or simply use pop_front until it will become empty) to get elements of your chain in the order you put them into.

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

I had a lot of fun while I participate in this contest. Thanks!

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

recently learn recursive backtracking and apply it to problem D, got AC :D

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

977C - Less or Equal: Another approach towards solving it can be by using Binary Search on the number x.

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

For C, if k == 0 and v[0] = 1, according to the tutorial/solution you would print 0. Shouldn't you print -1? As we have x in [1;109] or -1.

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

Почему мое решение четвертой задачи 37973724 на третьем тесте выводит в вашем компиляторе одно, а у меня все правильно работает(?

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

    Лучше не объявлять массив с переменным размером. У Вас оно, может, и скомпилируется, а другой компилятор может и не позволить — произойдёт переполнение.

    На что очень похожа эта ситуация — числа в массиве очень большие, поэтому резервируемого места не хватает и вместо двух чисел в ответе мусор.

    Попробуйте написать a[100] вместо a[n].

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

      Лучше не объявлять массив с переменным размером. У Вас оно, может, и скомпилируется, а другой компилятор может и не позволить — произойдёт переполнение.

      wat?

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

      Непонятно, причём здесь массив с переменным размером, компилятору то какое дело, переменный он или нет. Всё зависит не от компилятора, а от размера стека при тестировании (потому что локальные массивы создаются на стеке). На КФ с этим проблем, вроде как, нет, и размер стека равен ограничению по памяти в задаче.

      С чем точно будут проблемы — так это с long int. На локальном компе это может быть int64_t, но на КФ это всегда int32_t. long int -> long long.

      И это всё равно не помогает, так что где-то ещё есть баг.

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

        Прошу прощения за бред с размером массива:)

        Но ведь замена "long int" на "unsigned long long" решает проблему с этим тестом. Или Вы про тест 21? (3 4 1 2)

        UPD. Баг в том, что не выполняется проверка делимости на 3. Вместо строчки a3[i]=a[i]/3; Должно быть

        if (a[i] % 3 == 0) a3[i]=a[i]/3; else a3[i]=0;

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

    long int != 64 bit.

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

Another solution for D:

There can't be (x, 2*x, x/3) in the same array (x is multiple of 3) ... so for each number x, if it's not multiple of 3 you just go to 2*x, otherwise there's either 2*x or x/3 you can go to.

iterate over all numbers to start the chain with and check whether the chain exist.

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

Я анеме

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

Can someone hack this, or is it correct?

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

    This is correct, maybe you expected TLE, but (x, 2*x, x/3) can't be in the array at the same time then, suppose you know the first element of the solution only one of 2*x and x/3 are in the array so it is O(n) after found the first element and the number of starts is N all the elements of array so this solution run in O(N^2)

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

For problem D you can view the problem as a directed graph where x maps to x*2 and x/3 if x|3.

Start point has indegree of 0.

Ignoring "extra" numbers in your dfs (i.e number not in your array) a dfs from the startpoint will always find a chain (guaranteed to exist) that is your answer.

void dfs(ll cur) { if (s[cur*2]) dfs(cur*2); if (cur%3==0 && s[cur/3]) dfs(cur/3); ans.push_back(cur); }

(you have to reverse ans)

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

Here's my solution to D, a little different aproach: Sort the array, and then group up all numbers that goes with sequence like x,2*x,4*x,8*x until there is numbers that can go in that sequence (for example 4 8 6 3 12 9 has 4,8;3,6,12;9 — 3 sequences) I used map<int,vector> to store that. Then, simply try to merge them one by one, and see what sequence can merge with another sequence. Here's my submission for this problem

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

    This approach is quite complicated. I'm struggling with the variable names. Can you explain how you merge ?

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

      Well, first sort them. Sorting is important because i will go from smallest to biggest element and do this approach:

      If I checked that number, then you don't do anything, but if I didn't went for that number (let's say d[i]), then I will put in map<d[i],vector>.push_back(d[i],2*d[i],4*d[i]) — as long as there are numbers 2*d[i] or 4*d[i]. example: 3 4 6 8 9 12 in map<3,vector> push back 3,6,12. then I go to 4 and map<4,vector> push back 4,8. Then I go to 6, but it belongs to map<3,vector> so I don't do nothing. Again, I don't do anything with 8 and 12, but I didn't checked 9, so map<9,vector> push_back 9

      After that I have 3 maps:

      map<3,vector> (3,6,12)

      map<4,vector> (4,8)

      map<9,vector> (9)

      Then I iterate trough map, find for every one of them last number in vector (let's say that number is x) and see if there is map<x/3,vector>. If there is, then merge it until there is no such map. Merging is done in if (x%3==0 and m[x/3].size()>0) statement.

      after you done with merging that map, repeat the process until you iterate all elements in map. Because answer always exists, there will be for sure one map that has size n. Just print it. I hope I made it a bit clearer, I know it's complicated I struggled with it a lot :D

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

        Oh Good job !

        I understood it now. Please don't name your variables haha. It's difficult for others to understand.

        Actuallly, it can be much simpler. Every number x can have only one successor. Either 2x or x/3. An array can't have both 2x and x/3 because if you reach 2x you can never reach x/3

        So the sequence starting from each A[i] is unique.

        Now, all that's left to do is find the first element and find the sequence starting from there. This first element is an x, such that x/2 and 3x are not present in the array.

        I wrote about my solution here. :)

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

hi guys , can someone explain probleme c tutorial pls? i did solved but i didn't understand the solution

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

    Do you try to approach this problem with binary search ? If not so you should do it by binary search since its kinda help you to understand the solution. Here is my approach to problem C with bns.

    • First binary search a value x from 1->1e9 (boundery)

    • We can check how many number are <= x with a simple for loop suppose this number cnt

    • If cnt < k then l = mid + 1. if cnt == k then this is our desire answer. If cnt > k r = mid-1; (l,r here is the left and right boundery, initially l = 1, r = 1e9)

    So if understand this approach you will notice that the value that satisfy the problem is between some elements in the sorted array (i did some debugging order to realize this). And to be specific its the number in the solution above (between ans[k-1] and ans[k] — 1 i think!). The fun part about binary search solution is that you dont have to worry about some corner case! Hope this help

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

Can someone explain me the editorial solution for D?

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

My solution to last problem got hacked (input resulted in TLE). After changing "unordered_map" to simply "map" it gets accepted. Why is it so? I know that "unordered_map" are generally less efficient for range iteration through a subset of their elements, but here random elements (having value one less than the current one) is being accessed.

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

Can someone tell me why my submission for F got WA?

http://mirror.codeforces.com/contest/977/submission/37961004

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

    guys, i really dont understand why my approach is wrong. I just look for every element that wasn't used yet next element of the sequence in the array, using binary search, while it is possible. And then just check if this sequence has the biggest length. I would be glad, if you will say what bug my implentation has, or what mistake in my approach, or some testcase which i can check by myself.

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

The editorial's for D is amazing!

We can also prove that under the operation (x2 and /3) no number occurs more than once, and since the problem says that there's always a solution, we can be sure that every number has a degree of at most 2, so we can just find the head of this chain (which has no predecessor) and just iterate from it giving an O(nlogn) complexity.

submission 37959164

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

    how can we say it cannot have duplicates? I used a set after contest and passed but it still doesn't feel right

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

      If there is a pair that duplicate (have the same value) then you'd not be able to include it. Hence its will conflict with the problem statement. That how i understand

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

      Well, imagine you had two numbers with the same value say X, multiplying by 2, and dividing by 3 are two independent operations, meaning you can't reverse them by using the other operation, hence you can't have the same numbers unless you can multiply by 1 which is not mentioned in the statement, since there's no operation that will keep X as is for two or more operations, we conclude that all number must be distinct.

      Simple check against that fact: add assertion to your code to check the validity of this.

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

In problem D, how can I prove that for the solution to exist the graph has to be a chain?

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

    If it diverges it can't have an answer. This is because you can view the answer space as an infinite tree. This is because x/3 and x*2 cannot be reversed. (i.e there is no x*3 and x/2 operation)

    Problem guarantees answer.

    Therefore it cannot diverge.

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

Can someone explain me why my submission for C got TLE after contest finished? Yesterday it got Accepted but today it shows TLE on XX test :/ I already fixed this small optimization bug on reading big arrays but still. Kinda unfair in my opinion.

http://mirror.codeforces.com/contest/977/submission/37940448

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

please someone explain the problem D in details with approach.. :( :(

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

Great contest. I enjoyed the problems immensely even though they're easy. It's a great way to distinguish between participants who are green and cyan. It helped me become blue ! Which was the goal of my year. I'm so happy I was blessed with the grace to achieve this goal. I'll work hard to sustain my rating and increasing my knowledge.

I was wondering if anyone could rate the difficulty of these questions in terms of Div 2. i.e. ... If these questions were used in Div 2, which number would they be at ?

Here is my GitHub repository of my solutions. Feel free to ask if you have any doubts. :)

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

Can Someone Explain Problem F With Example

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

Many specialist became expert. it seems that new expert become instead of old specialist.

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

F was a variation of LIS problem. I used recursive DP but got TLE... How can I optimize it any further or do I have to use Iterative DP instead of recursive?

Here's my code:

http://mirror.codeforces.com/contest/977/submission/37962276

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

This submission: http://mirror.codeforces.com/contest/977/submission/38003904 for problem D: http://mirror.codeforces.com/contest/977/problem/D works well on my machine, and is not able to pass test 1 on server. Can anyone help me with it ?

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

    I made some changes in your code. In bool DFS(int idx, int cnt) function you were not checking if idx can be last vertex of sequence, and in the end of same function you should return false. Corrected Solution change at line no. 43 and 58.

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

      Thanks lot my friend, about: "In bool DFS(int idx, int cnt) function you were not checking if idx can be last vertex of sequence" It is not necesary since I always enter in a non-visited node, the very bug here was that I wasn't returning false, as you certainly pointed: "in the end of same function you should return false." I already fixed it !!! and AC. Thanks a lot again

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

любил конкурс много

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

For problem #F, if the input is:

3
1 3 4

why the ans is:

2
2 3

there is no 2 in the array? Maybe I haven't understand the meaning?

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

I have question, you have said " To qualify as a trusted participants of the third division, you must: take part in at least two rated rounds (and solve at least one problem in each of them)", So, why someone are rated in div3? (just like Milhous who is the rank1 in the contest)

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

    Regardless of whether you are a trusted participant of the third division or not, if your rating is less than 1600, then the round will be rated for you.

    So although those participants are not trusted, they are still rated.

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

Question D was very interesting and had many solutions.

The editorial's approach was to sort by powers of 3 first and then powers of 2 to break the tie. But since the operations only affect the power of 2 and the power of 3, we can say that if the power of 3 is equal, the number with the greater power of 2 will be the larger number.

So, sort by power of 3 first, and then the number itself.

My approach was to sort it by the criteria exp(2) — exp(3).

Here is a detailed analysis of my approach on Quora.

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

For D, just sort with custom cmp function: if exp_2( a ) < exp_2( b ) then a goes before b. if they are equal, then a goes before b if and only if exp_3( a ) > exp_3( b ) — so this time the sign is opposite. Then just sort array. complexity is nlogn so I dont understand why was n<=100 given.

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

How to solve problem E-Cyclic Components using Disjoint Sets?

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

    Find the nodes of each connected component (nodes having same DSU-parents) and push the nodes in a vector. One vector for each connected component.

    Now for each connected component:

    • If it has p nodes and p+1 edges, then it is a cycle. In other words, each node of it must have an adjacency list of size to.
    • Else it is not a cycle.
»
8 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I am unable to understand 977F. The problem states to "choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers".

How does 2 3 5 6 satisfy the condition about consecutive integers. Also 2 isnt even in the input!

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

Can somebody tell me why my solution to F fails?
Here's the link : Link. Thanks :)

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

It would be a great help if someone explains what is logically wrong with this. http://mirror.codeforces.com/contest/977/submission/38408042

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

I solved E by dfs. when i used dfs by using stack i am getting tle on tc 6 whereas on using recursion my solution got accepted . Can someone please tell me the reason.

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

In problem E, Dont you mean if the vertices degree is divisible by 2?

Here you have 1 of degree 4.

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

Can someone please tell me why I got the wrong answer on test 5 for this submision: 40518652

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

can someone help me about how i got tle in 977f

48190103

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

Also, we can solve D by using DFS. We can build a graph of given numbers: if we have number u and (u / 3 or 2 * u) -> create edge. Our task is to find the path of n length.

Solution: https://mirror.codeforces.com/contest/977/submission/58871899.

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

Can someone please explain why this solution for question D gives a TLE verdict? Isn't the time complexity for this code O(nlogn)? Or is there something else that makes this code really slow? Here's the link to the code. 71393068

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

Can someone please tell me a short test case where my code for problem F may have gone wrong

Link for my code : https://mirror.codeforces.com/contest/977/submission/80704453

Link for question : https://mirror.codeforces.com/contest/977/problem/F

My approach :

I inserted all the positions of each element in a map< long long, vector< long long>>.

Then I'm simply traversing over all the keys of the above map, and checking if the keys are consecutive or not.

If they are not then break, otherwise check if that element is present in the right part of the array from where we have picked the last element.

If not possible then break, otherwise make that element as our current and continue doing the same while inserting the picked up indexes in a temp vector and check if size of that vector is greater than previously stored ans and then replace it.

Now there's obviously sth wrong or incomplete in my approach, and it would be highly helpful of anyone to help me.

Thanks in advance !!

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

The spoiler is bugged, please fix.

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

F is nice!!

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

i do F by dsu , in nlogn by not using rank compression

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

discretization + binary search + linked list to solve F.221689456

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

Is there a Disjoint set union approach for E? I tried to figure it out but it gave wrong answer on test case 18.

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

For problem F it is enough to define $$$dp[a_i] = dp[a_i - 1] + 1$$$ because as we increase $$$i$$$, the answer for each state can only increase.