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

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

988A - Разнообразная команда

Разбор
Решение (Vovuh)

988B - Подстрочная сортировка

Разбор
Решение (Vovuh)

988C - Одинаковые суммы

Разбор
Решение (Vovuh)

988D - Точки и степени двойки

Разбор
Решение (Vovuh)

988E - Делимость на 25

Разбор
Решение (Vovuh)

988F - Дожди и зонтики

Разбор
Решение (Vovuh)
Решение (step_by_step)
  • Проголосовать: нравится
  • +37
  • Проголосовать: не нравится

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

Can you explain a bit about what the Convex Hull Trick and Li Chao tree are ? Most Div 3 contestants like myself have little-to-no idea about what they are :)

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

Hard contest for Div3, I think that it is Div2. Statistics of successful solutions in comparison with previous Div3 and Div2 confirms this

  | #486 (Div. 3) | #485 (Div. 2) | #484 (Div. 2) | #483 (Div. 2) | #482 (Div. 2) | #481 (Div. 3)
-------------------------------------------------------------------------------------------------
A |          3308 |          4140 |          2291 |          3879 |           3503|          2493 
B |          2460 |          2763 |          2175 |          2952 |            636|          2508
C |          1313 |          1963 |          1416 |           743 |           1362|          2139
D |           292 |           600 |           370 |           392 |            130|           777
E |            64 |           501 |            14 |             9 |              7|          1066
F |            18 |             8 |             3 |             - |              -|           824
G |             - |             - |             - |             - |              -|           261
»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone please explain the use of npos in solution B — "if (s[i + 1].find(s[i]) == string::npos) " ?

Cplusplus.com says that it is the greatest possible value for an element of size_t , What does it means ?

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

    It is almost the same as -1 (means "not found" or some similar). I use it just because of my habit. You can replace it with -1 and nothing will change.

    I can be wrong and maybe someone else will explain it better than me.

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

    Just complementing Vovuh's answer: since npos is unsigned, -1 actually corresponds to the largest possible integer value. Since it's greater than the size of any string, it never equals a valid position, and is thus returned to indicate a failed search.

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

In solve 988D:

			if (isl && int(res.size()) < 2) {
				res = { lx, x[i] };
			}
			if (isl && int(res.size()) < 2) {
				res = { x[i], rx };
			}

Maybe it would be more correct?:

                        if (isl && int(res.size()) < 2) {
				res = { lx, x[i] };
			}
			if (**isr** && int(res.size()) < 2) {
				res = { x[i], rx };
			}
  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Woooops, yes, you are right! But surely this solution is also correct and the second if statement is useless :D It is because of you will check this pair of points when x[i] will be the right point and lx will be the left point.

    I will fix it anyway, thank you :)

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

There is any other way to think/solve D?

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

vovuh,In problem C why did you use stable_sort? Normal sort also passed. What is the difference? In the future at what kind of situations should we use stable sort?

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

vovuh in F we can use another dp with O(a) memory and O(a2) time:

in case we have umbrella in i-th position: dp[i] = min i <= j (dp[j] + (j - i) * w[i])
if we don't: dp[i] = min(dp[i + 1], dp[i])

and when doing convex hull trick on this dp, we can see that slopes are decreasing, so we don't need additional structures to maintain convex hull. so this can be solved in O(a * log(a)) time

link: 38904415

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

Problem F — Someone can help me?, i can't find mi error :(.

My code

Thanks in advance c:

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

In problem D tutorial, how is k equal to L ?

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

    If we transform to the binary representation, any power of two is represented by 1 and number of 0's (may be zero) to the right of that 1, for example: 1 and 1000, which are equal in decimal to: 1 and 8, respectively.

    So, when you sum two different powers of two together, you will get a number which is represented in binary as two 1's and number of 0's (may be zero), for example:

    10000

    +

    00100

    =====

    10100

    So, the result is not a power of two.

    But there is one case that will give us a power of two from summing two powers of two, and it is summing a power of two to itself, like:

    01000

    +

    01000

    =====

    10000

    Hope it answered.

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

      Thanks for the answer, it cleared a lot. But why does the answer not exceed 3? it could have been dist(a, d) * 4 instead of 3.

      upd: ok, I got it, in case of abcde there's still a abcd triplet in there, so the distance between a and c will still be 3*2^k, and it will be still invalid.

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

        Welcome :).

        The equation dist(a, d) = dist(a, b) * 3 came from the proofs before it.

        In the same way in which the tutorial proved that dist(a, b) = dist(b, c), we can prove that dist(b, c) = dist(c, d). So, the following equation is hold:

        dist(a, b) = dist(b, c) = dist(c, d) ( = x, for example).

        And we know that: dist(a, d) = dist(a, b) + dist(b, c) + dist(c, d) = x + x + x = 3 * x. And from here, 3 came.

        And any changing in this 3 means that some changes in the previous parts of the proof were happened, for example:

        dist(a, d) = 4 * x can come from: dist(c, d) = 2 * x, which means that: dist(b, d) = dist(b, c) + dist(c, d) = x + 2 * x = 3 * x, which isn't a power of two, but in the problem, we need dist(b, d) to be equal to a power of two.

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

Will the vector in problem C's solution too large? Cuz it maybe 2 * 1e5 * 2 * 1e5 elements...?

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

Why am i getting TLE on div 3 D when using map, and AC when using original binary search ?

Using binary search: https://codeforces.me/contest/988/submission/38916484

Using STL Map : https://codeforces.me/contest/988/submission/38916424

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

" You can also notice that the order of swaps doesn't matter and you can rearrange them in such a way that no leading zero appears on any step. " How to prove this or any kind of intuition behind this ?

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

    You can rearrange the operations in such a way that first of all you move the leftmost non-zero digit in the number to the left in order to keep the number correct after each of the next operation.

    But in general case you don't know which digit will be leftmost non-zero (there are too many cases that are depend on the digits you choose), so to make this problem much simpler you firstly move digits you choose to the right and then find the leftmost non-zero digit and move it to the left.

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

can anyone explain the 3rd case of 988F .

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

    Test Case:

    10 1 1
    0 9
    1 5
    

    the segment 0-9 is under rain and only haft an umbrella on the position 1 so you never can pass the position 0 without getting wet, so the output is -1

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

Can anyone tell, why http://codeforces.me/contest/988/submission/38851322 this solution didn't work for Problem C?

The approach given in the Editorial also fails in Python.

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

Why does the memory blow up on 7th test case? 38888128 (Problem B)

I know the complexity is O(n2)...but why does memory blow up?

Is it something to do with sorting string array?

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

Probably a simple solution to E : http://codeforces.me/contest/988/submission/38965567 As we have only 4 options 00, 25, 50, 75 at the end Just count the place by which these digit is to be moved obviously taking care of leading zeroes at the beginning in case the swap is made.

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

Can someone explain the problem with this approach for "Rain and umbrellas"?

let - dp[x][0] --> indicates that umbrella was not used for segment (x-1) to (x)

  • dp[x][1] --> indicates that umbrella was used for segment (x-1) to (x)

Now, dp[x][0]
= infinity (or invalid) --> segment (x-1) to x is under rain

= min( dp[x-1][0], dp[x-1][1] ) --> segment (x-1) to x not under rain

dp[x][1]
= (best wt umbrella uptil now) + dp[x-1][1] --> I continue carrying same umbrella

= (best wt umbrella uptil now) + min( dp[x-1][0], dp[x-1][1] )
--> I drop heavy wt umbrella and pick more lighter umbrella

Getting wrong answer. Not able to find whats wrong with the approach.

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

    The problem in your approach is best umbrella to be carried till position i may not be necessarily the best one for i+1 but other umbrella might help. For computing the optimal solution, you MUST consider all previous choices of umbrellas. This is the dynamic programming approach, what you BEST is actually greedy. Hope it helps.

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

Can anybody help me with this: http://codeforces.me/contest/988/submission/38960067 I can't find my mistake. Is stuck in testcase 5.

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

In problem F I use:

int dp[i] — the minimmum fatigue to get to position i,

bool rain[i] — true if position i is into a raining interval,

umbrellas[i] — the minimmum weight of an umbrella at position i, if there is no umbrella, umbrellas[i] = 0;

In my approach I calculate for each position i, what's the minimmum fatigue of getting there using the umbrella at position j (j <= i):

dp[i] = min( umbrellas[j] * (i — j) + dp[j — 1]) for each j from i to 0;

I can't see where my idea fails, still stuck in testcase 5.

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

    Not reading carefully the problem caused me multiple WA5, specifically this lines:

    he must carry at least one umbrella while he moves from x to x+1 if a segment [x,x+1] is in the rain (i.e. if there exists some i such that li≤x and x+1≤ri).

    Hope you don't come across the same mistake

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

about Problem D: why the size of the answer set can be 1?

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

Can anyone please tell me how standard compare function works or refer me any article about it?

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

Problem 988B- Substrings Sort Can anyone please explain how this cpp snippet works.

sort(s.begin(), s.end(), [&] (const string &s, const string &t) { return s.size() < t.size(); });

It's functionality is to simply sort the entire vector of strings according to the size of individual strings. Thanks in advance :)

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

    it works because:

    in order to obtain "YES": strings with equal length must be the same otherwise they cannot be substring of each other; string A which has length < length of B must be before B (since A must be a substring of B). So we just need to sort them and then check a substring condition for two consecutive strings (because A is a subtring of B; B is a substring of C; so A is a substring of C).

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

I have trouble understanding the description of question F. Consider the example:

10 2 2
1 5
7 10
4 1
9 2

So, can he drop his umbrella at 5; and pick another umbrella at 9; or he needs to carry an umbrella from 4 to 10 to avoid being wet from 7..9

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

I'm tryna do F using the following approach but I'm getting WA on test 19 :

let the states of dp be the umbrellas. Now dp[i] keeps the minimal fatigue to reach the umbrella i. Now for all umbrellas I've kept the minimal fatigue to reach it. So now say the best or minimal fatigue to reach the destination, when the last umbrella that is used it say k, is dp[k]+(des-k)*weight[k]. Note that the destination is the last rainy segment's end point.

So I'll take the minimum out of all (reaching the destination with K as the last umbrella.)

This looks quite right intuitively but I'm getting WA on test 19. I've been tryna figure it out but couldn't.

Please help.

submission:
https://codeforces.me/contest/988/submission/161450389