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

Автор Errichto, 8 лет назад, По-английски

Hi. I'm back from USA!

I will do a new lecture tomorrow (Thursday) at 2pm CEST on my Youtube channel: https://www.youtube.com/watch?v=7hFWrKa6yRM. Watch me live (and ask questions), or just watch the video later. This will be part 1, and I will do part 2 in a few days (maybe Tuesday).

UPDATE — part 2 is coming on Thursday, same time. Link: https://www.youtube.com/watch?v=gXxu-Cr4b4c.

There are no prerequisites this time. I recommend reading the materials below in advance, and trying to solve problems 1 and 5. If you are strong, just read problems and see if you can't solve something (maybe problem 8?).

Technique "Exchange Arguments"

If we're given n items and we should choose some of them and choose their order, we should sort them with some strange/tricky comparator, and then do O(N2) dynamic programming. The dp should be dp[pref][used] — best possible result (balance) if we chose used items so far (in the prefix pref).

"Strange/tricky comparator" checks which of the two elements should be earlier, usually just solving the problem for N = 2. Alternatively: given two adjacent elements, when is it good to swap them?

Bad comparators

Not every comparator is valid, e.g. return A.first < B.second is not. A comparator is fine if it defines the order.

Warning

Not all problems use the described technique. I think it's a good lesson to see some similar problems and understand when it's possible to use some algo/technique.

Problems

  1. Boxes (link to Polish judge) There are n (n ≤ 2000) boxes, i-th with weight wi and strength/durability si. You can choose some subset of boxes and rearrange them in any order you like. Find the maximum possible number of boxes in a tower where the strength of a box must be not smaller than the sum of weights of boxes above.
    Bonus: n ≤ 105.

  2. Hero (Potyczki 2014) Bitor has H health points and he must defeat n (n ≤ 105) monsters. The i-th monster deals di damage, but after death it drops a potion that restores ai hp (hp can be above the initial value). Can Bitor choose the order of fights in such a way that his hp never drops below 0? Print "NO" or "YES" with the order of monsters.
    Bonus: n ≤ 2000 and maximize the number of defeated monsters.

  3. Farmcraft (POI) You are given a rooted tree with n (n ≤ 105) vertices. You are in the root. Moving along an edge takes 1 minute. You want to visit all vertices and get back to the root as fast as possible, what takes exactly 2·(n - 1) minutes. When we visit vertex v for the first time, the countdown is started there and after av minutes that vertex will be "ready". We want to minimize the moment when all vertices are ready. When will it happen?

  4. Pretty Good Proportion (GCJ 2015 Finals) You are given a binary string of length n (n ≤ 105) and a real value r (0 ≤ r ≤ 1). Find a substring where the ratio of ones is as close to r as possible.

  5. Different taste There are n (n ≤ 105) cookies. The i-th of them gives you ai points if you have it, and bi to your opponent if he has it. Players take a cookie in turns, you start.
    Each player maximizes the difference: his_score — opponent_score.
    What will be your and opponent's scores if you both play optimally?

  6. BearEats (TCO 2015, 3B) Same problem, but your opponent is greedy and he always eats a cookie with the biggest value bi.
    Bonus: https://www.hackerearth.com/challenge/competitive/algorithms-india-hacks-2016/approximate/h-bear-and-cookies-2/.

  7. Coins (Polish Junior Olympiad) There are n (n ≤ 105) stacks of coins, each with two coins. You are given the value of each of 2·n coins (positive values).
    You are also given an integer k (k ≤ 2·n). What is the maximum possible value of k coins taken from the top? (If you want to take the bottom of two coins, you must also take the top one.)

  8. Parentheses You are given a sequence of strings, each consisting of characters '(' and ')' only. The total length doesn't exceed 105.
    You can reorder the strings and remove some characters. The remaining concatenated string must be a balanced parentheses string (TODO, how is this called?) correct bracket sequence. What is its maximum possible length?

  9. Concatenation You are given a sequence of n (n ≤ 105) numbers (ti ≤ 109). Order them in such a way that their concatenation is maximized.
    Bonus: SumCards from TCO 2018, Poland round.

See my Youtube channel (link) for more videos on algos and CP, and my Facebook page https://fb.com/errichto for some shorter posts, news, problems.

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

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

Isn't it called "exchange argument"?

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

I can add some problems:
100113K - The Merry Student Life During the Term...
101149B - No Time for Dragons
100971I - Deadline — harder than two previous
100026J - Annihilate the Beetles — one more

LOL, there are Russian names in the English version of the site. Looks like a CF bug. Problems have English statements, don't worry.

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

Is there a judge where we can submit the problems?

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

If you want to submit problems from points 1 and 2 to archive with English statements, here they are:

  1. CODEFESTIVAL 2017 D

  2. ICPC Finals 2016 L

Btw, does anybody have a link to English version of the bonus problem for point 1 (maximize the number of items, n ≤ 105)? I know only one such problem, with statements in Russian only: 100908H - Пицца для вечеринки.

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

I can add this problem also 277D.

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

Is it delayed by an hour?

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

Altruistic Amphibians.

Escape. (Problem 2 on a tree.)

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

I think this problem from NWERC 2017 uses this technique: Installing Apps

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

2018-2019 ICPC, NEERC, Northern Eurasia Finals rated or not?

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

please tell me an effective way to obtain a rating

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

Could you tell if there is a link to submit task 7. And the other tasks (except 1,2 and 4, it is not very hard to find them)

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

When is the next stream scheduled? Can't wait to see it!

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

Part 2 starts in four minutes.

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

I am getting wrong answer in 3rd problem. Can anyone help in finding mistake? [Question link] [Code link]

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

A big fan of your lectures. Thanks for the lectures with such high quality! BTW, when's the next lecture scheduled?

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

Okay, I solved the first problem by first sorting the boxes according to the sum of their weight and strength (wi + si), and then applying 2D dp. It got AC, but I'm still not sure as why this sorting technique works. The only thing I have in my mind is the greedy fact that the boxes having more strength and more weight should be placed at the bottom. But this seems greedy to me. Can someone provide some kind of proof?

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

    If there's a solution with bad order, you can take two consecutive boxes of bad order and swap them.

    • »
      »
      »
      7 лет назад, скрыть # ^ |
      ← Rev. 3  
      Проголосовать: нравится +10 Проголосовать: не нравится

      Thanks! I got it. :)

      For someone who is still curious, I hope this is useful:

      The optimal approach is to sort the boxes according to the sum of their weight and strength (Boxes having smaller value of $$$S_i + W_i$$$ will be placed on top of other boxes having greater value of $$$S_i + W_i$$$. We'll call this order as good order), and then use the approach similar to knapsack dp.

      Now the question is that whether it is possible to obtain a better answer by not ordering the boxes in good order.

      Suppose a better answer exists in which the boxes are not ordered in good order. Then at least two boxes $$$A$$$ and $$$B$$$ will exist in this solution such that $$$B$$$ is placed just on top of $$$A$$$, and they are in bad order (i.e., $$$S_b + W_b \gt S_a + W_a$$$). Now we'll prove that it is possible to swap these two boxes so that they are in good order and the answer still remains the same. Suppose the sum of weights of all the boxes placed on top of $$$B$$$ is $$$x$$$ (it is okay to not consider the boxes which are below $$$A$$$). Then clearly, $$$S_b \ge x$$$ and $$$S_a \ge W_b + x$$$. Now adding $$$W_a$$$ on both sides in the second equation gives $$$S_a + W_a \ge W_b + x + W_a$$$. Combining this with the above equation gives $$$S_b + W_b \gt S_a + W_a \ge W_b + x + W_a$$$, which gives $$$S_b + W_b \gt W_b + x + W_a$$$, which in turn gives $$$S_b \gt x + W_a$$$. Thus we proved that it is possible to swap boxes $$$A$$$ and $$$B$$$ so that $$$A$$$ is placed just on top of $$$B$$$ so that they are in good order and the answer still remains the same.

      Conclusion: The optimal answer that we can get by sorting the boxes in good order, is indeed the optimal answer overall.

      P.S. : Let me know if I'm still missing something.

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

I tried to solve the problem 6 (bonus) but didn't manage to come up with the idea once the sorting is done.

I was reading solutions to the problem and found tourist's code (must be logged in to be able to see it) but I still don't get it. Any idea on what he did or hint to solve the problem?

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

    I don't remember the solution perfectly but I think you need these two things:

    1) When moving from one group of cookies (i.e. those with the same value b[i]), it's only important how many moves your opponent will still spend in previous group(s). You need dp for that. It's $$$last[k]$$$ in tourist's code, I think.

    2) Then, think about what happens even if there is only one group of cookies. You try taking them from biggest a[i] and there is some probability that it's already eaten. What is that probability?

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

I cannot quite understand why exactly do we need to simplify the equations for comparator functions. Like in 2nd problem, is it wrong if we wrote comparator function as:

return (d1+max(0,d2-a1))<(d2+max(0,d1-a2));

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

Problem 5. Is my solution correct?

Sort the cookies by $$$ a_i - b_i $$$ , lets call it $$$ c_i $$$ ( and $$$ c_i \leq c_{i+1} $$$ ), then if $$$ x = c_n - c_{n-1} + c_{ n-2 } - c_{ n-3 } + .... $$$ then my score is $$$x$$$ and my opponent's is $$$-x$$$?

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

This is very similar to problem 2. Complete The Projects (Hard Version)

EDIT : This problem uses the same ideas as problem 1.

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

Where I can submit these problems solution?

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

I was trying to submit a solution for the first problem here, Link to Polish Judge, as Errichto gave the link in the first problem. But it seems that no matter what I try, the verdict is RTE and it says Violation of memory protection. Syscall 383 () was called. If I submit an empty file, I get WA, but if I submit a file containing a single scanf and a single printf and nothing else, it still gets a RTE. Is there a particular file we are supposed to read from in this judge?