Комментарии

I used this strategy.For example take numbers 1,2 , ... ,n. Write them in the next order( let n % 2 = 0): 1, n, 2, n-1, 3, n-2, ... ,n/2, n/2+1. You can see 2 last terms are adjacent, just swap 1st and last terms: n/2+1, n, 2, n-1,..., n/2, 1. The main disadvantage is that you need the diff between any 2 numbers more than 1 ,so : n/2 — 1 > 1 so n > 4. Now we know how algorithm works for 1 row. For > 1 rows, for example:

1 2 3 4 5

6 7 8 9 10

Performing operation we can get:

3 5 2 4 1

8 10 7 9 6 . You can see the difference in each vertical pair is m ( so they`re adjacent ), so we just write each even row in reverse order:

3 5 2 4 1

6 9 7 10 8. If m is odd :for each even row, swap first and middle numbers so the final answer is:

3 5 2 4 1

7 9 6 10 8. The same idea can be used if m < 5 and n >= 5. This algorithm works if and only if max(n,m) >= 5.

P.S. First step: For even n you can swap not n/2 + 1 ,1, but n/2 and 1. Then you`ll get n/2 + 1 — 1 > 1 => n/2 > 1, n > 2. So, algorithm works if max(n,m) >= 4. Other cases can be handled easily.

Thank you !

Is is possible to use strategy like this ? Do tree traversal root-left-right. If child's color different from parent's then increase answer.

На adamant → Codeforces Round #453 (Div. 1 & Div. 2), 9 лет назад
-13

How many problems in the contest?

На fcspartakm → Codeforces Round #452 (Div. 2), 9 лет назад
0

Check the sum = n*(n + 1) / 2. If it is even, the answer is 0 (because you can divide it in 2 equal groups), else the answer is 1. Now, you have to find the 1st group: If n is even, then just write 1 4 5 8 9 12 until it is lower than n. Why does it work? (first number is from 1st group ) 1 2 (first group is second group + 1) 4 3 (first group = second group) 5 6 (first group is second group + 1) 8 7 (first group = second group) and so on if n is odd, use the same idea but from the end For example n = 7, then 7 6 (diff is 1) 4 5 (diff is 0) 3 2 (diff is 1) 1 (diff is 0)