Codeforces Round #600 (Div. 2) Editorial

官方题解;

(有时候不知怎么打不开cf,搬过来方便看)

1253A - Один подъем

If we set di=biaidi=bi−ai, we have to check that dd has the following form: [0,0,,0,k,k,,k,0,0,,0][0,0,…,0,k,k,…,k,0,0,…,0]. Firstly check that there is no negative element in dd.

Solution 1 : add 00 to the beginning and the end of the array dd, then check that there is at most two indices ii such that didi+1di≠di+1.

Solution 2 : let ll be the smallest integer such that dl0dl≠0, and rr be the greatest integer such that dr0dr≠0. Check that for all lirl≤i≤r, di=dldi=dl.

Complexity : O(n)O(n) for each test case.

Implementation (solution 1)

 1 #include <cstring>
 2 #include<iostream>
 3 #include<cstdio>
 4 using namespace std;
 5 int a[101000],b[101000];
 6 int n,t;
 7 int main()
 8 {
 9     cin>>t;
10     while(t)
11     {
12         t--;
13         cin>>n;int f=0;int l=n+1,r=0;
14         for(int i=1;i<=n;i++)
15         cin>>a[i];
16         for(int i=1;i<=n;i++)
17         cin>>b[i];
18         for(int i=1;i<=n;i++)
19         {
20             a[i]=a[i]-b[i];
21             if(a[i]>0)f=1; 
22             if(a[i]!=0)l=min(l,i);
23             if(a[i]!=0)r=max(r,i);
24         }
25         for(int i=l+1;i<=r;i++)
26         if(a[i]!=a[i-1])f=1;
27         if(f)
28         cout<<"NO\n";
29         else
30         cout<<"YES\n";
31     }
32     return 0;
33 }
View Code

1253B - Silly Mistake

We can solve this problem with a straightforward greedy solution: simulate the events in the order in which they occured, and as soon as the office is empty, end the current day and begin a new one.

We can prove that if there exists a valid solution, this greedy algorithm will find one (and furthermore, it will use maximum number of days, even if it wasn't required).

To do the simulation efficiently, we should maintain the state of each employee in an array (never went to the office today / in the office / left the office) and the number of employees currently in the office.

Each time we end a day, we have to reset all states of employees involved in the day (not all employees, otherwise the solution would be O(n2)O(n2)).

Final complexity is O(n+e)O(n+e) where ee is the number of employees, or O(n)O(n) if you compress the array beforehand.

Implementation

1253C - Sweets Eating

Let's sort array aa. Now we can easily that if Yui wants to eat kk sweets, she has to eat sweets k,k1,,1k,k−1,…,1 in this order, because of rearrangement inequality (put lower coefficients (day) on higher values (sugar concentration)).

A naive simulation of this strategy would have complexity O(n2)O(n2), which is too slow.

Let's look what happens when we replace kk by k+mk+m. During the first day, Yui will eat sweets k+m,k+(m1),,k+1k+m,k+(m−1),…,k+1. Then, we reproduce the strategy used for xkxk, but one day late : all coefficients are increased by 11.

Formally, xk+mxk=new+incxk+m−xk=new+inc where new=(ak+m++ak+1)new=(ak+m+…+ak+1) because of new sweets eaten and inc=(ak++a1)inc=(ak+…+a1) because the coefficient of these sweets are all increased by 11 (we eat them one day later).

We can derive the following formula : xk=(ak+ak1++a1)+xkmxk=(ak+ak−1+…+a1)+xk−m.

If we maintain the current prefix sum, and all previous answers computed in an array, we can compute all answers in O(n)O(n).

Final complexity is O(nlogn)O(nlog⁡n), because sorting is the slowest part of the solution.

Implementation

1253D - Гармоничный граф

For each connected component, let's find the weakest node ll and the biggest node rr in it (with one DFS per connected component).

If we look for all components at their intervals [l ; r][l ; r], we can see that two components should be connected in the resulting graph if and only if their intervals intersect. This leads to a O(n2+m)O(n2+m) naive solution : create a second graph where nodes represent components, add an edge between all pairs of components with intersecting intervals, and choose any spanning forest.

To optimize it, generate intervals in increasing order of ll (starting DFS in increasing order of nodes numbers). Look at them in this order, maintaining the biggest end BB seen. If lBl≤B, it is necessary to connect current interval to the interval ending at BB (hence increment answer).

It is quite easy to prove that doing only these connections is also sufficient (i.e. resulting graph will be harmonious).

Final complexity is O(n+m)O(n+m).

Implementation

1253E - Antenna Coverage

We can add an antenna (x=0,s=0)(x=0,s=0). It will not modifiy the answer, because it would be non-optimal to increase the scope of this antenna.

Let dpxdpx be the minimum cost to cover all positions from xx to mm inclusive, knowing that position xx is covered. We compute dpdp in decreasing order of xx.

Base case is dpm:=0dpm:=0.

The default transition is dpx:=(mx)dpx:=(m−x).

If position x+1x+1 is initially covered, dpx:=dpx+1dpx:=dpx+1

Otherwise, let's consider all antennas and their initial intervals [li;ri][li;ri]. If x<lix<li, let u=(lix1)u=(li−x−1), then a possible transition is dpx:=u+dpmin(m,ri+u)dpx:=u+dpmin(m,ri+u).

We take the minimum of all these transitions. Note that we always extend intervals as less as possible, but it's optimal because :

  • If after using this interval ii, we use another interval jj (at the right of ii), the time spent to extend ii could have been used to extend jj instead, which will be more optimal.
  • If ii was the last interval used, we don't care because the default transition will take care of this case.

The final answer will be dp0dp0.

There are O(m)O(m) states and O(n)O(n) transitions, hence final complexity is O(nm)O(nm) with very low constant. O(n2m)O(n2⋅m) can also get AC because of very low constant.

Implementation

1253F - Cheap Robot

Key insight 1: Since we always end on a central, at any time our robot have to be able to reach the nearest central.

Key insight 2: Since we always start from a central, from any node uu, going to the nearest central, then going back to uu can't decrease the number of energy points in the battery.

Firstly, let's do a multi-source Dijkstra from all centrals. We denote dudu the distance from node uu to the nearest central.

Consider a fixed capacity cc. Suppose that we're on node uu with xx energy points remaining in the battery. Note that xcdux≤c−du.

If x<dux<du, we can't do anything, the robot is lost because it can't reach any central anymore.

Otherwise, if xdux≥du, we can go to the nearest central, then go back to uu, hence we can always consider than x=cdux=c−du.

This is a simple but very powerful observation that allows us to delete the battery level in states explored. Hence, we can now solve the problem in O(mlogm+qmlogn)O(mlog⁡m+qmlog⁡n), doing binary search on answer and simple DFS for each query.

We need to optimize this solution. Now, reaching a node uu will mean reaching it with xdux≥du.

During exploration of nodes, the necessary and sufficient condition for being able to reach node vv from uu, through an edge of weight ww, is that (cdu)wdv(c−du)−w≥dv, i.e. du+dv+wcdu+dv+w≤c.

Hence, if we replace the weight of each edge (u,v,w)(u,v,w) by w=du+dv+ww′=du+dv+w, the problem is reduced to find a shortest path from aiai to bibi, in terms of maximum weight over edges used (which will be the capacity required by this path).

Solution 1 (offline):

Sort edges by new weight. Add them progressively, maintaining connexity with DSU.

As soon as two endpoints of a query become connected, we should put current capacity (i.e. new weight of the last edge added) as answer for this query.

To effeciently detect this, we can put tokens on endpoints of each query, and each time we do union (of DSU), we make tokens go up to the parent. If we do union by rank, each token will move at most O(logn)O(log⁡n) times.

Solution 2 (online):

Let's construct a MST of the new graph with Kruskal.

It is well-known that in this particular MST, for every pair of nodes (u,v)(u,v), the only path from uuto vv will be a shortest path (in terms of maximum weight over the path).

Hence we just have to compute the weight of paths in a tree, which can be done with binary lifting.

These two solutions both run in O(mlogm+qlogn)O(mlog⁡m+qlog⁡n). Implementation of solution 1 is a bit shorter, but solution 2 can deal with online queries.

猜你喜欢

转载自www.cnblogs.com/mjc191812/p/11878190.html