牛客网ACM多校第二场 I car(规律)
链接:https://www.nowcoder.com/acm/contest/140/I
来源:牛客网
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*n的格子,有m个路障,车辆遇到路障就会坏掉,车辆相遇也会坏掉,只能在边界放车,车的走向只能朝向对面,问最多能放多少辆车子
思路:规律题,假设不放路障,会发现当n为偶数时,能放2*n个车辆,当n为奇数时,能放2*n-1个车辆,每当放一个路障,在这一行和这一列的两个车都不能放了,但是有一点比较特殊,就是当n为奇数时,在正中间有路障时,只会影响一辆车
#include <stdio.h>
const int MAXN = 100005;
int col[MAXN],row[MAXN];
int main(void)
{
int n,m;
int x,y;
while(scanf("%d%d",&n,&m) != EOF) {
while(m--) {
scanf("%d%d",&x,&y);
row[x] = col[y] = 1;
}
int ans = 2 * n - (n % 2);
for(int i = 1; i <= n; i++) {
if(row[i] == 1) ans--;
if(col[i] == 1) ans--;
}
if(n % 2 && (col[n / 2 + 1] || row[n / 2 + 1])) ans++;
printf("%d\n",ans);
}
return 0;
}