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.
The first line contains single integer n (1 ≤ n ≤ 106) — 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 strings 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.
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.
3 UUD
5 6 3
10 UUDUDUUDDU
5 12 23 34 36 27 18 11 6 1
题意:给你n个台阶,U代表向右,D代表向左,然后离开当前台阶,这个台阶会翻转,也就是U变为D,D变为U,现在问从每个台阶走要多少步才能出楼梯。
题解:对于一个台阶,如果从左边出去,那么在前面包括它自己的U要小于后面的D,左面的D和右面的U都是无影响的,这个可以自行模拟一下。
然后对于每一个从左边出去的块,它一定至少走i步,i是当前的下标.
参考神犇的思路
假如序列是这样的:
UUDDUDUDU
我要从第5格出发。
8个时刻过后
UDDDUUUDU
而且回到了第5格。
看看交换了什么,从上往下第4格以及从上往下第4个U。
8个时刻意味着什么?
从上往下第4格以及从上往下第4个U位置差*2。
因为每个位置都经过了两遍,所以,只有两个位置交换符号,其他不变。
接着就一直交换,直到这个位置往上都是U。
答案就是,交换的代价+最后全U往上的代价。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
char a[1000005];
ll num[1000005],ans[1000005];
int main(){
ll n,i,cntu=0,cntd=0;
scanf("%lld",&n);
scanf("%s",a+1);
num[0]=0;
ll tot=0;
for(i=1;i<=n;i++)if(a[i]=='D')num[++num[0]]=i;
for(i=1;i<=num[0];i++){
tot+=(num[i]-i)*2;
ans[i]=i+tot;
}
for(i=1;i<=n;i++){
if(a[i]=='D')a[i]='U';
else a[i]='D';
}
reverse(a+1,a+1+n);
num[0]=0;
for(i=1;i<=n;i++)if(a[i]=='D')num[++num[0]]=i;
tot=0;
for(i=1;i<=num[0];i++){
tot+=(num[i]-i)*2;
ans[n-i+1]=i+tot;
}
for(i=1;i<=n;i++){
printf("%lld ",ans[i]);
}
printf("\n");
return 0;
}