Codeforces Round #484 (Div. 2) A. Row
Posted moxin0509
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #484 (Div. 2) A. 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.
【题意】:
题意就是1代表这个位置有坐人,0代表这个位置没有坐人。
如果1旁边有坐人,就是No
如果空位置还能放下一个1,也是No
【思路】:
我发现如果有3个连续的0的话,肯定是No
如果有两个连接的1的话,肯定也是No
所以我一开始写了一个判断3个0的,然后wa
后来想了一下,两个连续的0也可能导致No
比如,001 or 100
然后我发现了一下就是一开始两个0或者是结尾两个0,
这种情况就没其他的。
特判一下就可以了;
但是还是wa。
然后我想会不会是一个数的时候出现问题
果然一个0也有可能是No
针对于0
这种情况
然后考虑完后一发ac
附上代码:
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int MAXN = 1005; char team[MAXN]; int main() { int n; while(~scanf("%d",&n)) {//printf("...\n"); memset(team,0,sizeof(team)); scanf("%s",team); int ans=0; int flag=0; for(int i=0; i<n; i++) {if(n==1&&team[i]-‘0‘==0) { flag=1; break; } if(team[i]-‘0‘==0) {ans++; if(ans>=3) { flag=1; break; } if(ans==2) { if(i==1||i==n-1) {flag=1; break; } } } else if(team[i]-‘0‘==1) { ans=0; if(team[i-1]-‘0‘==1||team[i+1]-‘0‘==1) { flag=1; break; } } } if(flag) printf("No\n"); else printf("Yes\n"); } return 0; }
以上是关于Codeforces Round #484 (Div. 2) A. Row的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #484 (Div. 2)
Codeforces Round #484 (Div. 2)
Codeforces Round #484 (Div. 2) A. Row
Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)
Codeforces Round #484 (Div. 2) B. Bus of Characters(STL+贪心)982B