Codeforces Round #677 (Div. 3, CF1433) solution

Article Directory

Solution

T1

Direct violence is enough. Mathematical techniques can also be used.

T2

For every two consecutive segments 1 1An interval of00The sum of 0 is the answer.

T3

First, if all numbers are the same, obviously output − 1 -11

Otherwise, there must be a maximum that can eat all other numbers. We only need to find a maximum value so that its left side or its right side are not all maximum values.

Time complexity O (n) O(n)O ( n )

T4

An ingenious and somewhat watery structure problem.

First, we sort the entire sequence. If all numbers in this sequence are the same, obviously it won't work; otherwise, we find the longest prefix with equal numbers . Then, the first number is connected to all numbers not in this prefix , and the last number is connected to all numbers in this prefix except the first number .

Time complexity O (n) O(n)O ( n ) . Thatn ≤ 5000 n ≤ 5000n5 0 0 0 There is a bit of water.

T5

A combinatorial math problem that compares water. Let m = n 2 m=\frac n 2m=2n

First, there is a total of A nm × m! A_{n}^m×m!Anm×m ! Kinds of plans (if the same plan is not deleted).

It can be found that { 1, 3 1,3 1,3}{ 2 , 4 , 5 2,4,5 2,4,5 } and { 2, 4, 5 2,4,5 2,4,5}{ 1 , 3 1,3 1,3 } Essentially the same, so divide by2 22

Adhesion, { 1, 3, 4 1,3,4 1,3,4 } and { 3, 4, 1 3,4,1 3,4,1}, { 4 , 1 , 3 4,1,3 4,1,3 } are essentially the same. For a set there arem 2 \frac m 22mKind, but there are two sets, so divide by (m 2) 2 (\frac {m} 2)^2(2m)2

So the answer is A nm × m! 2 × (m 2) 2 \frac {A_{n}^m×m!} {2×(\frac {m} 2)^2}2×(2m)2Anm×m!

Why can't even Ni Yuan take the exam? Just make a n ≤ 20 n ≤ 20n2 0 ? Is there any distinction...

Trivia: This problem can also be solved with hash!

T6

A dp dp with a little bit of thinking contentd p title.

First, we run dp dp for each rowd p . The state is designed asdpi, j dp_{i,j}dpi,j, Which means that in this row, a total of ii is selectedThe number of i , the sum of these numberskkThe value of k isjjWhen j , the maximum value of the sum of these numbers.

Then, for each row, for all i ≤ m 2 i≤\frac m 2i2m, We all save all dpi, j dp_{i,j}dpi,j. Then, for multiple rows, we run the grouping backpack again and it's done.

Time complexity O (nmk) O (nmk)A ( n m k ) .

T7

A good graph topic.

First, we first run out all x, yx, yx,The shortest path between y (usingD ijkstra DijkstraD i j k s t r a ), and then enumerate the edges (that is, change the weight of this edge to0 00 ). Suppose this edge isu −> v u->vu ->v , then ifxxxyyIf the shortest path of y changes, it must become:

①From xxx arrivaluuu, 来uuu tovvv fromvvv toyyy ;
②Fromxxx tovvv fromvvv arrivaluuu, 来uuuyyand

It can be found that we only need to run out with u, vu, vu ,v is the shortest path of the source, and then try to decreasexxxyyThe shortest path of y is fine. Since this is an undirected graph, the correctness of this solution is guaranteed (in an undirected graph,aaa tobbThe shortest path of b isbbb toaaa shortest path)

Time complexity O ((n + m) (k + m) logm) O((n+m)(k+m)logm)O ( ( n+m)(k+m ) l o g m ) , you can pass this question.

Code

Cuckoo

Guess you like

Origin blog.csdn.net/Cherrt/article/details/109198781