CF 405B Domino Effect(想法题)
题目链接: 传送门
Domino Effect
time limit per test:1 second memory limit per test:256 megabytes
Description
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 Input
14
.L.R...LR..L..
5
R....
1
.
Sample Output
4
0
1
解题思路:
题目保证测试数据(看input加重字眼)保证L 与 L 之间一定有 R,R 与 R 之间一定有L,所以有了这些保证就很好判断了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int N;
char str[3005];
int ans[3005];
bool flag = true,IsL = false;
memset(str,0,sizeof(str));
memset(ans,0,sizeof(ans));
scanf("%d",&N);
scanf("%s",str);
for (int i = 0; i < N; i++)
{
if (str[i] == 'L' || str[i] == 'R')
{
flag = false;
if (str[i] == 'L')
{
IsL = true;
break;
}
else
{
if (str[i] == 'R')
{
IsL = false;
break;
}
}
}
}
int j = 0,sum = 0,len = 0;
for (int i = 0; i < N; i++)
{
if (str[i] == '.')
{
continue;
}
else
{
ans[j++] = i;
}
}
if (flag)
{
printf("%d\n",N);
}
else
{
len = j;
sum = 0;
if (IsL)
{
for (int i = 0; i < len - 1; i++)
{
if (i%2==0)
{
sum += (ans[i+1]-ans[i]-1);
}
else if (i&1)
{
if ((ans[i+1]-ans[i])%2 == 0)
{
sum++;
}
}
}
if (str[ans[len-1]] == 'L')
{
sum += (N-ans[len-1]-1);
}
}
else
{
sum += ans[0];
for (int i = 0; i < len - 1; i++)
{
if (i&1)
{
sum += (ans[i+1] - ans[i] - 1);
}
else if (i % 2 == 0)
{
if ((ans[i+1]-ans[i])%2 == 0)
{
sum++;
}
}
}
if (str[ans[len-1]] == 'L')
{
sum += (N-ans[len-1]-1);
}
}
printf("%d\n",sum);
}
return 0;
}
最新文章
- 前端工程师技能之photoshop巧用系列第三篇——切图篇
- CodeFirst进行数据迁移之添加字段
- Android经典完美退出方法
- XPM转换与查看工具
- Cocos2d-X3.0 刨根问底(八)----- 场景(Scene)、层(Layer)相关源码分析
- IDEA中 @override报错的处理步骤
- Content-type 的说明
- hdu 2052 Picture(java)
- ubuntu 折腾之路
- Count Color
- DSOframer 微软官方API的查阅方法
- 细节!重点!易错点!--面试java基础篇(一)
- YII 实现布局
- Android查看stdout 和stderr
- MySQL 删除数据库中反复数据(以部分数据为准)
- web.config中configSections section节 -Z
- 2013 QCon北京演讲:跨终端的WebKit渲染机制
- 2018-2019-2 网络对抗技术 20165328 Exp2 后门原理与实践
- 【温故而知新】HTTP 报文
- atcoder NIKKEI Programming Contest 2019 E - Weights on Vertices and Edges