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

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

Добро пожаловать на 2016-2017 CT S03E01: Codeforces Trainings Season 3 Episode 1 - 2010 Benelux Algorithm Programming Contest (BAPC 10). Продолжительность тренировки — 4 часа 30 минут. Тренировка открыта как для команд, так и для индивидуальных участников. После ее окончания вы можете дорешивать задачи тренировки или поучаствовать в ней виртуально, если не смогли принять участие одновременно со всеми. Пожалуйста, участвуйте в тренировке честно.

Так как это тренировка, то возможно набор задач будет расширен, если для значительного количества участников он окажется простым.

Условия задач будут на английском языке, ведь мы же готовимся к ACM-ICPC!

Удачи!

  • Проголосовать: нравится
  • +119
  • Проголосовать: не нравится

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

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

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

How can I find the time limit for each problem?

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

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

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

А можно посмотреть на авторское/какое-нибудь AC решение по задаче H ?

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

    Если все еще актуально — в тренерском режиме можно посмотреть

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

    Тут наверное что то можно найти, если не ошибаюсь.

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

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

    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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

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

        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 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

        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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

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

        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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится
        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 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

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

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

      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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

    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

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

can someone explain how to solve B??

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

    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 лет назад, # ^ |
      Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

      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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Plz explain how you got both the j ranges !!

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

      --

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

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

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

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please tell me how to solve problem J?

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

    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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

the image is something so great :D

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

Help me please. How to solve problem I?

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

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

What about the 9th case in the last problem?

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

Как решать Е(геом)?

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

    Бинпоиск по ответу. Как проверять:
    1. Сжать поле с 0, 0, w, h до r, r, w — r, h — r
    2. Увеличить радиус всех существующих окружностей на r
    3. Можно вставить окружность с радиусом r если есть точка, которую не покрывает ни одна из окружностей, и она находится в прямоугольнике(r, r, w — r, h — r -- координаты диагонали прямоугольника). Это по-идее можно проверять сканлайном, но я сделал функцию f(x1, y1, x2, y2) — находится ли хотя бы одна точка прямоугольника (x1, y1, x2, y2) вне всех окружностей. Если ни одна из точек не находится то я делю этот прямоугольник на 4 и ищу рекурсивно. Если в какой-то момент прямоугольник полностью лежит в какой-нибудь окружности то я вылетаю.

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

Editorials, solutions and everything is here!

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

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

    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 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

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

good luck to all

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

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

could u plz tell me problem G's data?

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

Problem A.

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

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

Can someone please tell me how to solve problem E?

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

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

How to solve problem K?

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

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

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

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

    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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

What is idleness limit exceeded?