giorgosgiapis's blog

By giorgosgiapis, 6 years ago, In English

Fourth round of COCI 2017/2018 season will take place this Saturday at 14:00 UTC.
You can find the full schedule for this season here and register for the contest here.

Let's discuss the problems here after the contest.

  • Vote: I like it
  • +31
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Remainder: Contest starts in less than 20 mins.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve the third problem (Automobil) for N,M <= 1'000'000?

  • »
    »
    6 years ago, # ^ |
    Rev. 4   Vote: I like it +13 Vote: I do not like it

    Let's calculate the answer first without any operations. Then calculate array R and C, Ri denotes the product of all query's for the ith row, similarly for columns Ci denotes the product of the ith column (I created them lazily with std: : map). Then update the answer according to these, first by rows then by columns. Then update the intersection of the queried rows and columns manually (for example if in one row we multiplied by a and then in one column we multiplied by b then at their intersection we considered this value a + b times but should've considered it ab times), since K is pretty small there won't be many (atmost ), so this way we have a O(K2) algorithm.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      Ah, I think I got too caught up thinking of how to handle the intersections, should've realised that there would only be ~K2 of them. Thanks!

  • »
    »
    6 years ago, # ^ |
    Rev. 4   Vote: I like it +7 Vote: I do not like it

    I solve this problem as follows:

    1 Add all values that don't have modifications (blank areas on the image), this step has a time complexity of numbers of queries executed over rows multiply by numbers of queries performed over columns.

    2 For each row found in the queries find their corresponding sum and exclude every element that is affected by a column query (red areas on the image). After that add to your answer, this sum multiply by every Y found on the queries that affected the current row.

    3 Same as the previous step but by columns.

    4 Finally, add to your answer the cells marked in red on the image above.

    Time complexity O( K*K ). Memory complexity O( N ).

    I hope that this will be useful for you. If you need I can share you my code.

  • »
    »
    6 years ago, # ^ |
    Rev. 6   Vote: I like it 0 Vote: I do not like it

    My approach was a little diferent that mavd09 and mraron

    The problem is to solve the next summation for each column:

    where ki = multiplication of times row i was updated and vi is the value that is in i - th row of each column. C is the multiplication of times that column was updated. But the difference between each column for values (vi) is 1 between consecutive columns (I mean vji + 1 = vj + 1i with j being the j - th column ). So we can rewrite the solution as:

    Now if you look that carefully, the terms and are always same for each j. So you only need to calculate them once. Call the first sumViKi and second sumKi

    Now our answer would be

    It can be reduced a little bit. But that's enough for me. So now you only need to precalculate both terms sumViKi and sumKi and the rest is just iterate over columns.

    Complexity( )

    Code

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve D (large) and E?

  • »
    »
    6 years ago, # ^ |
    Rev. 4   Vote: I like it +3 Vote: I do not like it

    D:

    dpi, j =  can i-th player win if (i - 1)-st player said number j. dpi, j = true if

    1. ai ≠ ai + 1 and dpi + 1, j + 1... j + k has at least one false.
    2. ai = ai + 1 and dpi + 1, j + 1... j + k has at least one true.

    Write array a many times and use some prefix/suffix sums to find that "at least one" thing in instead of .

    Total complexity: .

    Code

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Thank you. I wanted to do the same thing but defined state a little different so I couldn't do bottom up DP.

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I also did this, but will it definitely pass? There are NM dp states, and K processing at each state, giving us O(NMK), or about 1.25 * 1011 operations. Am I missing something?

      Edit: I was mistaken, should've paid more attention to the explanation. Thanks for the clarification.

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        No, process each state is O(1) cause you only need to track the last winning and last losing state.

      • »
        »
        »
        »
        6 years ago, # ^ |
        Rev. 4   Vote: I like it 0 Vote: I do not like it

        if you do transitions in . Check the code.

  • »
    »
    6 years ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it

    I think is like dp[i][j] = is turn of i-th animal and the number the previous animal said is j. Now I need to look for a winning or losing state in (i+1)-th animal depending on what kind of animal is animal i and i+1. The first idea of course is to iterate through [j+1 — j+k].

    But if you see carefully, the state of [i][j] always is looking in [i+1]. So you only need to know what are the position of the last winning and losing state in [i+1].

    Then check if last winning <= j+k or last losing <= j+k depending on the case if i and i+1 are same animal, or not.

    AC code

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve 140, 160?

  • »
    »
    6 years ago, # ^ |
    Rev. 3   Vote: I like it +13 Vote: I do not like it
    E
    F
    • »
      »
      »
      6 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      How to find maximum/minimum of unimodal function if there are more xs with same value of F(x)?

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Nothing changes. It attains its minimum when X = (median of Y)

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          I meant for an arbitrary unimodal function (I know it is so for this one).

          • »
            »
            »
            »
            »
            »
            6 years ago, # ^ |
            Rev. 2   Vote: I like it +3 Vote: I do not like it

            If you are saying about staircase-like functions, then it don't happen as the function is convex. (I regret "unimodal" is not a good word to explain that function)

            And you can't find a maximum in unimodal function. Let f(x) = (x == 12345678). Then you should find 12345678 somehow but it's impossible

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For E/140 I sort of did a 2-Dimensional Ternary Search. Not entirely sure both are convex functions. Did anyone else do something like this? Or is there another way to do it>

  • »
    »
    6 years ago, # ^ |
    Rev. 3   Vote: I like it +3 Vote: I do not like it

    I doubt that will work. But for fixed i ternary search works.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      I generated some random tests in python to check if choosing i is convex and you're right of course, it isn't.

»
6 years ago, # |
  Vote: I like it +37 Vote: I do not like it

results?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve F?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    My F/CESTE was Dijkstra's algorithm with vertices (v, time), that the time must be increasing and cost decreasing on each v. It was too slow for O(NM * MaxTime) vertices in the worst case, but luckily passed.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    We know that there exists x (0 <= x <= 1) for which the shortest path with weights time * x + cost * (1 — x) gives the optimal answer for time * cost.

    Great, then let's find shortest paths with x = 0.

    Then, let’s find minimum x < x’ <= 1 such that there is an edge (u, v) such that with weights time * x’ + cost * (1 — x’) Path ((1->u) (optimal for x) -> (u, v)) is better than (1->v) (optimal for x).

    And rebuild shortest paths with x = x’.

    We will do this process while we can find such x’.

    (We can find this x’, just solve some equation a * x + b * (1 — x) < c * x + d * (1 — x) for each edge).

    Code: https://pastebin.com/NXPX7Kb5

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 3   Vote: I like it -8 Vote: I do not like it

      I think I might have understood what you just said. However, I'm far from being able to prove any of the facts you used. First, let me make sure I understood it the right way: for a fixed x, you compute the optimal path from 1 to all the other vertices, where the weight of a path is defined as time * x + cost * (1 — x) (basically x is a constant that helps in defining the weight of an edge). Now, my problem is: how do you know that there exists a proper choice of x so that the optimal path that you get in that case is also optimal when considering the weight as time * cost? It makes sense intuitively but I can't think of any attempt to prove it. Then, you also count on the fact that you can somehow make leaps that take you to a better answer while increasing the x. This seems even harder to prove, so if you have any idea how to prove any of these facts, I'd be grateful to see them. Aand my last question would be: this is a really strange approach, maybe even more interesting than Alien's trick (which I heard is actually Lagrange multiplier), so I was wondering whether you just came up with it, or you saw something somewhat similar to it in some other problem. If so, could you share that problem with us? Thanks in advance!

      To the dowvnoters: Really?:)) It's so funny to see the -7 when my comment also explains part of the solution that some people might not have understood and definitely helped at least those. CF community doesn't cease to impress me