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

Автор awoo, история, 7 лет назад, По-русски

1175A - Путь к нулю

Идея: Roms

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

1175B - Поймай переполнение!

Идея: awoo

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

1175C - Электрификация

Идея: adedalic

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

1175D - Разделение массива

Идея: adedalic

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

1175E - Минимальное покрытие отрезков

Идея: adedalic

Разбор
Решение 1 (PikMike)
Решение 2 (PikMike)

1175F - Количество подперестановок

Идея: Roms

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

1175G - Очередная задача на разбиения

Идея: adedalic

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

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

Can someone explain the idea of https://codeforces.me/contest/1175/submission/55197107? Seems he uses a deque, but why his solution is correct?

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

Less mathematical explanation of C,anyone ?

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

    Let's consider this query: $$$n = 7$$$, $$$k = 4$$$, $$$a$$$ = {1, 4, 5, 7, 10, 14, 16}. Let's consider a range of $$$k = 4$$$ consecutive elements of $$$a$$$: {1, 4, 5, 7}. For some certain $$$x$$$, these are the 4 closest values in $$$a$$$. The 4th-closest value is the farther of 1 and 7. $$$f_k(x)$$$ is then $$$max(x - 1, 7 - x)$$$. To minimize this expression, we set $$$x$$$ to the average of 1 and 7. This is necessarily the minimum of $$$f_k(x)$$$ for all $$$x$$$ where the four closest elements of $$$a$$$ are from that set. Note that the first and last of this set must be $$$k$$$ apart, and we don't care about what's between them.

    No $$$x$$$ will ever occur where its closest $$$k$$$ elements of $$$a$$$ are not consecutive. So, we just need to find the minimum of the minimum $$$f_k(x)$$$'s from each of these ranges, i.e. the minimum value of $$$\frac{a_i+a_{i + k}}{2}$$$ for all valid $$$i$$$. This takes constant time for each check, so the solution is $$$O(n)$$$ time.

    There was certainly a gap in my logic somewhere here, but I think I got the point across. Sorry for explaining too much and overcomplicating. :P

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

    Suppose that you will brute force it, then you have a list of distances, and you sort it. You want the k-th element of it.

    Let the input be something like: [..., ?, ?, A, B, C, D, E, ?, ?, ...], with K = 3.

    Notice that if you choose C as the answer, the nearest points would be one of these:

    • A, B, C
    • B, C, D
    • C, D, E

    All the other points will certainly be farther from C.

    So the problem is all about finding a segment with the lowest value for the farthest element in this interval, which is in the middle.

    Keep looking a window of size K and get the middle point of it. Choose any interval with the lowest of these values.

    Hope it helps :D

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

Did anyone write autor's solution in F?

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

For problem G, how to prove that $$$opt(i, j) \lt opt(i, j+1)$$$, where $$$opt(i, j)$$$ is the optimal value for calculating $$$dp[i][j]$$$? We need this in order to apply the Divide and Conquer DP optimization.

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

Can anyone explain the binary search solution (accepted) for problem C ?
I was doing during the contest with binary search but couldn't get AC.

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

    The idea is to binary search on minimum distance.

    Lets say distance is d then for all i (1 to n) we can form up segments [A[i] — d, A[i] + d]

    Now check if there are minimum k+1 intersections of these segments if yes it means we can find point x such that kth value would be d.

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

anupamshah_ make a sliding window of length k(k points) and just take the midpoint of it's endpoints. Take the minimum length window.

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

can someone explain how binary lifting is applied in E ?

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

    Basically the same way it's done on trees. After you precalculated $$$up[j][i]$$$ (the index of the last taken interval if you started from $$$i$$$ and took $$$2^j$$$ intervals greedily), you set the current interval $$$cur$$$ to the one which starts to the left or at $$$x$$$ and ends as much to the right as possible, then iterate over $$$j$$$ from $$$\log n$$$ down to $$$0$$$ and set $$$cur$$$ equal to $$$up[j][cur]$$$ if the right border of $$$up[j][cur]$$$ is strictly to the left of $$$y$$$. $$$2^j$$$ is added to answer then. In the end $$$up[0][cur]$$$ should be the interval which covers $$$y$$$.

    You just have to handle some cases carefully: if the first interval covers $$$y$$$ already and if -1 is reached. You can refer to my code in editorial for implementation.

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

    sparseTable[i][j] = maximum position which can be reached from i'th point using (1 << j) intervals.

    Base Case : sparseTable[i][0] = max(sparseTable[i-1][0], be[i][j]). where be[i][j] = right endpoint of all intervals starting at i.

    Update : sparseTable[i][j] = sparseTable[sparseTable[i][j-1]][j-1] i.e. taking a jump of length (1 << (j-1)) from i and another one of length (1 << (j-1)) from point reached by previous jump.

    Query : Start from x. Consider all jumps of length power of two from largest to smallest. If by taking a jump we can reach a index less than y(note : not equal to y) update x to that index.

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

For problem F, there is a way of two pointer approach to check if the interval is a permutation, instead of using random distribution.

Assuming that we are going to handle the cases when $$$A \leq l \leq B \leq r \leq C$$$ and the maximum element is on the right of position $$$B$$$, let's set the current interval $$$[l, r]$$$ as $$$[B, B]$$$ and build a boolean array that records whether some value occur in the current interval.

Then we enumerate and move $$$r$$$ from $$$B$$$ to $$$C$$$. During that process, we move $$$l$$$ to the leftmost such that there is no duplicated element in $$$[l, r]$$$ and $$$r + 1 - \max(a_{B}, a_{B + 1}, \ldots, a_{r}) \leq l \leq B$$$. After moving, we check if $$$l = r + 1 - \max(a_{B}, a_{B + 1}, \ldots, a_{r})$$$ and $$$\max(a_{l}, a_{l + 1}, \ldots, a_{r}) = \max(a_{B}, a_{B + 1}, \ldots, a_{r})$$$.

Note that $$$l$$$ won't move right more than $$$(C - B)$$$ times, and of course won't move left more than $$$(C - A)$$$ times, so the complexity is guaranteed.

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

Anyone keep getting wrong answer on test 5 for question B?

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

In problem F, you can easily test in constant time if a subsegment is a permutation of $$$1..x$$$ by precomputing the following:

  • For each $$$r$$$, the value $$$c_r$$$ — the left end of the largest segment ending on $$$r$$$ such that it has unique values
  • A sparse table for constant time maximum queries.

A subsegment $$$l, r$$$ is a permutation if $$$l \geq c_r$$$ and $$$rangeMax(l,r) = r-l+1$$$.

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

Can someone please explain why he wrote : " That way the maximum value you can achieve is about 2^32*50000, which fits into the 64-bit integer. " in the solution of B Catch Overflow

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

    that maximum value : 2^32*50000 is the maximum value of res in his ediorial code. let's take this test case:

    100000
    for 100 //
    for 100
    ...
    for 100 // 5 times 
    add //
    add
    add
    add
    ...
    add // 99990 times of add
    end  //
    end
    ...
    end // 5 times
    

    So after about 5 "for 100", cur.top() will be 1*10^10 and therefore cur.top() will be 2^32 from now on (2^32<10^10). As there are 99990 times of add, each time res+=cur.top() (which is res+=2^32). And finnally res will be 99990.2^32 — still fits the 64-bit integer.

    But if we don't take the min(INF, cur.top()*x) (INF is 2^32), then: First, stack cur will be overflowed because the value doesn't fit the 64-bit integer. (cur.top() can be 100^49999 as there are 49999 times of "for 100") Second, long long res will also be overflowed

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

Question about problem $$$F.$$$

I understand that it is necessary for $$$f(l,r)$$$ to be equal to the xor of the first $$$r-l+1$$$ positive integers. However, how do we know this is sufficient to conclude that $$$a_l, a_{l+1}, \cdots, a_r$$$ are indeed a permutation of $$$1,2,\cdots, r-l+1?$$$ For example, the list of numbers $$$1,1,3,7$$$ has a total xor of $$$4,$$$ but this is also the xor of $$$1,2,3,4.$$$ Thus, how does the solution know that simply checking this condition is sufficient to show a subpermutation?

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

For problem E solution 2 how are they claiming that all the queries can be done in linear time? Path compression will take logarithmic time. Can anybody please explain..

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

In problem F ( The number of subpermutation ) why we have taken 128 bit strings for hashing purpose , i mean why we did not take 64 bit string or 128+64 bit string ?? and How to select number of string bits required to be on safe side ?

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

    I tried by changing the bits used for hashing and submitting the authors soln in this manner 128 -> 64 -> 50 -> 40 -> 30 -> 28 -> 26 -> 24

    and it gave WA when 24 bits were used , then i tried 25 bits and it worked ? one more query is it possible to design testcases to break higher bits soln .

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

Excuse me for my poor English, but I really can't find anything that is about "partiton" and the problem G at the same time.

Is it actually "partition" or something?

Searched "partiton" at https://fanyi.baidu.com/ and only got the explanation for "partition", so ...

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

On C submission 56001270 wrong answer query 29: Jury has better answer than participant.

Where query 29: N = 4, K = 2 a =[ 1, 3, 4, 5]

My solution gives answer = 2. Jury answer = 4.

Let, x = 2. d[] = [ | 1-2| , |3-2|, |4-2| , |5-2| ] = [ 1, 1, 2, 3] — already sorted. d[K+1] = 2.

Why jury answer is 4 ?

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

for problem d , how can i get that idea,which features can inspire me?

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

Can someone please explain the second technique i.e. path compression style in the editorial of Problem E. I tried but can understand it completely. I tried to google path compression but can't find anything other than path compression in DSU.

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

can any one explain problem C Electrification 3 2 1 2 5 why we chose 3 not 2? coz if we chose 3=

1 2 5
we get
2 1 2 
after sort==1 2 2 for k=2 we get 2

but if we choose 2
1 2 5
we get 
1 0 3
after sort=
0 1 3


now we can clearly say for k=2 '1' is smaller for 2.

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

Can someone explain better how the second optimization is applied in Div2-E ?

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

For problem F, I think of hash a subarray as (x + a[l])(x + a[l+1])...(x + a[r]) with random value of x and random prime small modulo such as 10000079 then ultilize FFT but it seems not work because of the the probability of repetition is too high :<<

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

Roms in your solution for problem F, it is asked to map each no. to a random 128-bit string. I believe for that purpose, you are using a pair<long long, long long> to store it. But you are using mt19937 which as per this blog can only generate 32-bit numbers, and to generate 64-bit numbers you should be using mt19937_64. So, isn't the model solution only mapping nos. to random 64-bit numbers? Can you please check?

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

Does DP approach for problem D got Time Exceed? Apart from it, could you help me how to use DP for D problem?

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

My solution for problem B using logarithm to avoid overflow. (https://codeforces.me/problemset/submission/1175/74378404)

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

For question D My logic: keep on getting index with minimum prefix sum till now, in order to have those indices with minimum effect in my answer using a priority_queue? I get WA on test case 7. Can you tell me why am i wrong? Can't find a test case for it. Any help is much appreciated. Thanks a lot Problem 1175D - Array Splitting My submission : 77223612

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

I did C with order statistics, binary searching the distance

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

How do I make my $$$nlogn*logn$$$ Binary Search + Prefix Sums solution pass on C?

https://codeforces.me/contest/1175/submission/85123220

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

Alternative solution for 1175F - The Number of Subpermutations
Let's count the subpermutations $$$[l, r]$$$ such that $$$a_l \gt a_r$$$ (then, reverse the array to find the other subpermutations).
For each $$$i$$$ calculate $$$f_i$$$, the maximum position such that $$$a_i, \dots, a_{f_i}$$$ are distinct.
It's easy to prove that, for each $$$l$$$, the only possible $$$r$$$ is the maximum position such that $$$r \leq f_l$$$ and $$$a_l \gt a_r$$$. $$$[l, r]$$$ is a subpermutation iff the sum of the $$$a_i$$$ ($$$l \leq i \leq r$$$) is equal to the sum of the $$$i$$$ from $$$1$$$ to $$$r-l+1$$$.
Then, you can solve the problem offline by iterating over the value of $$$a_l$$$ (from $$$1$$$ to $$$n$$$) and finding $$$r$$$ by keeping a set with the positions of the elements $$$ \lt a_l$$$.

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

There is no need for XOR-hashing in F, we can solve it in $$$O(n)$$$ with deques by maintaing suffix maximums as we iterate over the potential rightbound of a subpermutation: 250062852