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表示有人占位,人和人不能坐一起,且给出的序列没有其他人可以坐下了。
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define mem(a,b) memset(a,b,sizeof(a)) #define P pair< int , int > const int maxn = 100000+10; const int INF = 0x3f3f3f3f3f; char a[1005]; int main() { int n; scanf("%d",&n); scanf("%s",a+1); if(n==1) { if(a[1]=='0') printf("No\n"); else printf("Yes\n"); return 0; } if((a[1]=='0'&&a[2]=='0')||(a[n-1]=='0'&&a[n]=='0')) { printf("No\n"); return 0; } int cnt1=0,cnt2=0; for(int i=1;i<=n;i++) { if(a[i]=='0') cnt1++; else cnt1=0; if(a[i]=='1') cnt2++; else cnt2=0; if(cnt1>=3||cnt2>=2) { printf("No\n"); return 0; } } printf("Yes\n"); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define mem(a,b) memset(a,b,sizeof(a)) #define P pair< int , int > const int maxn = 100000+10; const int INF = 0x3f3f3f3f3f; int head[maxn],cnt=0,son[maxn]; int ans=0; struct node { int to,next; } p[maxn*200]; void add(int u,int v) { p[++cnt].next=head[u]; head[u]=cnt; p[cnt].to=v; } void dfs(int u,int f) { son[u]=1; for(int i=head[u]; i; i=p[i].next) { int v=p[i].to; if(v!=f) { dfs(v,u); if(son[v]>1&&son[v]%2==0) ans++; else son[u]+=son[v]; } } } int main() { int n,u,v; scanf("%d",&n); for(int i=1; i<n; i++) { scanf("%d %d",&u,&v); add(u,v); add(v,u); } if(n&1) { printf("-1\n"); return 0; } dfs(1,0); printf("%d\n",ans); }
PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~