Row
You're given a row with nn chairs. We call a seating of people "maximal" if the two following conditions hold:
- There are no neighbors adjacent to anyone seated.
- It's impossible to seat one more person without violating the first rule.
The seating is given as a string consisting of zeros and ones (00 means that the corresponding seat is empty, 11 — occupied). The goal is to determine whether this seating is "maximal".
Note that the first and last seats are not adjacent (if n≠2n≠2).
The first line contains a single integer nn (1≤n≤10001≤n≤1000) — the number of chairs.
The next line contains a string of nn characters, each of them is either zero or one, describing the seating.
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No".
You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
3
101
Yes
4
1011
No
5
10001
No
In sample case one the given seating is maximal.
In sample case two the person at chair three has a neighbour to the right.
In sample case three it is possible to seat yet another person into chair three.
大意:0代表该座位没有人占,1代表该座位有人占。
有两个规则: 1,两个1之间必须隔一个0。
2,使1尽可能多。
特判一下前两个元素和后两个元素(wa了两发,没考虑后两个元素),中间的元素只需考虑 两个1之间只能隔1~2个0就行了。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 1000000000; const int maxn = 100; int T,n,m; using namespace std; int main() { int i,n,d1=INF,flag=0; char s[1010]={0}; char emp; scanf("%d%c",&n,&emp); gets(s); for(i=0;i<n;i++) { if(s[i]=='1') { d1=i; break; } } if(d1>1) flag=1; for(i=d1+1;i<n;i++) { if(s[i]=='1') { if( !((i-d1)==2||(i-d1)==3) ){ flag=1; break; } else d1=i; } } if(n>=2) { if(s[n-1]=='0'&&s[n-2]=='0') flag=1; } if(flag) printf("No\n"); else printf("Yes\n"); return 0; }
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 复杂业务系统线上问题排查过程
· 通过抓包,深入揭秘MCP协议底层通信
· 记一次.NET MAUI项目中绑定Android库实现硬件控制的开发经历
· 糊涂啊!这个需求居然没想到用时间轮来解决
· 浅谈为什么我讨厌分布式事务
· Coze Studio:字节跳动 Coze 的开源版本来了!第一时间深度解析
· 为大模型 MCP Code Interpreter 而生:C# Runner 开源发布
· 面试时该如何做好自我介绍呢?附带介绍样板示例!!!
· 复杂业务系统线上问题排查过程
· 独立开发:高效集成大模型,看这篇就够了