时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述 

White Cloud has a square of n*n from (1,1) to (n,n).
White Rabbit wants to put in several cars. Each car will start moving at the same time and move from one side of one row or one line to the other. All cars have the same speed. If two cars arrive at the same time and the same position in a grid or meet in a straight line, both cars will be damaged.
White Cloud will destroy the square m times. In each step White Cloud will destroy one grid of the square(It will break all m grids before cars start).Any car will break when it enters a damaged grid.

White Rabbit wants to know the maximum number of cars that can be put into to ensure that there is a way that allows all cars to perform their entire journey without damage.

(update: all cars should start at the edge of the square and go towards another side, cars which start at the corner can choose either of the two directions)

 

For example, in a 5*5 square

 

legal

 

illegal(These two cars will collide at (4,4))

 

illegal (One car will go into a damaged grid)

输入描述:

The first line of input contains two integers n and m(n <= 100000,m <= 100000)
For the next m lines,each line contains two integers x,y(1 <= x,y <= n), denoting the grid which is damaged by White Cloud.

输出描述:

Print a number,denoting the maximum number of cars White Rabbit can put into.

示例1

输入

复制

2 0

输出

复制

4

备注:

 

 思路:

先考虑n为偶数的情况,将矩形分为4个区域,每辆车的方向如备注所示,这样可以保证互不相撞,且显然是最优的。

奇数时,中间的那一行和那一列会冲突,只能放一辆车。

有障碍时,只需要移除障碍影响的区域的车辆即可,可以发现是最优的。

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. #define MAXN 100005
  7. int n,m,row[MAXN],col[MAXN];
  8. int main()
  9. {
  10. scanf("%d%d",&n,&m);
  11. if(n==1)
  12. {
  13. if(m==0) printf("1\n");
  14. else printf("0\n");
  15. exit(0);
  16. }
  17. memset(row,0,sizeof row);
  18. memset(col,0,sizeof col);
  19. while(m--)
  20. {
  21. int x,y;
  22. scanf("%d%d",&y,&x);
  23. row[y]=1;
  24. col[x]=1;
  25. }
  26. int ans=0;
  27. for(int i=1;i<=n;i++)
  28. ans+=!row[i]+!col[i];
  29. if(n%2 && row[n/2+1]==0 && col[n/2+1]==0) ans--;
  30. printf("%d\n",ans);
  31. return 0;
  32. }