Codeforces 733e

解析一道算法题目,关于在特定规则下的楼梯行走策略,旨在找到从每个台阶出发直至走出楼梯所需的最短步数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sleep in Class
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 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.

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.

Examples
input
3
UUD
output
5 6 3 
input
10
UUDUDUUDDU
output
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值