MikeMirzayanov's blog

By MikeMirzayanov, history, 8 years ago, In English

Welcome to 2016-2017 CT S03E01: Codeforces Trainings Season 3 Episode 1 - 2010 Benelux Algorithm Programming Contest (BAPC 10). The training duration is 4.30 hours. It is opened for teams as well as for individual participants. After the end you may use the practice mode to complete problem solving. Also it will be availible as a virtual contest for whose of you who can't take part today. Please, do not cheat. Only fair play!

It is possible that the problems will be too easy for some participants, it is possible that we will add some problems.

Good luck!

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

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

idleness limit excedded on dynamic ip/op problem F !! any flushing needs to be done for java ??

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

How can I find the time limit for each problem?

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

Can someone please explain the solution for the first test case for the "Collatz" problem?

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

    Only after the contest.

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

    for 12 backward ropes to be cut are coming from 24,22,...,14. forward ropes to be cut are originating from 5,7,...,11 so total ropes to be cut are 10.

»
8 years ago, # |
  Vote: I like it +13 Vote: I do not like it

In the Gene Shuffle problem shouldn't the output for the first seq pair be: 2-2 4-7 8-8 9-10 instead of: 1-3 4-7 8-8 9-10 ? seqs: [1 2 3 6 4 7 5 8 9 10] [3 2 1 4 5 6 7 8 10 9] 1-3 cannot be a minimal common segment as it contains 2-2

Please advice, Thanks,

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

    Actually you have misunderstood the problem statement ! By minimal segment it means that i.....j can not be further divided into i...k and k....j

    In your eg 1-3 has been turned into 2-2 but 1 and 3 have not been considered. But the problem statement expects CONTINUOUS segments of similar elements with different permutations.

    So final answer shoould always be of form 1...k1,k2....k3,k...k,kn.....n.

    A more clear problem statement would have helped

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

Can someone please tell me how to solve problem C? I think it is DP?

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

    First calculate mm[i] for each subset i of 9 cells where mm[i] denotes the number of perfect matching for this subset. This can be calculated via DP/brute-force easily.
    then let dp[i][j] denote the height i and topmost floor has subset j matched to i+1 then answer for any N = dp[N][0]. All values can be calculated in O(3^9*N) as follows:

    dp[0][0] = 1;
      rep(i, 1, 5002) {
        rep(j, 0, 511) { //previous subset was j.
          int x = 511 ^ j;
          for(int k = x; ; k = (k - 1)&x) {//for a subset k of the remaining
            dp[i][k] += mm[511 ^ (j | k)] * dp[i - 1][j]; dp[i][k] %= mod; //number of matching of whole - j - k
            if(k == 0) break;
          }
        }
      }
    
    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry... What exactly is the meaning of mm[i]? What is the value of mm[511]?

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

        mm[i] denotes that if for the grid of 3*3(cells renumbered from 0..8) i denotes the subset which is non-empty then in how many ways they can be perfectly matched:
        mm[511] = 0 since 9 is odd: no perfect matching
        mm[510] = 4 : if you work on paper with only one corner cell empty there are 4 ways in which you can match all non-empty cells with their neighbours.

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

      Let me check if I got the meaning of the DP function correctly...

      f(i,j) = number of matchings considering floors 1 to i such that floor i's rooms in subset j are matched to their equals in floor i+1

      Is that it?

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

        Yes. So when j=0, you get answer for N as dp[N][0].

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

      could you tell me the complexity of finding all subsets of x? THX

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

      Can you post your complete soln ?? How did u calculate mm ?

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

        Here is my complete solution: http://ideone.com/UT395f — though I calculated mm tediously using DP(I knew how to calculate number of perfect matchings using DP), this can be calculated using brute-force as well since there are only 9 cells.

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

      Ain't that O(N*2^9*2^9)? Because x can have as many as 2^9 subsets if x=511

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

        It will be for each subset sum of sizes of subset since the for(k=x..) loop runs only on the subsets of a set. And sum(each subset size of a set of size k) = sum((k choose i)*2^i) = 3^k.
        Hence (3^9)*N.

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

      I'm confused. It's obvious that

      int x = 511 ^ j;
      

      equal to

      int x = j;
      

      Also this 511 ^ (j | k) = j. Could you confirm or simplify this?

      • »
        »
        »
        »
        8 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        int x = 511 ^ j
        

        is not equal to

        int x = j.
        

        It means that we make a 'x' mask of elements which are not in 'j'.

        511 ^ (j | k)  
        

        This we can replace with

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

          511 to binary equal 111111111 and j always less then 512, therefore 511^j = j. Am I right?

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

      It took me a good amount of time to understand what the for loop for(int k = x; ; k = (k — 1)&x) does. Well done actually! sumit16 Does this algorithm has a well known name (for generating combinations of set bits of the set bits of an integer)?

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

    I came up with a different approach from sumit16's DP. My DP also has O(N39) complexity and works as follows.

    Let f(i, m1, m2) be the number of pairings considering floors 1 to i such that floor i's rooms in bitmask m1 still need to be paired and rooms in bitmask m2 are currently paired with the floor below, which is i - 1.

    Then the answer is f(N, 511, 0): consider floors 1 to N, all 9 rooms in floor N still must be paired and no room in floor i is currently paired with the floor below, which is N - 1.

    I'll use set notation for bitmasks when convenient.

    Base cases:

    f(1, 0, 0) = 1

    $latexbug$

    General transition (i ≥ 1, m1 > 0):

    Let be any unpaired room (the smallest will do fine). Then:

    $morelatexbug$

    where V(j) denotes the set of neighbour rooms of j which are present in m1.

    You're probably thinking: "this is a 4-state per element bitmask, so it runs in O(N49)!". Yes! But you can supress one of the 4 states. If , then j is already paired and cannot also be in m1. So you can translate a tuple (i, m1, m2) to an array indexing [i][x1][x2]...[x9], where xj = 2 if , xj = 1 if and , and xj = 0 otherwise. And you should store 2-byte integers, so MLE won't be a problem.

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

    Similiar DP approach, but (maybe) faster:

    dp[layer][level][now_consider]: The first two dimension is the same as the dp mentioned above, while the third dimension is the cell we are considering. For a particular cell, we can either

    1. ignore it (and match with the upper level one)

    2. pair with the cell on the right

    3. pair with the cell on the bottom

    (To avoid duplicate, we don't consider pairing with cell on top or left).

    The transitions are: if k == the bottom-right cell, we cannot pair it anyway, therefore we consider next level, i.e. (i + 1, j, 1) += (i, j xor 511, k) else, (i, j, k + 1) += (i, j, k)

    pair with right: (i, j + bit(k) + bit(k+1), k + 1) += (i, j, k) if k is not the rightmost cells and the bitset in j allows

    pair with right: (i, j + bit(k) + bit(k+3), k + 1) += (i, j, k) if k is not the bottommost cells and the bitset in j allows

    As the transition is now O(1), the overall complexity is O(N * 2K * K), where $K$=9 is the number of cells

    code: http://codeforces.com/gym/101078/submission/20441012

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

      I have done something quite similar. But, I am getting WA if I consider pairin other than (right and bottom) ,such as(left,top).

      Here's my code which uses (right and bottom) pairing: http://codeforces.com/gym/101078/submission/32757763

      But it gives WA when I change the pairing to (left and top). Can you please explain this

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

        You are not supposed to leave any cell empty, but let's say you start with the upper-left cell, then you cannot pair it with either left or top, which makes 0 possible solution afterwards.

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

can someone explain how to solve B??

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

    the normal DP solution works: dp[i] = min time for first i songs:
    dp[i] = dp[j]+(sum(j+1..i)>m?(sum(S_j+1..S_i)-m)*a:(m-sum(S_j+1..S_i))*b)

    j varies from i-60*m..i by the condition that atleast one second for each song — this will give TLE
    j varies from i-2*m..i — AC cause the min point will occur <=2*m.
    O(n*m)

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

      Dynamic Programming.

      Let's define a cost function for which a block has to handle T seconds of songs. Since the penalty is the same for all songs, we should only worry about the total time we are trying to fit in each block. If you are trying to fit T seconds in a block, then depending on whether you need to cut or waste time, you should add the penalty.

      Each block can fit a maximum of 60 * M songs. So we should only attempt filling at most these songs.( So you can assign each song a second).

      For each block try giving j songs to earlier blocks, and fill i — j for the current one. ( Think of it as a standard N^2 DP). Obviously you should only loop till j < (60*M) so you can satisfy the above condition. This is too much however, the optimal point will always occur earlier, there are many tricks to do this, you can just plug in a value that fits the time limit, or as long as the sum of items is <= M. ( or till 2 * M).

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

      Plz explain how you got both the j ranges !!

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

      --

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

      Could you please explain how do you limit the range to i - 2 * m

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

I think the tests are weak in problem A. I got it accepted, but my solution get TLE with test

1
100000
1 3 4 5 6 ... 100000 2
2 1 3 4 5 ... 99999 100000
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please tell me how to solve problem J?

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

    make a bipartite graph with H vertices on left, V on right. For each horizontal word i that conflicts with a vertical word j, edge between i-j. Answer = H + V — bipartite_matching(graph) cause min_cut is the minimum number of edges needed to be cut :D

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

the image is something so great :D

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

Help me please. How to solve problem I?

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

    Use a doubly linked list and simulate the operations(list in c++).

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

      Hey,, can u please check my code ,, i used list in c++ ,, n still my solution got TLE on test 2

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

        Edit: you are doing some things differently which can be done simpler, see my code below. Also, maybe use fast IO.

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

The announcement was posted 6 hours ago, and the contest is already finished! What happened to making announcements at least one day in advance?

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

What about the 9th case in the last problem?

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

    In this test case your solution does not give the minimum penalty.

»
8 years ago, # |
  Vote: I like it +30 Vote: I do not like it

Editorials, solutions and everything is here!

»
8 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Can anyone find whats wrong with my MAze Recognition solution ?? I just did dfs using the dynamic inputs to find final room . If no moves possible it comes back to previous room . Used stack to store reverse moves and kept updating ans if goal room found. http://codeforces.com/gym/101078/submission/20451114

EDIt: GOT it nvm

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

Can someone explain why my solution for A will get TLE on Test Case #2? I think it is still O(N)...

http://codeforces.com/gym/101078/submission/20452670

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

Can anyone explain their detailed approach to C? I can't understand the editorial.

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

    Consider mm[1<<9] array which stores the no. of correct pairing possible

    mm[0] = 1;

    for i=1 to (1<<9)-1 1. Choose a filled room say k ( whose bit is set to 1 in i ) 2. Its pairing will be above/below/left/right 3.mm[i] = mm[i-k-left] + mm[i-k-right] + mm[i-k-up] + mm[i-k-below];

    Coming to dp part refer the dp soln in comment : http://codeforces.com/blog/entry/46993?#comment-313759

    let dp[i][j] be no. of correct pairings such that floor 1 to i have been filled and set(j) rooms of floor i are to be paired with corresponding set(j) rooms of floor i+1

    in the dp soln in comment, i stands for curr floor, j for set of rooms from i-1 floor which have to be paired with curr floor . Also see that the set(j) rooms have to be compulsarilty paired with set(j) rooms of curr floor. From the remaining (x in soln) 511 — set(j) rooms we have two choices : 1. Pair them on the same floor 2. Or else pair them with upper floor.

    So k stands for set of rooms to be paired with upper floor. Hence dp[i][k] = dp[i-1][j] * mm[rooms to be paired on curr floor] = dp[i-1][j] * mm[511 — j — k ) since j and k rooms dont pair on curr floor.

    Also pairings of curr floor have been calculated in mm. Note that k will always be a subset of 511 — j. Also final ans will be dp[N][0]

»
8 years ago, # |
  Vote: I like it +5 Vote: I do not like it

How to solve K? Is that related to LP or flow? (Well flow can be done via LP anyway)

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

good luck to all

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

Hi I think my code 20456300 for problem A is wrong but I got accept with this code

here is a test case that I think my code answers wrong:

1

1 2 3 4 5 6 7 8 9 10

10 9 8 4 5 6 7 1 2 3

for this test case my code answers 1-10 but I think answer is 4-6 am I right?

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

could u plz tell me problem G's data?

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

Problem A.

Before add "ios::sync_with_stdio(0)" I got TLE, after then I got AC instead :(

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

Can someone please tell me how to solve problem E?

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

Please Someone say the output of this input for the problem B

1

29 15

3 100

1 1 1 1 1 1 1 1 1 1 1 1 1 1 20 1 1 1 1 1 1 1 1 1 1 1 1 1 1

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

    99

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

      But Problem Says "At least one second of every single must be played" . Please Explain How 99 ?

      Edit : Got it :)

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

How to solve problem K?

I tried with simplex but it throws Memory Limit Exceeded, any other idea?

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

can anyone give me some test cases for 'L' problem... i am stuck at test case 9...

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

    Did you try to generate random test cases and use a model solution to check if your solution is printing right answer? It is usually in off, except for border cases.

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

iam trying to solve C with do but time complexity is 2^9 * 2^9 * 5000 this is done before the testcases and then every test at o(1) can anyone help me to reduce the time this is my code : Your text to link here...

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

What is idleness limit exceeded?