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

Автор hloya_ygrt, история, 9 лет назад, По-русски
Tutorial is loading...
Код автора
Tutorial is loading...
Код автора
Tutorial is loading...
Код автора
Tutorial is loading...
Код автора
Tutorial is loading...
Код автора
Разбор задач Codeforces Round 384 (Div. 2)
  • Проголосовать: нравится
  • +123
  • Проголосовать: не нравится

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

Что с разбором?

UPD: Кажется, исправлено

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

Could anyone explain D to me again? I could not fully get the editorial.

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

    Do DFS.. For each child get 2 values their pleasentness sum and the max pleasentness and 2nd max pleasentness value till that child.

    Keep one max_ans and update it with max(max_ans,max1+max2)

    Update and return your sum and max(your sum, max)

    Do the above for each node and finally print max_ans

    This works because of the 'max1' and 'max2' coming from the different subtrees of the children thus satisfying our condition that the subtrees shouldn't intersect

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

My solution to B is, the answer is the index of the right most bit 1 (1-based) in the k. 22954844

I got this by solving some random values of n and k, but have no proof why this is correct :D, anyone knows why?!

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

о, сразу разбор, круто

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

I have a bit different approach for problem E, which fits in the TL even when N is up to 10^6.

The solution consists of tho phases. The problem is much easier if we can find such K that every element will be present either K or K+1 times. We can binary search to find it. Foreach step of step binary search, we will define dp[mask] as the smallest index of the sequence for which it is possible to have a sequence of number from mask ending on it. Updating this dp is quite easy, we just check all possible additions of the last element to our mask and binary searching on prefix sums the new interval.

The second phase is as follows: we have the correct K found, so every number will be present either K or K + 1 times. Once again we will do bitmask dp, just considering all possible 28 cases this time and using the previous idea.

Overall complexity: O(2K * K * logN * logN + 22K * K * logN)

You can refer to my submission: 22976778

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

    isn't it just binary search on possible values of len?

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

    Good idea. I'd just like to add some improvement.

    Define dp[ind][mask] as the size of largest sequence that can be built starting from index ind and only using elements unset in mask, with each element being repeated x or x + 1 times.

    For each state, either you'll add the element at index ind for x times, or x + 1 times, or 0 times and move to ind + 1.

    We can do some pre-computation to be able to get the position of x - th following occurrence of some element in O(1).

    Clearly, this function for a fixed x is O(N * 2K).

    As you've said, we can do binary search on x, and the validation function will be whether this dp returns 0 or not.

    This way achieves better complexity and simpler code.

    Total complexity: O(log(N) * N * 2k).

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

      Sir I did not understand the binary search part how are u using binary search and for what purpose? I am a newbie it would be very helpful if you guide ?

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

        Each number in the required sequence will be repeated x or x + 1 times.

        If there's a sequence with some x, then of course there's another sequence with x - 1. You can just delete one occurrence of each number from the first sequence to get the second one.

        Since you want to maximize the sequence length, you want to maximize x. And since the possibility of getting sequence with x is monotonic (i.e. it's true in some range [0, y) , and false in [y, OO) ), you can use binary search to get y.

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

can someone explain to me how did we get those formulas for C?

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

    2/n = 1/n + 1/n

    so, x = n; observe that, 1/y + 1/xy = 1/x if y = x+1;

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

    2/n = 1/n + 1/n

    So we have just made the problem much easier because how we just need to represent 1/n as a sum of 1/x and 1/y (then we would have 2/n = 1/n + 1/x + 1/y)

    Then let's take a look at the second testcase and find out that x = n + 1 and y = n * (n + 1) are right for all the cases (just because 1/(n + 1) + 1/n(n+1) = n + 1 / n(n + 1) = 1/n)

    Let's check that 1/x != 1/y != 1/z

    n != n + 1 for every n

    let n = n^2 + n then n^2 = 0, n = 0. But all n are positive so this also work for every n and the last case n + 1 = n^2 + n <=> n^2 = 1 <=>(n is positive) n = 1. So in this case we can't represent 2/n as 1/x + 1/y + 1/z

    Finally x, y, z would be less than 1e9 because n less than 1e4, so n^2 + n is less than 1e8 + 1e4 , so this restriction is useless.

    As for me, I can't guess why this problem costs only 1250, I think it is much harder than B and than common Div2C

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

    Here You can read here for more information :D. This is the website that I googled during the contest time.

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

Задача B

Решение за O(log(k)):

Рассмотрим нашу последовательность, заметим, что на каждой четной позиции (0-индексация) стоит единичка, т е если k четно, то ответ — 1

Иначе, удалим все единицы, получим новую последовательность, а k поделим пополам (т к колво единиц — половина от общего числа чисел). В новой последовательности на каждой четной позиции стоят двойки. Если новое k (после деления на 2) четно, то ответ — 2

Иначе удаляем все двойки и т д (видимо по индукции можно доказать, что так делать можно сколько угодно раз)

Исходя из этого алгоритма, ответ на задачу — номер самого младшего 0-бита в числе k (точнее k — 1 из за 0-индексации)

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

How could i get the formula for C Mathematically ?

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

    You know sample test 2? Take it for instance... Suppose you can write x=n, y=b, z=(n*b), ok? Then,

    2/n = (1/n) + (1/b) + (1/(n*b))

    1/n = (1/b) + (1/(n*b))

    1/n = (n+1)/(n*b)

    1 = (n+1)/b

    b = n+1

    Then, x=n, y=b=n+1, z= n*(n+1) solve the equation, since they are all different;

    For n=1, you can see that there's no solution: if x=1, then we have to find y and z that satisfies 1=(1/y)+(1/z) Since they must be different from x, we can maximize the value of the equation by y=z=2, then we would have a solution; in that case, y=z, and it can't be a solution by the hypothesis that they are all different ;)

    As you can see by sample test 1, it is not a "formula", there are other solutions. You have to find a "pattern" that works for any given n ;)

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

    what i did was to break 2/n = 1/n + 1/n now we break the whole equation into 1/n + 1/n = 1/x + 1/y + 1/z now break it further that is

    1/n = 1/x + 1/y -> 1

    1/n = 1/z -> 2

    from 2nd z = n

    1/n = 1/x + 1/y ( Diophantine Equation )

    whose solutions are x = n + a and y = n + b and a*b = n*n

    take a = 1 and b = n

    therefore x = n + 1 and y = n + n*n

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

Кто-нибудь может объяснить, как из условия задачи D явно понять, что два поддерева должны не пересекаться?

Из условия: Иначе говоря, не должно быть подарка, который был бы подвешен к подарку Хлои и подарку Владика одновременно.

Рассмотрим следующий граф: 2 вершины, 1 ребро между ними. Если мы выберем обе вершины как подарки, не будет подарка, который подвешен к подарку Хлои и подарку Владика одновременно.

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

    В данном случае это невозможно тк или первый подарок подвешен ко второму или второй к первому. И взятие верхнего из них автоматически возьмёт и подвешенный к нему подарок.

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

    они решили выбрать себе два таких различных подарка, что множества подарков, подвешенное к ним цепочкой из веревок и других подарков, не имеют пересечения. Иначе говоря, не должно быть подарка, который был бы подвешен к подарку Хлои и подарку Владика одновременно

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

For B just do F(k)=1 if k is odd, otherwise F(k)=F(k/2)+1, this will be O(logK).

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

В задаче А наверное O(n).

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

Question B has been tagged as bitmasks but what i did was something different. here is what i did . http://mirror.codeforces.com/submissions/logicbomb1#

How to do it using bitmasks?

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

I thought that problem A was a SSSP problem. After reading the tutorial, I realised that it is indeed a trick. But what if we have 3, 4, 5... companies rather than 2? Are we able to use any simpler algorithm than dijkstra's?

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

    First, observe that we only need edges between adjacent airports (cost 1) and between airports of the same color (cost 0). Although we have the option to jump from any airport to any other with cost |i-j|, we can reach it with the same cost by jumping there 1 by 1 (ex. instead of jumping straight from 3 to 6 with cost |3-6|=3, we can jump 3-4, 4-5, 5-6 with cost 1+1+1=3). So now we would have n edges except that we still have edges with weight zero between every pair of airports with the same color, giving potentially n^2 edges. To fix this, change your nodes from airports to airport colors (since any two airports of the same color can be reached with cost 0, we can consider them the same node). Now you only need the linear edges between adjacent airport (color)s, and you can do a BFS (all edges left are weight 1) with O(n) nodes and edges, so O(V+E) -> O(n)

    Note that in the case where there are only two colors, the graph you create will end up being either two connected nodes (if you have both 1's and 0's) or a single node (if the whole string is 0's or all 1's). This means the shortest path will always be 0 if you are the same color as the target, or 1 if you differ.

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

В задаче Е, можно еще раз, как мы определяем динамику?

Мне кажется, что я как-то неправильно понимаю, потому что не понимаю, что значит "для перехода переберем новый цвет", и как с помощью этой динамики проверить, подходит ли конкретный len.

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

    Ok, я понял, как выглядит динамика и как она пересчитывается. Правда, видимо, это нужно трактовать как максимальное кол-во цветов, которых мы берем len + 1, и заканчивая ровно на позиции pos.

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

I found the problem statement of Div-2 D quite confusing. For the pretest-4
Pretest-4-image

Ans according to me should be 19 but ans is 6. Can someone please help me understand why we can't rope between ( 1 and 8 ) and then give 1 to second person ( 16 + 3 = 19 )

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

    In the problem statement : "...the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible."

    Because they both have to cut the rope, they can't take the subtree with root = 1.

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

      ok then also lets cut the rope between ( 8 and 10 ) and then between ( 1 and 8 ). Then A will get ( 4 + 3 + 2 + 2 -4 + 3 + 3 = 13 ) and ( 3 ) therefore sum = 16 and not 6. Please correct me if i am wrong

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

        @saisumit : you cant cut the tree that way.

        Read the problem statement carefully:

        " they will choose prizes at the same time "

        [ You can't cut one part, and choose the next part later ]

        Also : " they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect "

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

    Remember "they will choose prizes at the same time", so you can't cut the rope between 8 and 10. In another word for "at the same time", you have to choose two disjoint subtrees. So the best solution is choosing only vertex 6 and vertex 7 which has answer = 3.

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

Could someone plz clearly explain the problem statement of D, do we have to consider different subtrees i.e. can't we take a subtree and then take a subtree which consists of it's ancestor. For eg : — if input is like this

3

2 2 2

1 2

1 3

can we first take 3 and then the remaining subtree of size 2 so total answer is 6, or should we take only nodes 2 and 3. can't we choose two subtrees so that one of them contains the lowest ancestor of other.

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

For given constraints in E, I didn't use Binary Search and got AC.
The complexity seems to be O(n2.2k), but I wonder whether all dp states would be useful here or not because the running time seems to be pretty fast even without binary search.

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

Can someone please explain me what is wrong with my solution for A. I have used the proper way to solve it which should be aaplicable even if two diffrent airports of type a and b were not adjacent to the destination airport.

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

    you just finding min distance with signs similar to s[a] and s[b], but it is not true. example: 5 2 5 11100 here your answer is 2, but you can go to 3rd airport for free (it's similar to s[a]) and you go to s[4] it will cost 1 and you will go to s[b] for free(s[4] equal to s[b])

    shortly, answer will be one if s[b] and s[a] different airports, and 0 if they are same

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

Nice edition

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

Can someone explain me the 743D — Chloe and pleasant prizes problem statement ???

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

My solution for problem B 22969930 :D

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

My solution for E is like O(8 * N + 8! * 2^8 * 8 * Log(N)). Did i win an asymptotic war?

And it works for 8 seconds on my local machine with n = 10^7

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

    I didn't came up with a way to maintain the length of dp array so I also ended up trying all possibilities, thank god k! (k=8) isn't that big.

    Haven't seen a Div2E problem been this open-ended for a while.

    My solution

    Time complexity, where k is the variety of cards: O(log^2(N) * k! * C(k, k/2))

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

Deleted

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

Isn't the complexity for problem A, O(1) ? Why is it mentioned as O(n) ?

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

My one-line solution to B : 22997490

It gives the position from the right of the first non-zero (1) bit. Using a builtin G++ function (that directly uses architecture-level instructions) to do this , is much more efficient.

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

How can the complexity of A be O(n) ?

Shouldn't it be O(1)?

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

I think test 27 is wrong here. The test case is the following- 10 5 8 1000001110 The jury's answer is 1. It should be two. According to me there are three possible cheapest ways to get to the destination. first is 5 to 6 then 6 to 8 price (8-6 =2) second is 5 to 7 then 7 to 8 price (7-5 =2) third is 5 to 10 then 10 to 8 price (10-8=2) my submission

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

    Answer is 1. Go from 5 to 6. : Costs 0

    then 6 to 7 : Costs 1

    then 7 to 8 : Costs 0

    Total Cost : 1

    No need to go directly from 6 to 8, use the fact properly that going from one index a to other index b such that I is 0. Infact, the answer can never be greater than 1 since the indices of transition from a '0' to '1' in the string are always consecutive.

    Code

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

sizeof(A's code) + sizeof(B's code) + sizeof(C's code) < sizeof(D's code)

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

Could you explain why the bitwise XOR works in problem A? I used a different logic though.

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

Can someone please explain me the sample code 3 of problem E. (i didn't got the condition 2 satisfied for it.... as far as the condition is exampled in question 1122 and not 1221)... please.

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

Div 2D :- Can someone tell why this got AC D-AC but this didn't D-WA ? Although both of them do same thing ,ie find top two child values. Changes are only in dfs function.

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

can anyone tell me why my code is not working on test case 15.: 23014828 code link:(http://mirror.codeforces.com/contest/743/submission/23014828)

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

Problem E is interesting, thanks!

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

In setter's code, I can't understand ~~~~~ amax(dp[in[k][it] + 1][j | (1 << k)], dp[i][j]); ~~~~~ mean ? Could anyone explain E to me again? I could not fully get the editorial

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

In E, I did not get the state represented by the dp. What exactly is the mask representing ? I found the editorial explanation insufficient.

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

    Our subsequence looks like this:

    c_1, c_1, c_1, c_1, ..., c_2, c_2, c_2, c_2, ... c_8, c_8, c_8, c_8,

    where c is some permutation, the order in which we are taking the colors. On each position in the array we have to know how many and what exact colors we had already taken. mask would be a bitmask of 8 bits, where i-th bit is 1, if we had already passed the segment with number i in c, otherwise 0. So the whole state is pos, mask — the maximal length of the subsequence, if we stand at the position pos in the array and already passed all the 1 bits in mask.

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

D approach:

For a node i let ai be its pleasantness.

Let where {h1, h2, ..., hn} are the children of i

and let ci = max(bi, bh[1], bh[2], ..., bh[n])

We compute each node bi and ci with a o(n) recursive dfs.

Now we'll find the solution of the sub-graph that contains a node (any) i as root. Denote this procedure dfs(i)

Case 1: the node i is the only node of the graph. Then it is impossible to generate a pair (that is the solution)

Case 2: the node i has only one child. Then the solution will be dfs(h1)

Proof: we can't choose i, in the case we do: we select the root => we can't select another node => we can't make a pair. So we need to choose another node, the solution will be inside the sub graph of the only child. dfs(i) will contain the same pair solution (if it exist) that i should hold

Case 3: the node i has two or more children. Now things get more interesting

Let max1, max2 be the child with 1st and 2nd greater ci respectively. They will always exist as there are two or more children.

now, we need a key observation

  • There is an optimal solution that don't use both nodes outside max1 subgraph

Proof: any solution that do so can replace one of its two elements with a node inside sub-graph max1 that will be always greater or equal that any node outside max1

So there are two chances

  • The optimal solution uses one node inside max1 and one outside (1)
  • The optimal solution uses two nodes inside max1 (2)

in the case (1) we will set the solution as cmax1 + cmax2 The sum of the two greater nodes of max1 and max2.

in the case (2) we cant set the solution as cmax1 because the two nodes lie inside max1. So we compute dfs(max1).

As we can do any of the two cases, then we compute both and we return the max value of them. (of course if dfs(max1) has no solution then we use only the case (1))

Total complexity: O(n)

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

Опечатка в задаче А. Там не О(п)

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

does anyone has a mathematically clear prove for the C problem cause i can't find any online.

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

What is the way to search for solutions to C, if you couldn't come up with the brilliant factorisation ?

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

https://mirror.codeforces.com/contest/743/submission/86323255 A simple way of doing b is to simply count the power of 2 in k and print it by incrementing one ... eg — for 18 = 2^1 * 3^2 .. ans is 2 do have a look at the solution.

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

if anyone has solved 2nd que using recursion,give ans link in comment section.