CF378E-Sleep in Class及378反思

来源:互联网 发布:故宫淘宝实体店地址 编辑:程序博客网 时间:2024/06/03 06:58

Sleep in Class

Codeforces Round #378 (Div. 2) E.Sleep in Class

The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs.

The stairs consists of n steps. The steps are numbered from bottom to top, it means that the lowest step has number 1, and the highest step has number n. Above each of them there is a pointer with the direction (up or down) Olga should move from this step. As soon as Olga goes to the next step, the direction of the pointer (above the step she leaves) changes. It means that the direction “up” changes to “down”, the direction “down” — to the direction “up”.

Olga always moves to the next step in the direction which is shown on the pointer above the step.

If Olga moves beyond the stairs, she will fall and wake up. Moving beyond the stairs is a moving down from the first step or moving up from the last one (it means the n-th) step.

In one second Olga moves one step up or down according to the direction of the pointer which is located above the step on which Olga had been at the beginning of the second.

For each step find the duration of the dream if Olga was at this step at the beginning of the dream.

Olga’s fall also takes one second, so if she was on the first step and went down, she would wake up in the next second.

Input
The first line contains single integer n (1 ≤ n ≤ 10^6) — the number of steps on the stairs.

The second line contains a string s with the length n — it denotes the initial direction of pointers on the stairs. The i-th character of string s denotes the direction of the pointer above i-th step, and is either ‘U’ (it means that this pointer is directed up), or ‘D’ (it means this pointed is directed down).

The pointers are given in order from bottom to top.

Output
Print n numbers, the i-th of which is equal either to the duration of Olga’s dream or to  - 1 if Olga never goes beyond the stairs, if in the beginning of sleep she was on the i-th step.


题解

E题咋看真的没思路,比赛时也没做出来。其实这道题是完全在能力范围内的,还是我最擅长的前后缀和orz。
分析一个给定的字符串,我们可以发现一个特点:不存在转不出去的序列,也就是说,所有的位置都是有解的,-1这个输出完全是蒙人的。
然后可以看,既然所有位置都有解,我们可以观察一个位置是怎么走出去的,不难发现,对于一个‘U’来说,都是先向右走再向左走,这样走来走去最后走出去的。对于‘D’就反过来。这样来看,我们就可以发现一个解的轨迹只跟那些需要转头的点有关,或者说解只跟拐点相关。那么什么是拐点呢?对于一个特定的位置来说,容易想到在它左边的‘U’和在它右边的‘D’就是拐点,统计所有可以到拐点到这个位置的距离就是这个位置的解了。
上面只是初步的想法,实际上如何统计所有可以到拐点也是一个问题。不难想到如果在左边的拐点多于在右边的拐点,那么在左边的拐点一定有一部分是用不到的。更具体的讲,设左拐点数目为n,位置分别为a1,a2…an,右拐点数目为m,位置分别为b1,b2…bm,且当前点位置为k,有 a1<a2<<an<k<b1<b2<<bn

0 0
原创粉丝点击