Codeforces Round #238 (Div. 2)B Domino Effect(想法题)
题目链接: 传送门
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;
}
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)