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

Автор pabloskimg, история, 4 года назад, По-английски
  • Проголосовать: нравится
  • +88
  • Проголосовать: не нравится

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

I'm washed and only tried the mirror for a bit but here's some sketches

C (didn't code)
D (didn't code)
F
H
I
J
K
L
M
  • »
    »
    4 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    In problem D, it's possible to keep the answer using only a segment tree with lazy propagation instead of a treap.

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

    Can you post the codes of I, J, M, I have been WA...thk

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

    Could you please elaborate a bit more on problem M? pls.

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

      Let's try to approach this problem greedily. At each step, we select the assignment with the lowest index such that we can still complete the remaining assignments later.

      Suppose we just want to complete all assignments. The best strategy is to sort the assignments by $$$ D_i $$$ and complete them in that order. Let's call this the basic order.

      Now, suppose we have the assignments sorted by $$$ D_i $$$, i.e., $$$ D_1 \leq D_2 \leq \dots \leq D_n $$$. What if we want to place the $$$ i $$$-th assignment first?

      First, let's notice that the best strategy remains the same: place the $$$i$$$-th assignment first and then order the remaining $$$n-1$$$ assignments by $$$D_i$$$. But how can we check if this new order is valid?

      We want to ensure that

      $$$ \sum_{k=1}^{j} T_k \leq D_j $$$

      for each $$$j$$$, which holds for the basic order (as we checked before).

      Now, what happens to these equations when we move the $$$i$$$-th assignment to the first position?
      - For $$$j \geq i+1$$$, nothing changes.
      - For $$$j = i$$$, since $$$i$$$ is now the first element, it is already satisfied.
      - The key case is for $$$j \lt i$$$. In the basic order, these assignments were completed before the $$$i$$$-th assignment. Now, since we are placing the $$$i$$$-th assignment first, the equation becomes:

      $$$ T_i + \sum_{k=1}^{j} T_k \leq D_j $$$

      This means that we need

      $$$ T_i \leq D_j — \sum_{k=1}^{j} T_k $$$

      for all $$$j \leq i-1$$$.

      You can think of this condition as ensuring that the difference between the time we originally completed assignment $$$j$$$ and its deadline is large enough to fit the $$$i$$$-th assignment at the beginning.

      To check this efficiently, we can maintain the minimum value of $$$ D_j — \sum_{k=1}^{j} T_k $$$ for all $$$ j \leq i-1 $$$.

      Since we can check for every assignment whether it can be placed first, we always select the one with the lowest original index, erase this assignment, and repeat the process for the remaining $$$ n-1 $$$ assignments, remembering the total time spent on the assignments already placed.

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

Auto comment: topic has been updated by pabloskimg (previous revision, new revision, compare).

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

Are the test cases the official ones? I got AC on problem C with an incorrect solution.

Test case
Polygon image

My AC code (152805814) answers 3, but the correct answer is 1 (and this other AC submission answers 1 152705102)

Edit: And this other test breaks all currently accepted solutions except from 152757395:

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

I'm sorry for my poor English

D
E

problem G I don't know the solution, I just guess

G

problem A I don't know how to solve it,wish somebody could tell me, I guess the key is that for a $$$Ax+By \gt =S$$$ the different $$$A$$$ and $$$B$$$ is $$$O(n^2)$$$,but I don't know how to solve the condition of simple quadrilateral

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

    Could you explain better what you do when x < 0

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

      let $$$r[i]=nxt[i]$$$,$$$l[i]=i$$$,then the answer will be $$$r[i]-l[i]$$$,for example $$$j=3,nxt[j]=5$$$ and when iterate $$$i=4$$$,assume that we get $$$sum[i]+x \lt sum[j-1]$$$,now $$$nxt[j]$$$ should be $$$4$$$,so we need to modify all $$$j$$$ that $$$sum[j-1] \gt C$$$,we can use a segment tree to modify $$$[sum[i]+x,inf]=i$$$,so we use segment tree to record

      $$$cnt[x]$$$ the number of $$$i$$$ in this node

      $$$sumr[x]$$$ the sum of $$$nxt$$$ in this node

      $$$suml[x]$$$ the sum of $$$i$$$ in this node

      my solution https://pastebin.com/RvzgS7n5

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

    I have a question about your proposed solution for problem E. How do we update the segment tree? Because let's say we want the minimum in range $$$[p+1, r]$$$, this minimum depends on a third parameter $$$l$$$ which is outside the range, so if you change $$$l$$$, all the values in the range change.

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

      You can build $$$n + 1$$$ segment trees where you fix $$$l$$$ for each segment tree. Each segment tree has size $$$O(n)$$$ so you have overall $$$O(n^2)$$$ memory in Segment trees.

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

      Oh,my fault,I use $$$O(n)$$$ segment tree to do it, the memory is $$$O(n^2)$$$ here is my solution (https://pastebin.com/Wut9fPYZ)

      let me just expain it

      $$$F$$$ is the same as I mentioned above

      $$$G$$$ and $$$H$$$ are the segment tree

      you can discuss 4 situations and use $$$4 * n$$$ segment tree to update the $$$F$$$

      as the memory limit, so I just use $$$maxm = 8000$$$ instead of $$$maxm = maxn « 2 = 12000$$$ or you can use $$$vector$$$

      btw,as you just want to know a min/max of prefix/suffix,you can use fenwick tree, the memory will be $$$1/4$$$ of the segment tree or std::set maybe also can solve this problem

      I'm sorry I haven't answered your so long

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

One more solution

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

I really liked problem G. Is there a way to encode (e.g. find a unique string/integer sequence to represent it) an undirected, unlabeled tree faster than in O(n*log^2(n)) ( let's say without hashing ) ?

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

    Yes, you can encode any rooted tree in $$$O(n log n)$$$ implementing AHU algorithm carefully. You can read more about it here. Then, using the code for a rooted tree is easy to generalize it for an unrooted one because each tree has at most two centroids. So you can call the algorithm for each centroid and merge both vector/strings.

    It's also possible to have an $$$O(n)$$$ algorithm if we use radix sort in AHU algorithm. Though I have never implemented it with radix sort but I read about it in this comment. But if I wanted a linear algorithm, I would prefer hashing to receive a simple integer to encode the tree. It's somehow similar to hashing a string and I learned it from this wonderful blog and this paper.

    However, I've seen your code for problem G and it seems that you are already familiar with a naive AHU algorithm. So to give you a complete answer I will try to explain how you can go from $$$O(n log^2 n)$$$ to $$$O(n log n)$$$.

    If I'm not wrong, what you are doing to encode a rooted tree is the following algorithm:

    Naive algorithm

    The size of the code is $$$O(n)$$$ but the algorithm is $$$O(n^2 log n)$$$ because for each node you are sorting a vector of size $$$O(n)$$$. However, for an unrooted tree, as you use the centroid, then each level of the tree will have complexity $$$O(n log n)$$$ and there are $$$O(log n)$$$ levels so the overall complexity is $$$O(n log^2 n)$$$.

    Let's optimize it to $$$O(n log n)$$$ for a rooted tree. The problem is that you are storing the complete code for all the subtrees. Instead of that, we can compute something like a "partial code" based only on the children of the node and not on the whole subtree. However, in this new algorithm, we will return a list of integers instead of a string and we're going to process level by level, starting from the deepest level.

    Optimized algorithm

    The code has size $$$2n - 1$$$ because each node will append 0 (so we have $$$n$$$ zeroes) and each node will have its label appended by its parent, except from the root that has no parent (so $$$n - 1$$$ more integers). And the overall complexity is $$$O(n log n)$$$ because the sum of sizes of all the vectors that we sort is $$$O(n)$$$ instead of $$$O(n^2)$$$ in the naive algorithm.

    Here is my implementation to encode a rooted tree (for unrooted tree just encode for each centroid). I hope it helps.

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

        Thank you for devoting your time to answer. The algorithm I came up with is pretty much what you described, except to achieve good complexity what I do is; at each node, I sort all the strings corresponding to small subtrees and merge them, I just dont touch the heavy subtree (if there is one, then I just reuse the string corresponding to heavy subtree without copying it). I think this trick is called just "merge small onto large" or something, I am not sure. Tbh now that I look at my algorithm I think a bound tighter than O(n * log^2(n)) may be possible. I cant come up with a tree when the complexity is as bad as n*log^2(n). Definitely n*log(n) is a lower bound. I feel like n*log(n) is the real upper bound as well.

        What you said totally makes sense though. We can just give ranks to nodes, and compare the ranks of the children, we dont need whole subtrees.

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

      I did the first approach but avoided the $$$n^2\log n$$$ complexity by comparing the strings with hashes as well. I got a pretty bad constant but I think it can be improved.