Codeforces Round #443 (Div. 2) D,E

来源:互联网 发布:高中化学辅导书 知乎 编辑:程序博客网 时间:2024/04/29 23:16

看了题解发现做题的时候比较智障。

D:
给一个长度为n的序列,每个序列有一个颜色ai,重复m次,一旦相同的颜色连续的长度大于k,这段就将被消除,求最终序列的长度。
Sol:

First, let’s see what happens inside one bus. We can use a stack containing pairs (city, number of participants from it). When the number of participants reaches k, we erase the pair.
Suppose we build this stack. r is its size, (ci, di) are pairs in it. Now consider the interaction of two such buses. At the border, a team is formed, if c1 = cr and d1 + dr ≥ k. If the inequality becomes an equality, then another team can be formed from the second and penultimate groups, etc. Let’s find the greatest p such that for each i = 1… p we have ci = cr + 1 - i and di + dr + 1 - i = k. Since the condition on i is symmetric with respect to , if p ≥ ⌈ r / 2⌉, then p = r.
Consider the case p = r separately. This means that the two buses are completely divided into teams. If m is even, answer is zero, otherwise answer is the sum of di.
Also, consider the case when r is odd and p = ⌊ r / 2⌋. In this case, after removing all teams at the borders of the buses, the queue looks like: left part of the first bus — mdp + 1 people from the cp + 1 city — right part of the last bus. If the number of people in the middle is divisible by k, then they will be divided into the commands, and the first half will unite with the last, and the answer is zero. If it doesn’t, then some teams will be formed in the middle, and the process will end there.
Finally, if r is even smaller, it can be seen that after the formation of teams at the borders of buses the process will end.

个人感觉官方题解讲复杂了。(可能是我没看懂)。看了前十的一份代码发现这道题其实是普及组难度题。

题解:

首先,删除原序列长度超过k的连续段,现在原序列中不含长度超过k的串,如果有新的删除序列,一定是当前序列前后拼接而成。
现在假设原序列不是一种颜色(如果是一种颜色则break)。也就是说该序列中间有断点将前后分开,那么直接检查前后序列是否拼起来相同颜色超过k,如果超过k则删除前后的长度为k的序列,问题又变成了另一个更短的序列重复m次,这是一个子问题。否则break。

如果跳出循环,那么检查当前序列颜色是否全相同,如果不是那么序列不能再消除。直接输出即可。(记得加上首尾序列的消除长度,因为他们不能拼接在一起)。
若颜色相同,直接mod k,如果剩余长度不为0,那么同上,否则直接输出0(因为中间序列消除完后两端序列可以拼接在一起)

Code:http://paste.ubuntu.com/25833759/

E:
题意:
直接看原题目吧,题意很好理解。

Recently a tournament in k kinds of sports has begun in Berland. Vasya wants to make money on the bets.
The scheme of the tournament is very mysterious and not fully disclosed. Competitions are held back to back, each of them involves two sportsmen who have not left the tournament yet. Each match can be held in any of the k kinds of sport. Loser leaves the tournament. The last remaining sportsman becomes the winner. Apart of this, the scheme can be arbitrary, it is not disclosed in advance.
Vasya knows powers of sportsmen in each kind of sport. He believes that the sportsmen with higher power always wins.
The tournament is held every year, and each year one new participant joins it. In the first tournament, only one sportsman has participated, in the second there were two sportsmen, and so on. Vasya has been watching the tournament for the last n years. Help him to find the number of possible winners for each of the n tournaments.

Sol:

Imagine a directed graph, in which the vertices are participants, and the edge means that one participant can win the other in some kind of sports. A participant can win a tournament if there is a directed tree in this graph that contains all vertices, and this player is a root.
Consider the condensation of this graph. Since for any two vertices there is an edge at least in one direction, condensation is a path. It is clear that the required tree exists if and only if the root lies in the first strongly connected component.
We will maintain these strongly connected components. For each of them we will store its size, the greatest power and the smallest power in each kind of sports.
What happens when the new sportsman is added? He can defeat the component if in some kind of sports he is stronger than the minimum in this component. Similarly, he can lose to a component if in some kind of sports he is weaker than the maximum in this component. We need to find the weakest of those components that he can lose, and the strongest of those components that he can defeat. If the first component is stronger than the second, the new sportsman forms a new component. Otherwise, all the components between the first and the second merge into one, and the new sportsman joins it.
How to do it effectively? We will store the components in a some search tree and use the comparison by minimum in the first kind of sports as a comparator. It’s easy to see that if you take any other sport or replace a minimum with a maximum, any two components will be compared in the same way. All we need is binsearch by one of the mentioned comparators: minimum or maximum for one of the kinds of sports.
At each step the number of operations with the tree is O(k) + k· number of components merged into one. At each step at most one component can be added, so the amortized time of one step is .
Overall time complexity is .

题解确实有点长,我说说我的理解:

首先,如果两个人彼此没有严格的大于小于关系(每个项目都比对方优),那么这两个人可以看做一个人(因为两个人比赛任意一个人可以胜出),不妨把这类人看做图中的一个点,记录这个点的所有人的最优值和最劣值(这些值都能与其他人比赛)。

将所有的人合并以后,图上的所有点必然满足严格的大于小于关系(否则合并)。可以胜出的人就是当前最大点所包含的人数。现在考虑如何加入一个点:
首先,每个点连边向前继,当前的图就是一个链,考虑找到加入的人可以胜出的最大点(用最大点的最小值比较)和可以失败的最小点(比较方法相反),合并这一段即可(这一段形成了一个新的环,可以缩成一个点)。

找出最大最小点写一颗平衡树或套用set即可。

Code:http://paste.ubuntu.com/25833796/

原创粉丝点击