Alex is a driver of a shuttle bus whose working duty is to drive around Byteland and let the tourists do sightseeing there.
The territory of Byteland is strange which can be represent by an grid with exactly 2 rows and N columns. There are M churches on some cells in Byteland where sightseeing there are forbidden. On the other hand, there is an attraction in each of the remaining cells.
On each day, Alex drives the shuttle bus from the frontier of Byteland, which is the top-left corner of the 2 × N grid. The shuttle bus can travel from one cell to its adjacent cells which have a common side with it each time. Alex will drive the shuttle bus to visit all attractions. Undoubtedly, he cannot drive into the cells where the churches are located.
Alex does not want to make his tourists bored, so he hopes to visit all attractions, except the churches, exactly once. The tour can end in any cell. Given the length of the grid and the positions of the churches, determine whether Alex can do so successfully.
Input
The first line of contains 2 integers N, M, representing the length of the grid of Byteland and the number of churches there. (1 ≤ N ≤ 109, 1 ≤ M ≤ 5000)
The following M lines contains 2 integers ri, ci, representing the position of the ith church. (1 ≤ ri ≤ 2, 1 ≤ ci ≤ N)
The positions of the churches are distinct and no church will be located at the top-left corner of the grid.
Output
Please output Yes if Alex can visit all attractions except the churches exactly once and output No otherwise.
Examples
Input
5 3 2 1 1 3 2 5Output
YesInput
3 2 2 1 2 3Output
NoInput
3 2 1 2 2 2Output
No
其实算是模拟?一般状态只要考虑奇偶就可以了,因为只有两行,每次走一行上下会切换。
以下几种情况超级坑
我的处理方法是把结尾的上下皆黑的方块全部删除这样只用考虑中间有上下皆黑和遍历方向堵死两种出不来情况
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 10005
using namespace std;
struct church
{
int r,c;
}c[maxn];
bool cmp(church a,church b)
{
if(a.c==b.c)return a.r<b.r;
else return a.c<b.c;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n,m,pre;
cin>>n>>m;
memset(c,0,sizeof(c));
for(int i=0;i<m;i++)
{
cin>>c[i].r>>c[i].c;
}
sort(c,c+m,cmp);
for(int i=m-1;;i--)
{
if(c[i].c==c[i-1].c&&c[i].c==n)
{
n--;
i--;
continue;
}
else
{
break;
}
}
int flag=1,cnt=0;
for(int i=1;i<=n;i++)
{
if(c[cnt].c!=i)
{
flag=((flag==1)?2:1);
}
else if(flag==c[cnt].r)
{
flag=3;
break;
}
else if(c[cnt+1].c==i)
{
flag=3;
break;
}
else cnt++;
}
if(flag==3)cout<<"No"<<endl;
else cout<<"Yes"<<endl;
return 0;
}