Row

time limit            per test 
1 second                memory limit                  per test256 megabytes
input                  standard input                     output                            standard output

You're given a row with nn chairs. We call a seating of people "maximal" if the two following conditions hold:

  1. There are no neighbors adjacent to anyone seated.
  2. 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 n2n≠2).

Input

The first line contains a single integer nn (1n10001≤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

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).

Examples
input
3
101
output
Yes
input
4
1011
output
No
input
5
10001
output
No
Note

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;
}

猜你喜欢

转载自www.cnblogs.com/coder-tcm/p/9057268.html
Row