牛客网暑期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的矩形内,有个小方格不能用,你可以在里面放小车,小车只能朝对面走,问最多能放多少车

思路:这个备注里暗藏杀鸡啊,一个任意的n*n的矩形,你都可以把它分为4块

⬇️ ⬇️ ⬅️ ⬅️
       
       
➡️ ➡️ ⬆️ ⬆️


所以无陷阱的矩形的小车数:n为奇数是ans = 2*n - 1,n为偶数时 ans = 2 * n这种情况,而如果n为奇数时,那么(n+1)/2这一行只能放一个

这样的话,矩形的每一行每一列都有小车,所以如果某一行被破坏或某一列别破坏,那么ans--

#include <iostream>
#include <string.h>
#include <string>
int main() {
    int n, m;
    std::cin >> n >> m;
    int ans = (n << 1);
    bool X[100005], Y[100005];
    memset(X, 0, sizeof(X));
    memset(Y, 0, sizeof(Y));
    for (int i = 1, x, y; i <= m; i ++) {
        std::cin >> x >> y;
        if (!X[x])
            ans --, X[x] = 1;
        if (!Y[y])
            ans --, Y[y] = 1;
    }
    if (n&1 && !X[(n + 1)/2] && !Y[(n + 1)/2]) ans --;
    std::cout << ans << std::endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_jizhideqingwa/article/details/81148658