codeforces Round #238(div2) B解题报告

B. Domino Effect
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play withthe dominoes and make a "domino show".

Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.

After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.

Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!

Input

The first line contains a single integer n (1 ≤ n ≤ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string si is equal to

  • "L", if the i-th domino has been pushed to the left;
  • "R", if the i-th domino has been pushed to the right;
  • ".", if the i-th domino has not been pushed.

It is guaranteed that if si = sj = "L" and i < j, then there exists such k that i < k < j and sk = "R"; if si = sj = "R" and i < j, then there exists such k that i < k < j and sk = "L".

Output

Output a single integer, the number of the dominoes that remain vertical at the end of the process.

Sample test(s)
input
14
.L.R...LR..L..
output
4
input
5
R....
output
0
input
1
.
output
1
Note

The first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.

In the second example case, all pieces fall down since the first piece topples all the other pieces.

In the last example case, a single piece has not been pushed in either direction.


题目大意:

        一堆多米诺骨牌,Chris可以同时在多个地方让骨牌朝不同的方向倒下(左或者是右).

      求最终没有任何倾斜的骨牌个数;

解法:

       总共有n个牌,最多需要n秒,n^2的模拟算法我们完全可以承受.

       模拟时,我们要尽可能的把骨牌可能存在的状态: 

              1. 有向某方向倒下去的趋势; 

              2. 已经倒下;

              3. 依然直立;

        然而在过程中,可能存在直立骨牌的情况只能有2种:

              1. 没有骨牌影响到他;

              2. 在同一时间有2个不同方向的骨牌影响;

        那我们在模拟的时候,就要用到2个数组:

              1. s[i]表示目前骨牌的状态,分别有: 1向右趋势,-1向左趋势,0直立不动, 2 已经倒下:

              2. t[i]表示骨牌的状态从哪个时刻开始(例如刚开始的时候,t全部为0,当第1s改变是,t[i]为1);

         然后我们在判断目前骨牌是否可以向某个方向倒下,需要如下的判断:

              1. 是否具有倒下的方向趋势, s[i] = 1 || s[i] = -1;

              2. 这个倒下的趋势,不能是当前时间遗传的 t[i] != k(k表示当前时间);

              3. 倒向的目标是否可以遗传这个趋势,这里可以分2种情况,一种是一直都没被传递(s[i] == 0),

                   还有一种是具有某一种趋势,并且是在本时间更新的(s[i] != 2 && t[i] == k);

      注意:

             如若存在 ...RL...情况的话,是会直接

#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

int n, ans;
int s[3010], t[3010]; //s[i]为目前骨牌的状态, t[i]为骨牌状态的起始时间

void init() {
	scanf("%d\n", &n);
	for (int i = 1; i <= n; i++) {
		char tmp;
		scanf("%c", &tmp);
		switch (tmp) {  
			case '.' : s[i] = 0;  break; //s[i]==0表示i骨牌直立
			case 'L' : s[i] = -1; break; //s[i]==-1表示骨牌具有向左趋势
			case 'R' : s[i] = 1;  break; //s[i]==1表示骨牌具有向右趋势
		}
	}
}

void solve() {
	for (int k = 1; k <= n; k++) { //最多操作n次
		for (int i = 1; i <= n; i++) {
			if (s[i] == -1 && t[i] != k) {
			//  向左倒,且该状态不是当前时间更新
			    s[i] = 2; //s[i]==2表示骨牌已经倒下
				t[i] = k; //t[i]=k表示骨牌已经在k时间被修改状态

				if (s[i-1] == 0 || (t[i-1] == k && s[i-1] != 2)) { //倒向的目标具有可传递性
					t[i-1] = k;
					s[i-1] += -1;
				}
			}

			if (s[i] == 1 && t[i] != k) { //右倒情况
				s[i] = 2;
				t[i] = k;

				if (s[i+1] == 0 || (t[i+1] == k && s[i+1] != 2)) {
					s[i+1] += 1;
					t[i+1] = k;
				}
			}
		}
	}

	for (int i = 1; i <= n; i++)
		if (s[i] == 0)  ans++;

	printf("%d\n", ans);
}

int main() {
	init();
	solve();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值