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

Автор fcspartakm, история, 8 лет назад, перевод, По-русски
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Разбор задач Codeforces Round 452 (Div. 2)
  • Проголосовать: нравится
  • +54
  • Проголосовать: не нравится

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

In problem D (n + (n - 1) - sum) / 2

I think it should be (n + (n - 1) - p) / 2

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

Pardon me for a stupid question.

In Div2B, the 13th test case is the following:

21 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31

and correct output is "YES"

Doesn't this show two consecutive leap years? year 1: 31 28 31 30 31 30 31 31 30 31 30 31, next year: 31 28 31 30 31 ... this should not be possible right?

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

I solved C by just making two set and giving element to the smaller sum set in a reverse sorted order.I don't know why this works though. Can anyone help me with proving this method's correctness or tell where it could fail?

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

    I am trying the same approach and in pretest 5 59998 its failing as i am putting 48950 twice. I am not sure why only that case failing :|

    EDIT: a lot of other cases are also failing. have to do something else,

  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +7 Проголосовать: не нравится
    1. A set of 4 numbers can always be divided into two sets so that their differences is 0. Take n, n-1, n-2, n-3. Set1 contains n, n-3 and Set2 contains n-1, n-2. When dividing a consecutive sequence, you can always start from the last, take a group of 4 consecutive integers, and start dividing like earlier.

    2. If you can't take a group of 4 consecutive integers, the sequences left might be: i) 1, which can be assigned to any set. ii) 1, 2 : where 1 can be assigned to one set and 2 can be assigned the other set. iii) 1, 2, 3: where 1 and 2 has to be assigned to one set and 3 has to be assigned to the other set.

    3. Your algorithm automatically takes care of the first step. When it assigns n to one set, and n-1 to another, then n-2 becomes assigned to the set containing n-1 and n-3 becomes assigned to the set containing n.

    4. Your algorithm also automatically takes care of the 2nd step. A quick thinking will reveal why.

    Hope you understood! :)

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

Can someone please explain solution to problem E. I don't understand the editorial.

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

A much easier solution for C : My solution link... 1. Just take the sum of the numbers= N*(N+1)/2.

  1. If sum is even, then logically you can divide it into two equal halves. Keep giving the largest numbers possible till you form half of the sum.

3.If sum is odd, then logically one group will have one more sum than other group(g2). Let G2 sum be x. Keep giving the largest numbers possible to form x

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

Can someone explain D? Why we are adding those numbers etc..

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

难得有一次cf这么适合Chinese。。。

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

Easier way to solve problem C . The sum of the first N numbers is N * ( N + 1 ) / 2, let's call it tot.

If you have a number X less than or equal tot, it is possible to represent it as a sum of first N numbers. ( Iterate from N to 1, and subtract when possible), call that function represent(X). This is very well known.

Now .. if tot is even, we can obtain a difference of zero which we obtain by represent(tot/2). If tot is odd, we can obtain a difference of one which we obtain by represent(tot/2).

In even case, we get the best possible answer, in the odd case it's easy to see that the best we can obtain is a difference of one, and hence the algorithm is optimal.

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

Another way to solve F is keeping a treap for each character. However, it is overkill in my opinion. But if you have template ready to be copied and pasted, then it should be fastest solution.

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

Существует другое решение задачи D, основанное на бинарном поиске, вот оно 33359972

Пусть нам необходимо посчитать количество различных пар чисел (a, b) из отрезка [1, n], таких что a < b и a + b = sum, где sum — некое заданное число. Найдем подотрезок [left, right] отрезка [1, n], все числа на котором будут иметь такую пару из этого отрезка.

Границы бинпоиска на left должны удовлетворять условию: для low не существует пары, для high существует. Тогда, после того, как будет выполнено условие high - low = 1, ответом для left будет high. Необходимо также проверить left = 1 перед бинпоиском.

Границы бинпоиска на right должны удовлетворять противоположному условию: для low существует пара, а для high- не существует. Тогда, после того, как будет выполнено условие high - low = 1, ответом для right будет low. Необходимо также проверить right = n перед бинпоиском.

Ответом для фиксированной суммы будет длина отрезка пополам: (right - left + 1) / 2

Тогда задача сводится к тому, чтобы сгенерировать наибольшее число, состоящее только из девяток и не превосходящее n. Пусть это число sum вида 9999...999. Затем посчитать количество пар по всем ответам для сумм вида digit·(sum + 1) + sum, где digit — разряд, дописываемый слева от числа sum (от 0 до 8).

Это решение имеет асимптотику O(log(n)) на ответ для одной суммы, а описанный подход может быть применен к другим задачам.

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

In problem D we can use a binary search for counting the number of pairs with a fixed amount on a interval: 33359972

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

Hey anyone got the solution of E by simulating the process using stacks, as i am getting stuck somewhere in this implementation.

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

Hi guys! Can anybody help me with problem F ?! I tried to solve it using a Multimap and a Fenwick Tree for building the initial positions but I'm getting "Wrong answer" on test 7 :(( Maybe I omit something. Thx in advance!

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

I have a doubt in Problem E.

While merging the two nodes, I can see how we are deleting the right and the left element from the second set(called segments), and then inserting the merged set. I don't understand, how can we perform the same operation in the first set(called lens) ?

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

Hey guys! Here are some of my thoughts on problem E this round, using priority queue and linked list! Have a glance! Use of Linked List and Priority Queue in CF Round 452, Problem E

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

In 899F, I didnt understand how would change the current l,r to positions in initial string using segment tree?

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

Hello guys, Happy Coming New Year to everyone! Can somebody help me, why do I keep getting TL7? Thanks in advance. Solution

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

Божественный разбор по D, в частности последний абзац — всё логично и всем сходу понятно.

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

Problem C

Can be solved greedily:

Start with putting N on the left pile and N - 1 on the right one. Then by placing N - 2 on the right and N - 3 on the left we got left - right == 0 and we reduce the problem to the one of the size N - 4.

At some point we end up with one of four possible problem sizes: 0, 1, 2, 3.

In the first case the difference was 0 and it's the best we can do.
In the second case we get 1 which is also the best solution because in than case N = 4k + 1 and the sum = N * (N + 1) / 2 = (4k + 1)(2k) is odd.
In the third case we end up with two numbers: 1 and 2, the best diff of 1 and 2 is 1 again and again it is optimal since N = 4k + 2 and sum = (2k)(4k + 3) is odd.
In the last case we have numbers 1, 2, and 3, we can place them so diff will be 0 which is optimal.

The code

import sys

n = int(sys.stdin.readline())

g = []
ls, rs = 0, 0

for i in xrange(n, 0, -1):
    if ls <= rs:
        g.append(i)
        ls += i
    else:
        rs += i

print abs(ls - rs)
print '{} {}'.format(len(g), ' '.join(map(str, g)))
»
8 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

This tutorial is not attached to the contest (I can see only Announcement link from there).

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

Problem F can also be done using sqrt decomposition.

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

Don't you think the editorials should be more elaborate(eg they can include one example).

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

Can anyone please explain why in problem D we are adding P/2 to the answer when P <= n+1 ?

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

    Because you can take P/2 pairs to form P as sum. Take as example, P = 9 and N+1 something bigger. To form P = 9 as sum of two numbers you can take, 1-8, 2-7, 3-6, 4-5. As you can see if you continue you will take 5-4 which is similar to 4-5. So moving until the middle of P gives you all the pairs and so P/2 occurs.

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

I've been stuck for ages... anyone know what test #33 was on div2E? http://mirror.codeforces.com/contest/899/submission/33345602

My answer is 1 + the correct answer. :(

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

Another solution for F :

Store the positions of every character in an array of Sets (like the tutorial)...

Then after each query we can find the original position which has the Kth order after the previous updates by storing original positions in a Binary Search Tree, such as Treap which I used and delete them from the tree while applying the query. Therefore you can get the range query according to the original positions then you can remove positions from the corresponding set using lower_bound like the Tutorial..

(Sorry about my bad English)

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

in C for 5998 the answer should be 0

bcoz if n is even then we can always keep all the numbers whose %4 gives 1 or 0 in the first group, while others in the second group. This always give us an absolute difference of 0.

plzz correct me if I m wrong?

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

    sorry I found out my mistake, but could anyone provide a proof for C

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

      Link to a submission based on my proof:

      36503825

      Let's consider first when n is even. Suppose that our solution is A = {2, 4, 6, ..., n} and B = {1, 3, 5, .., n - 1}. We see that

      , and therefore the difference between A and B is exactly . We can reduce this difference to zero or one by swapping elements between A and B.

      If we swap two elements x and x - 1 from A and B respectively we see that we have decreased the absolute difference by exactly two and, in general, we will only be able to reduce the difference in even amounts. Given that the difference is we need this amount to be even in order to be able to reduce it to zero or, in other words, n must be a multiple of 4. If it is not, we will only be able to reduce it to 1. After performing swaps the difference will reach one of these two values.

      If n is odd we have A = {2, 4, ..., n - 1}, and B = {1, 3, 5, ..., n}

      , and these two solutions can be transformed again to vectors that have difference zero or one by swapping x with x + 1 that belong to A and B respectively. The same arguments about being a multiple of 4 hold for the floor of

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

Автокомментарий: текст был обновлен пользователем fcspartakm (предыдущая версия, новая версия, сравнить).

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

Жесть я даун

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

I think Binary Search tag is missing in Problem F.

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

Problem E is tagged with flows , i wonder how can we solve this using max flow ?

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

In problem A, we can solve it easier. We can calculate the number of group can be made of 2(s) and 1(s) = min(1, 2). And then plus with the remaining groups by 1s. We can get a sum like this = min(numberof1, numberof2) + (numberof1 — min(numberof1, numberof2))/3