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

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

1187A - Stickers and Toys

Идея: MikeMirzayanov

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

1187B - Letters Shop

Идея: MikeMirzayanov

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

1187C - Vasya And Array

Идея: Roms

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

1187D - Subarray Sorting

Идея: Roms

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

1187E - Tree Painting

Идея: BledDest

Разбор
Решение (Vovuh)
Альтернативное решение (PikMike)

1187F - Expected Square Beauty

Идея: Roms и adedalic

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

1187G - Gang Up

Идея: BledDest

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

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

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 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +20 Проголосовать: не нравится

    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 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    i think so

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

    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 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      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 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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 лет назад, # ^ |
    Rev. 6   Проголосовать: нравится +25 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

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

      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 лет назад, # |
Rev. 3   Проголосовать: нравится +11 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      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 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Hey, he had asked to minimize the maximum number.

        Lol, Do check your argument again.

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

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

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

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

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

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 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    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 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

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

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

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

Why so many hacks on D?

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

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

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

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

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

      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 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

      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 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

Please link this editorial to Contest materials. Thank You

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

Why in the problem C we cannot replace all of the l1,r1,t1(t1==0) l1<=i<r1 with -1 but all of the others 0?

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

I have a doubt in question 3. I had the same approach, for t=1 the elements between l+1 and r (include should be greater than or equal to the prev element). If an element is not supposed to be greater than equal to prev I make it less than the previous one. I am keeping the count of those many inequalities in an array. Here is the link. But it fails the 4th test case. So in the other solution I just made a change and ran a loop to check if it satisfies the t=0 querries, and this solution passed. Can anyone help what I have been doing wrong, here is the link for successful submission. Thanks for your time !

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

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 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

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

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

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

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

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Divide the problem into two parts.

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

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 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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);
	}
}

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

Can anyone suggest more problems like E-Tree Painting ?

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

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.

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

Nice Round. I am even not able to understand the sample input that implies it must be an quality problem.I am even unable to understand the editorial that implies, the question must be balanced one and rating judgement was indeed right. As usual kudos to Edu Round.

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

In Problem B .. I did the same thing as mentioned in editorial but instead of using vectors of 26 size I used maps ... Now I am getting TLE Don't know Why ... At max the size of map .. is 26

Here is My submission link If Any one of you Lend me hand It would be a great help .. Thanku

https://codeforces.com/contest/1187/submission/252476710