awoo's blog

By awoo, history, 5 years ago, translation, In English

1187A - Stickers and Toys

Idea: MikeMirzayanov

Tutorial
Solution (adedalic)

1187B - Letters Shop

Idea: MikeMirzayanov

Tutorial
Solution (PikMike)

1187C - Vasya And Array

Idea: Roms

Tutorial
Solution (Roms)

1187D - Subarray Sorting

Idea: Roms

Tutorial
Solution (Roms)

1187E - Tree Painting

Idea: BledDest

Tutorial
Solution (Vovuh)
Alternative solution (PikMike)

1187F - Expected Square Beauty

Idea: Roms and adedalic

Tutorial
Solution (adedalic)

1187G - Gang Up

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +105
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +22 Vote: I do not like it

For G, could you explain more on how to "compress all edges that connect the same nodes of the network into one edge with varying cost"?

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +20 Vote: I do not like it

    It refers to this section:

    To model the increase of discontent from the companies of people, let's convert each edge $$$(x, y)$$$ of the original graph into a large set of edges: for each layer $$$i$$$, let's add $$$k$$$ edges with capacity $$$1$$$ from the crossroad $$$x$$$ in the layer $$$i$$$ to the crossroad $$$y$$$ in the layer $$$i + 1$$$. The first edge should have the cost equal to $$$d$$$, the second edge — equal to $$$3d$$$, the third — $$$5d$$$ and so on, so if we choose $$$z$$$ minimum cost edges between this pair of nodes, their total cost will be equal to $$$d z^2$$$. Don't forget that each edge in the original graph is undirected, so we should do the same for the node representing $$$y$$$ in layer $$$i$$$ and the node representing $$$x$$$ in layer $$$i + 1$$$.

    Here, we add edges with capacities equal to $$$1$$$ and cost equal to $$$d$$$, $$$3d$$$ and so on between some pairs of nodes. While looking for augmenting paths, we may analyze only one of these edges instead of all of them. For example, let's add one edge with capacity $$$k$$$ between these nodes, and maintain the flow on it (let's denote current flow as $$$f$$$). If we want to traverse this edge from layer $$$i$$$ to layer $$$i + 1$$$, then we may denote the current cost of this edge as $$$(2f+1)d$$$, because we picked every edge with cost $$$d$$$, $$$3d$$$, ..., $$$(2f - 1)d$$$, and the next one will have cost $$$(2f+1)d$$$. And if we traverse it in the opposite direction, then the current cost is $$$(-(2f-1)d)$$$ for the same reason. It essentially reduces the number of edges in a network (roughly divides it by $$$k$$$).

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Is it possible to combine this optimization with Dijkstra with potentials?

»
5 years ago, # |
  Vote: I like it +20 Vote: I do not like it

Is there a small typo in Tutorial for C? It says "$$$b_i = 1$$$ otherwise" but this seems like it should be $$$b_i = -1$$$.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    i think so

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Maybe my reply is bit unrelated. I saw your code for problem C. Can you tell me why have you excluded "r" from getting initialized when using the statement "Arrays.fill(sorted, l, r, true)" .

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      I assume you're referring to 56395721.

      The reason for this is that sorted is an array of length n-1, where sorted[i] means that (i, i+1) is a sorted pair. Thus, saying the indices 3 through 5 are sorted is like saying the 3rd and 4th pairs are sorted.

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

For E, you mention a simpler idea without rerooting; can anybody explain this idea?

My intuition was to pick a root node (one that maximizes the height of the tree) and then return the sum of the sizes of the subtrees. It failed because I didn't know how to break ties between candidate roots — or maybe the whole approach was wrong from the beginning :/

I'd appreciate any ideas and intuition that led you to solve this problem; It would help me and other people improve!

  • »
    »
    5 years ago, # ^ |
    Rev. 6   Vote: I like it +25 Vote: I do not like it

    The approach can be simplified a little because we don't actually need to reroot the tree. The score of a child node would be the score of the parent minus the size of the child subtree plus the number of nodes not in the child subtree, which would be parentScore - childSize + n - childSize = parentScore + n - 2 * childSize.

    It would look like this:

    void dfs(int node, int p, long long score)
    {
    	best = max(best, score);
    	for (int neighbor : adj[node])
    	{
    		if (neighbor != p)
    		{
    			dfs(neighbor, node, score + n - 2 * sz[neighbor]);
    		}
    	}
    }
    

    Full solution: 56483647

»
5 years ago, # |
  Vote: I like it +10 Vote: I do not like it

My approach for C was a little different.

First, I initialize whole of my answer array to 1.

I used a 2-D array boolean overlap[i][j]. What I did was for each range that should be sorted, I traversed through the range and made overlap[i][i+1] = true for each L <= i < R.

Now for each range that should be unsorted, I find two consecutive indices i and i+1 inside the range such that overlap[i][i+1] is false. If we succeed to find such 'i', I simply do ans[i] = ans[i+1]+1. This makes the range unsorted. If we could not find such i for any of the ranges, we conclude that it is impossible, and print NO. Otherwise, we simply print YES and whole of the answer array in the next line.

Link to my AC submission: 56331868

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can we solve E using Diameter of tree__ and applying DFS to only on one leaf node of because its other siblings will give same answer.(not so sure about this approach).

56363139 This is my submission i used DP but in another way i calculated for each edge its ans and took max from that but i don't know why i am getting TLE on test case 71. My hash function for creating unique value for edge(parent,child) = ((parent<<20)+child) as its unique for each edge. Also i have avoided all the symmetrical siblings by applying another DFS. But still getting TLE. Could anyone suggest why?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I tried that(using the diameter) but it's not posible because there are many paths that can represent the diameter of the tree and try all of them it's very expensive.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I changed map --> unordered_map and got AC with your previous submission!

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Actually at first i used unordered_map<pair<int,int>,int> for the edge,that was giving me compilation error then i changed it to map then it compiled,also after that i used (x<<20+y) as a hash function for edge(x,y) also it means x->y != y->x,but i forgot to change it to unordered map.but thanks,it got passed.

»
5 years ago, # |
Rev. 3   Vote: I like it +11 Vote: I do not like it

Here is an intuition behind C:

Start with a number $$$\geq n$$$ and start assigning that number to the array from left to right.

Decrease the number when you can and keep it the same when you must.

Now, it's pretty easy from here. Just initialize the whole array with all $$$-1$$$ and for facts to type-1 fill the range $$$[l + 1, r]$$$ with $$$0$$$. After that cumulate the array. Now for all the ranges of type-2 check if those ranges are not sorted. If any range of type-2 is sorted, then answer is NO, else you have your answer.

Here is my submission which I solved after the contest: 56371547

A follow-up question could be to minimize the maximum number in the solution array.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yup the follow-up could be quite easily solved using the method mentioned in the tutorial, by just setting the rightmost value to 1 and then figuring out the leftmost value using array b.

    • »
      »
      »
      5 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I think it won't work.See the problem states "not sorted in non-decreasing order",doesn't necessarily mean "sorted in decreasing order".For this test case ~~~~~ 7 2 1 1 3 0 4 7 ~~~~~ the ans is 2.In your way it would be 4.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Hey, he had asked to minimize the maximum number.

        Lol, Do check your argument again.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Why starts form L+1, not from L in case of type 1?

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Give it a dry run on sample case-1 with $$$[l, r]$$$.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Its to handle this type of case

      10 3  
      1 1 5  
      1 6 10  
      0 2 7
      
»
5 years ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

what is s[v] in E? I'm not exactly sure.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It's the amount of nodes of the subtree that belongs to the node V(including itself)

»
5 years ago, # |
Rev. 2   Vote: I like it -8 Vote: I do not like it

I have a question regarding my solution for problem D. My approach was along these lines:

bool solve(l, r){
    idx_a = max_el_idx(A[l]..A[r-1]) // index of max element in array A between l and r
    idx_b = max_el_idx(B[l], B[r-1]) // index of max element in array B between l and r
    if(idx_a > idx_b) return false;
    push(A, idx_a, idx_b); // pushes the element at index idx_a in array A, to index idx_b
    return solve(0, idx_b) && solve(idx_b+1, r);
}

Then we can just call solve(0, n) and print "yes" if true, and "no" otherwise. The algorithm works, but I got a time limit. Link to submission: 56341552

My questions are the following:

  1. Is the time complexity of this approach not O(P * log(P)) where P = Sum over all n-s?
  2. Is there an algorithm that I can use to be able to efficiently (hopefully O(1)) answer queries of the type max(A, l, r) -> returns the maximum element in the array A, between indices l and r?

Looking forward to your help!

p.s. Feel free to criticise any part of my code! I'm here to learn :D

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Seems like you're looking for a range query data structure. Range maximum queries without modifications can be done in O(1) using sparse tables, while queries with updates can be done in O(log n) using binary indexed trees/Fenwick trees or segment trees.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Your solution is $$$O(N^2)$$$

    e.g. an ascending array, you check every elements in your find max index code.

»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Why so many hacks on D?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Why a solution for problem "D" with complexity (n log(n)) got TLE ? https://codeforces.net/contest/1187/submission/56335176

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Because the STL of C++ takes more time than you thought.Why not try Fenwick Tree?

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I thought that the complexity is (n log n) so I didn't think about anything I will try this, Thanks for your help :)

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

My solution for B is way different than what's written in there! https://codeforces.net/contest/1187/submission/56331183

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hey there. First of all, nice competition :) and thank you for the editorial. Small question, in F, where does the log(n) factor come from?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    the result need to mod $$$M$$$, so it takes $$$log(N)$$$ to get $$$x^{-1}$$$

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yeah, my bad. Of course, it should be $$$\log(MOD)$$$.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Since we are calculating inverses of values that are at most $$$n$$$, we can use the extended euclidean algorithm to find modular inverse in $$$O(log(n))$$$ time.

»
5 years ago, # |
  Vote: I like it +9 Vote: I do not like it

In problem E, the solution can also be represented as $$$\displaystyle\max_{v} \sum_{u} dist(u, v)$$$ by thinking of it with the "contribution method": once we fix a root $$$r$$$, then $$$r$$$ gets counted once, its neighbours are counted twice, and so on.

This way of looking at it might be helpful to rule out solutions based on "maximizing height", "choosing a proper diameter", and so on.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Really interesting, but how does it rule out the other solutions? Is it because there's no greedy way to pick the right node v?

»
5 years ago, # |
  Vote: I like it +7 Vote: I do not like it

Please link this editorial to Contest materials. Thank You

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I solve C in this approach:

make a connected component for all sorted range using Disjoint Set then i test the unsorted cases on the ranges if all range inside one component you can't build it.

this My code : 56333646

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Wow, I have to say that was a beautiful approach and easy to understand. Thanks, for sharing.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For C, can somebody explain array'nxt' stands for?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem C I first merged the overlapping intervals of the first type(increasing ones). Now all the increasing intervals are non overlapping. Then I checked if there is any interval of second type that comes entirely inside an interal of first type. In such case answer is not possible.

Then I filled the array with [n, n — 1....1]. Next I iterated all the increasing intervals and made them increasing. For example if a increasing segment is [4, 7] then it would look like this for n = 10

Before [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].

After [10, 9, 8, 7, 8, 9, 10, 3, 2, 1].

In this way as soon as we get out of the increasing intervals the array is non increasing. But I am getting WA. 56470710. Can someone point out my mistake or provide a test case where it fails

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The interval of the second type need not be completely inside an interval of first type. Just check for the case 10 2 1 2 7 0 4 9 The answer should be NO. Your code returns YES 10 9 10 11 12 13 14 3 2 1

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I have solved it now. Thanks for replying

»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Can you explain PikMike's solution of E task please? Especially what does dp[] mean and why we add to dp[v] (tot — sum[u]).

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Divide the problem into two parts.

    Part 1
    Part 2
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone explain me the problem B? I am a beginner in this field and I am not able to understand this problem (not even after seeing the solution). Any help would be commendable.

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Problem E (little modification of Vovuh's code)

void dfs(int v, int p = -1) {
	ans = max(ans, dp[v]);
	for (auto to : g[v]) {
		if (to == p) continue;

		dp[to] = siz[0] + dp[v]-dp[to]-siz[to] + dp[to] - siz[to];

		dfs(to, v);
	}
}

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone suggest more problems like E-Tree Painting ?

»
15 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In the E problem,according to this dp relation dpv=sv+∑to∈ch(v)dpto,

for tree like this,

1
                     / \
                    2   3

answer would be dp[1]=3+dp[2]+dp[3]=5

but if i choose nodes in order, 2 then 3 then 1, then answer will be 3+2+1=6.

Have in understood the question wrong.If so then please correct me.

UPDATE: understood. dp[node] denotes the max no. of points you can gain when you choose node as the first black node.