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

Автор Ahmad_Elsagheer, 9 лет назад, По-английски

Hello Codeforces, I hope you enjoyed the round!

Just some notes about the problems:

  • In problem A, the picture and example notes should complete your understanding for the problem if the statement itself is not clear.

  • Solutions that check the lights of each part separately should have failed on pretests. My bad.

  • Problem C was assigned as B at the beginning, but moved to C lest it is harder than B difficulty. However, I think problem B is still easier than problem C (check the solution below).

  • I thought problem D is easier than problem E. Once, conditions are well-understood and related to each other and the problem is modeled correctly, then its implementation is easy.

The points I have just described is my own opinion in the problems. Of course, you might have a different point of view. However, I would like you to keep in mind that I did my best to make statements clear and pretests strong.

Thanks for your understanding!

812A - Sagheer and Crossroads

For pedestrian crossing i (1 ≤ i ≤ 4), lanes li, si, ri, si + 2, li + 1, ri - 1 are the only lanes that can cross it. So, we have to check that either pi = 0 or all mentioned lanes are 0.

Complexity: O(1)

Implementation


812B - Sagheer, the Hausmeister

When Sagheer reaches a floor for the first time, he will be standing at either left or right stairs. If he is standing at the left stairs, then he will go to the rightmost room with lights on. If he is standing at the right stairs, then he will go to the leftmost room with lights on. Next, he will either take the left stairs or the right stairs to go to the next floor. We will brute force on the choice of the stairs at each floor. Note that Sagheer doesn’t have to go to the last floor, so he will go to the highest floor that has a room with lights on.

Complexity: O(n·2n)

Implementation


812C - Sagheer and Nubian Market

If Sagheer can buy k items, then he can also buy less than k items because they will be within his budget. If he can’t buy k items, then can’t also buy more than k items because they will exceed his budget. So, we can apply binary search to find the best value for k. For each value k, we will compute the new prices, sort them and pick the minimum k prices to find the best minimum cost for k items.

Complexity:

Implementation


812D - Sagheer and Kindergarten

Let’s go through scenario requests one by one. For request a b, if toy b is free, then child a can take it. Otherwise, child a will wait until the last child c who requested toy b finishes playing. Since, no child can wait for two toys at the same time, each child depends on at most one other child. So we can put an edge from the a to c. Thus, we can model the scenario as a forest (set of rooted trees) as each node has at most one outgoing edge (to its parent).

For query x y, if toy y is free, then child x can take it and no child will cry. Otherwise, toy y is held by another child. Lets denote z to be the last child who requested toy y. So x now depends on z. If z is in the subtree of x, then all children in the subtree of x will cry. Otherwise, no child will cry. We can check that a node is in the subtree of another node using euler walk (tin and tout) with preprocessing in O(n) and query time O(1)

Complexity: O(k + n + q)

Implementation


812E - Sagheer and Apple Tree

In the standard nim game, we xor the values of all piles, and if the xor value is 0, then the first player loses. Otherwise, he has a winning strategy. One variant of the nim game has an extra move that allows players to add positive number of stones to a single pile (given some conditions to make the game finite). The solution for this variant is similar to the standard nim game because this extra move will be used by the winning player, and whenever the losing player does it, the winning player can cancel it by throwing away these added stones.

This problem can be modeled as the mentioned variant. Lets color leaf nodes with blue. The parent of a blue node is red and the parent of a red node is blue (that’s why all paths from root to leaves must have the same parity). Blue nodes are our piles while red nodes allow discarding apples or increasing piles.

If the xor value of blue nodes s = 0, then Soliman loses on the initial tree. To keep this state after the swap, Sagheer can:

  • swap any two blue nodes or any two red nodes.

  • swap a blue node with a red node if they have the same number of apples.

If the xor value of blue nodes s ≠ 0, then Sagheer loses on the initial tree. To flip this state after the swap, Sagheer must swap a blue node u with a red node v such that

Complexity: O(n + maxA) where maxA is the maximum value for apples in a single node.

Implementation

You can read more about games from this link

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

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

Very interesting E problem.

can someone give me a good game theory reference in english?

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

I still cannot grasp the problem C. Is not it a standard knapsack DP problem?

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

edit: nvm I see this case is invalid

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

Problem D is too hard to understand. At first sight I think it is something about DAG, which is too difficult for Div. 2. When I finally get the author's idea, there is no time for me to implement :(

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

    I thought it's about finding the number of nodes in all the paths going from x to y in a DAG. Now I'm​ reading the editorial and can't find anything similar :D

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

    I thought the conditions are clear. If you will put a dependency edge from one child to another, then the condition "No child can request a toy while waiting for another" should make it clear that each child has at most one outgoing edge. Thus, it is a tree not a DAG.

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

      Sorry if this sounds rude or harsh. But if there are 4 comments all with positive upvote about how unclear the statement is, then the statement is probably unclear. Furthermore, only 1 yellow of out so many yellow and red solve it out of contest, which suggests most of them misunderstood it. I am not saying what you mention is not in the statement, but what you should take away is that the presentation is not clear, and therefore should do better next time by either putting such case in the sample task, or simply making it easier to understand.

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

I have solved question B, using DP time complexity O(n*m) Solution B

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

Problem B has a dynamic programming solution. Complexity: O(n.m) 27496654

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

I have a somewhat simpler solution of E.

The game described is a variation of staircase nim, in which only odd numbered positions are important for computing the nim-sum. Its different because in staircase nim, the coins(Apples) in the last step, are not removed. But it can be handled by adding one empty node to each leaf-node.

By careful observation, it can be seen that in trees with even parity paths , even-positions are important and in odd-parity paths, odd positions are important.

We can compute the nim-sum of the tree by this, and the rest is similar to editorial. (Blue nodes are important nodes).

Solution

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

Problem B -- How to solve if we remove "he will not go upstairs until all lights in the floor he is standing at are off" condition?

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

    won't that be just travelling salesman problem ?! Solvable using DP with bit mask in O(n * 2^n)

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

      Not exactly, there might be more than n lights and you may enter each floor from any side, so the mask can't be simply the floors, it should be the lights. I think there's an O(n) dp where you make a mask of the ways you need to use (down on left/right, got here from left/right) but it looks tricky (at least without trying to code it and just thinking).

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

    Consider the state (floor, stairs, mustLeft, mustRight):

    • floor indicates the index of the current floor (we will start from ground).

    • stairs is a binary number: 0 for left stairs and 1 for right stairs.

    • mustLeft is a flag. If it is 1, that means we must reach the left stairs by passing through the whole corridor in the current floor, or by moving downstairs later from the upper floors. It is set to 1 if the left stairs of lower floors must be reached to reach some rooms with lights on (this is explained in the next paragraph).

    • mustright is a flag similar to mustLeft but for right stairs.

    Assume we have a state (floor, 0, mustLeft, mustRight). Now, we have two options:

    • pass through the whole corridor to the right stairs and go to state (floor + 1, 1, 0, 0).

    • pass to a certain room r and go back to left stairs and go to state (floor + 1, 0, 0, newMustRight). newMustRight is 1 if and only if there is an unvisited room after r with lights on or mustRight = 1. Let q be the first room after r with lights on. We will hypothetically assume that we are standing at both left and right stairs. So, we will go left -> r -> left and right -> q -> right. And we set mustRight = 1 for the next state to inform it that the right stairs must be reached anyways to take it downstairs to the current floor and our hypothesis becomes valid.

    The dp value of the current state is equal to the dp value of the next state plus:

    • number of moves done in the current floor. In case 1, it is the whole corridor. In case 2, it is the moves done to reach r from left stairs and back  +  moves done to reach room q from right stairs and back.
    • 1 for going upstairs if there is a next floor to go to.
    • 1 if newMustRight = 1, because we will take the stairs later from next floor to current floor.

    And do the same for stairs = 1.

    What remains is handling where we will end this path. This can be handled with an extra flag indicating if we decided where to stop or not. If we are to choose a room to stop at (r or q of a certain floor), we will remove one "back" calculation as we don't have to go back to stairs. One another modification is that mustLeft and mustRight will take three values 0, 1 and 2. The default value is 2, because the stairs, we will take downstairs to reach q needs to be taken again upstairs. Unless it is set this for the end room in which case it will take the value 1.

    The complexity is O(nm).

    It's an interesting question and I hope my solution is correct. Let me know if you need further clarification.

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

I not understand problem D, very complicated description.

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

Can anyone explain dp solution for B . I am trying brute force but there are many small complications so its better to solve via dp . Btw I am a beginner to dp so please try to explain as simply as possible . Thanx

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

In problem D, isn't the graph a disjoint collection of DAG instead of forest of trees. So if it's a DAG then euler tour shouldn't work for correct solution! I mean there could be possible multiple root nodes for any node and so multiple ways to reach that node?

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

For problem D, I think the ans for the case

3 2 4 1 1 1 2 2 3 2 3 1 1 2

is 2? (Child 1 and child 3 cry.) But I ran the implementation code, got answer 0.

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

Why did you color the leaf nodes as blue. What if I colored the leaf nodes with red and took the xor of the blue nodes.

I am new with game theory. Just trying to understand this variation of nim.

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

    This is because you could only take away stuff at the leaf nodes, effectively that means nim is only played at nodes with even distance from the leaf.

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

    I suppose it will be wrong because actions with leafs can't increase number of apples in any other vertex, so they must be treated like nim piles.

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

    Nim game with increases has two moves:

    • remove positive number of stones from one of the piles.

    • add positive number of stones to one of the piles.

    We agreed that this problem has a solution similar to standard nim as proved in the link. The model I described in the editorial is equivalent to this variant: red -> blue is the second move and blue -> red/eat is the first move.

    If took the xor of red nodes instead of blue ones, will this new model still equivalent to nim game with increases? The answer is no, because the move blue -> eat has no equivalent move in the variant (as if you are saying "take non-empty set of apples from nowhere and remove them away").

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

Why does the solution for D print 0 for this input:

3 2 5 1
1 1
1 2
2 1
3 2
3 1
2 2

Shouldn't 2 and 3 cry now?

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

Problem D was a bit puzzling by its rules. And you want to write something like

3 3 4 1
1 1
2 1
2 2
1 2
3 3

To decide that you should make oriented graph and find DSU in it and make something magic later to find if edge x->y can combine different DSUs...

Now I understand why this test is not correct, thanks.

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

Задача B, может быть решена быстрее, чем за экспоненту. Ее можно решить за линию http://mirror.codeforces.com/contest/812/submission/27499584

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

In problem C you said "So, we can apply binary search", how do you come up with this conclusion from what already said before that? In other terms what are the conditions to apply bs.

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

    If you have a monotonically increasing function f(x) and you want to find largest x such that f(x) ≤ k for some given k, then you can use binary search. Let our domain be limited to the range . We will try the midpoint m = (L + R) / 2:

    • If f(m) > k, then all x > m are not valid because the function will yield greater values. So, we set R = m.
    • If f(m) ≤ k, then all x < m are valid because the function will yield smaller values and the inequality still holds. So, we set our answer so far to be m and L = m.

    We go on search till we find the largest value.

    For functions with YES/NO answers, the function must look like:

    • ... YES YES YES NO NO NO NO ...

    • ... NO NO NO YES YES YES ...

    So, when you test a certain value, you will get information about the values before it or the values after it, so you can decrease your search space.

    There is a nice editorial about binary search in this link.

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

My DP for problem B. submission

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

In problem C: Ques didn't mentioned that
Shageer must buy items consecutively. This ruined the problem.

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

In the standard nim game, we xor the values of all piles, and if the xor value is 0, then the first player loses. Otherwise, he has a winning strategy. One variant of the nim game has an extra move that allows players to add positive number of stones to a single pile (given some conditions to make the game finite). The solution for this variant is similar to the standard nim game because this extra move will be used by the winning player, and whenever the losing player does it, the winning player can cancel it by throwing away these added stones.

This problem can be modeled as the mentioned variant. Lets color leaf nodes with blue. The parent of a blue node is red and the parent of a red node is blue (that’s why all paths from root to leaves must have the same parity). Blue nodes are our piles while red nodes allow discarding apples or increasing piles.

If the xor value of blue nodes s = 0, then Soliman loses on the initial tree. To keep this state after the swap, Sagheer can:

swap any two blue nodes or any two red nodes.

swap a blue node with a red node if they have the same number of apples.

If the xor value of blue nodes s ≠ 0, then Sagheer loses on the initial tree. To flip this state after the swap, Sagheer must swap a blue node u with a red node v such that s ^ a[u] ^ a[v] = 0.

Complexity: O(n + maxA) where maxA is the maximum value for apples in a single node.