codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

500.md (8289B)


      1 Source: https://codeforces.com/blog/entry/15513
      2 
      3 ### [500A — New Year Transportation](//codeforces.com/contest/500/problem/A)
      4 
      5 In this problem we are given a directed graph, and asked whether a particular vertex is reachable from vertex 1. It is possible to solve this by running a depth-first search starting from vertex 1.
      6 
      7 Since every vertex has at most one outgoing edge, it is possible to write the DFS as a simple loop. Some people used this to make [very short](//codeforces.com/contest/500/submission/9307130) submissions.
      8 
      9 ### [500B — New Year Permutation](//codeforces.com/contest/500/problem/B)
     10 
     11 Imagine a graph with one vertex for each entry in the permutation, and edges between pairs of swappable entries. It is easy to see that no matter how many swaps we make, no entry can end up in a location that is not in its connected component. We can also show that within any connected component, we can achieve any rearrangement of the entries that we wish.
     12 
     13 Since we want to make the lexicographically smallest permutation, we should greedily rearrange the entries within each connected component so that they are in increasing order.
     14 
     15 *N* ≤ 300, so we are not concerned with runtime. One can determine the connected components using a number of standard algorithms (Floyd-Warshall, Union Find, etc.).
     16 
     17 ### [500C — New Year Book Reading](//codeforces.com/contest/500/problem/C)
     18 
     19 Consider an arbitrary day *d*, on which we must read book *b*. Let *d*' be the last day before *d* on which book *b* was read (if there is no previous day, let *d*' =  - ∞). Observe that, on day *d*, we cannot avoid lifting all of the books read on any day between *d*' and *d*. If we add up all the weights of these required liftings, we will have a lower bound on the answer.
     20 
     21 This lower bound can be computed with two loops, in *O*(*M*2) time. We must be sure not to add a book's weight multiple times, if it was read multiple times in the interval (*d*', *d*).
     22 
     23 If we initially arrange the books in order of the first day on which they are read, we will achieve the described lower bound. So, we have our answer.
     24 
     25 ### [500D — New Year Santa Network](//codeforces.com/contest/500/problem/D)
     26 
     27 By linearity of expectation, *E*(*d*(*c*1, *c*2) + *d*(*c*1, *c*3) + *d*(*c*2, *c*3)) = *E*(*d*(*c*1, *c*2)) + *E*(*d*(*c*1, *c*3)) + *E*(*d*(*c*2, *c*3)). By symmetry, those three values are all equal. So, we need only compute *E*(*d*(*c*1, *c*2)). We can multiply it by 3 to find the answer.
     28 
     29 Let us imagine that a random selection (*c*1, *c*2, *c*3) is made. We'll define a function *f*(*j*), for each edge *j*, so that *f*(*j*) = *w**j* if *j* is part of the shortest path from *c*1 to *c*2, and *f*(*j*) = 0 otherwise. Observe that ![](https://espresso.codeforces.com/c72d62815b000da4dbfa44f827edc7b662133c5c.png). Again by linearity, we have ![](https://espresso.codeforces.com/221c02a874fe395661fbf8e52455669116b42e28.png).
     30 
     31 We can write *E*(*f*(*j*)) = *w**j*·*P*(*j*), where *P*(*j*) is the probability that edge *j* is included in the path from *c*1 to *c*2. If we compute *P*(*j*) for all *j*, we can then compute ![](https://espresso.codeforces.com/b2297b25218e5064ef539ef6d564cb774c8b8d04.png) in *O*(*N*). Also, we can handle updates in *O*(1); if *w**j* changes to *w*'*j*, we subtract *w**j*·*P*(*j*) from the answer, and add *w*'*j*·*P*(*j*).
     32 
     33 So, let's figure out *P*(*j*) for each *j*. Any edge *j*, if removed from the tree, separates the graph into two separate connected components *a* and *b*. Edge *j* will be included in the shortest path from *c*1 to *c*2 if and only if one of them is in *a*, and the other is in *b*. So, we want the number of ways to select (*c*1, *c*2, *c*3) such that *c*1 and *c*2 are on opposite sides of edge *j*, divided by the total number of ways to select (*c*1, *c*2, *c*3). Let's have |*a*| and |*b*| denote the number of vertices in *a* and *b*, respectively. Then ![](https://espresso.codeforces.com/b8a60c65abf37bbccf6aa78a3ef1ec7ef5bdc8f8.png).
     34 
     35 To figure out |*a*| and |*b*| for each *j*, we can root the tree arbitrarily and compute the depth and subtree size for each vertex in *O*(*N*) time. Then for an edge *j*, if *v**j* is the deeper vertex incident to *j*, we know one component has size equal to the subtree size of *v**j*. For the other component, we use the fact that |*b*| = *N* - |*a*|.
     36 
     37 ### [500E — New Year Domino](//codeforces.com/contest/500/problem/E)
     38 
     39 When domino *i* is knocked over, it covers the interval [*p**i*, *p**i* + *l**i*]. If we see a query (*x**j*, *y**j*), it is equivalent to the question "If we knock over all dominoes with index ![](https://espresso.codeforces.com/02e6e7355d9b698e1b6ed8eee87c84454f5ad655.png), how much of the interval [*p**x**j*, *p**y**j*] won't be covered?" We can modify that question a little more, to say that we knock over all dominoes with index *i* ≥ *x**j*, without changing its answer.
     40 
     41 Now, consider knocking over the dominoes from right to left. As soon as we knock over domino *i*, we will immediately process all of the queries with *x**j* = *i*, and record their answers. What we need is a data structure that supports two operations: "cover range [*p**i*, *p**i* + *l**i*]" (when we knock over a domino) and "compute how much of the range [*p**x**j*, *p**y**j*] is not covered" (when we wish to answer a query). This can be done using coordinate compression and a segment tree.
     42 
     43 It's worth noting there are a lot of other ways to solve this problem. The other tutorial uses a completely different approach. Also, the approach described here may be implemented using different data structures. For example, here is [my implementation](//codeforces.com/contest/500/submission/9331215) using BBSTs and a Binary Indexed Tree.
     44 
     45 ### [500F — New Year Shopping](//codeforces.com/contest/500/problem/F)
     46 
     47 We will use the standard dynamic programming approach for 0/1 knapsack. To summarize what it does for us, imagine we are given an ordered list of items (*c**i*, *h**i*). Let *F*(*k*, *b*) be the maximum happiness we can buy, if we consider only the first *k* items in the list, and have a budget of *b*. If *K* is the size of the list, and *B* is the maximum possible budget, we can compute *F*(*k*, *b*) for all possible *k* and *b* in *O*(*KB*) time.
     48 
     49 Let us sort all of the items by their display time. Suppose that we focus on the queries (*a**j*, *b**j*) with ![](https://espresso.codeforces.com/44f59982bdab574296469627debf99a248069c37.png), for some particular *t*. Let *A* be a list of the items with display time in (*t* - *P*, *t*], and let *B* be a list of the items with display time in (*t*, *t* + *P*). Let both lists be sorted in order of display time.
     50 
     51 Every query with ![](https://espresso.codeforces.com/44f59982bdab574296469627debf99a248069c37.png) will have available to it some suffix of *A*, along with some prefix of *B*. We'll do a knapsack DP on the elements of *A*, in reverse order, and another on the elements of *B*, in normal order. Finally, to answer any query (*a**j*, *b**j*) with ![](https://espresso.codeforces.com/44f59982bdab574296469627debf99a248069c37.png), we can consider all possible ways to split the budget *b**j* between the items in *A* and the items in *B*, in linear time. For each possible way to split the budget, we need simply look up one value from each DP table to know the maximum possible happiness.
     52 
     53 If we perform the process above on *t* = 1, 1 + *P*, 1 + 2*P*, 1 + 3*P*, ... until *t* exceeds the maximum possible day *D*, we'll be able to answer all of the queries. Let's think about the runtime of this solution. For each *t*, the described process takes *O*(*KB*) time, where *K* is the number of items whose display time is in (*t* - *P*, *t* + *P*). Each object can only appear in up to two of these intervals. So, the overall runtime for all of the knapsack DP's we perform is *O*(*nB*). Computing the final answer takes *O*(*B*) per query, or *O*(*qB*) overall.
     54 
     55 ### [500G — New Year Running](//codeforces.com/contest/500/problem/G)
     56 
     57 Don't know how to solve this yet. Maybe someone who is not gray can provide the solution. :)