codeforces 405B Domino Effect (模拟题,中等难度,细心就能一遍AC)

1、http://codeforces.com/problemset/problem/405/B

2、题目大意:

给出n张牌和他们要倒的方向,我们知道一张倒的牌会压到其余的一边相邻的所有牌,同时题目又给出有往左倒的有往右倒的,如果往左倒的和往右倒的牌数相同的话就可以使得中间的牌还是站立的,最后输出有几张牌站立着

3、解题思路

题目其实很简单,模拟就行,分这么几种情况,如果第一张方向倒的是往左的,那么左边从第一张到目前的无一幸免都得倒下去,如果最后是R,那么后边的都得倒,中间的就需要判断LR之间的牌数是奇数就cnt++,因为中间的牌能最终站立着,只要处理好了两边的情况,就OK了,写完代码之后自己测了10多组样例结果测出代码好几处错误,改完自己的测试样例后,一遍就AC了,交题前的测试好重要。。。

4、题目:

B. Domino Effect
time limit per test
1 second 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 with the 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.

5、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 3005
char s[N];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",s+1);
        int flag=0;
        int cnt=0,j,qr=0,ql=0,flag1=0;
        for(int i=1; i<=n; i++)
        {
            if(s[i]=='R' && flag==0)
            {
                cnt+=(i-1);
                flag=1;
                qr=i;
            }
            if(s[i]=='R')
            {
                flag=1;
                qr=i;
                flag1=0;
                for(j=i+1; j<=n; j++)
                {
                    if(s[j]=='L')
                    {
                        ql=j;
                        if((j-i-1)%2==1)
                        {
                            cnt++;

                        }
                        flag1=1;
                        break;
                    }
                }
                if(flag1==1)
                    i=j;
            }
            else if(s[i]=='L')
            {

                ql=i;
                flag1=0;
                if(flag==1)
                {
                    for(j=i+1; j<=n; j++)
                    {
                        if(s[j]=='R')
                        {
                            qr=j;
                            if((j-i-1)%2==1)
                            {
                                cnt++;
                            }
                            flag1=1;
                            break;
                        }
                    }
                    if(flag1==1)
                        i=j;
                }
                flag=1;
            }
            else if(s[i]=='.' && flag==1)
            {
                cnt++;
            }
        }
        if(qr>ql)
        {
            cnt=cnt-(n-qr);
        }
        if(qr==ql)
        {
            cnt=n;
        }
        printf("%d\n",cnt);
    }
    return 0;
}
/*
14
.L.R...LR..L..
5
R....
1
.
5
R...L
5
LR...
*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值