1. 程式人生 > >牛客網暑期ACM多校訓練營(第二場)car

牛客網暑期ACM多校訓練營(第二場)car

時間限制: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*n的方格里,我們要在每條邊上擺放小車,然後小車向另一邊移動,當兩個小車相遇在同一個格子裡或者走到事先被標記的點的話就會毀掉,問最多可以擺放幾個小車

思路:這個按照題目裡的畫法畫一畫就會發現如果n為偶數,最多放2*n個,奇數時2*n-1個,然後我們統計被破壞的行和列,若這一行有標記點則減一,列同理,但是要注意當n為奇數時,他的中間那一行或者列最多隻能放一個小車,所以按照演算法的統計,當n為奇數時,若中心點被標記,要加一,否則會與結果不符;

下面附上我的程式碼:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define lson o<<1
#define rson o<<1|1
using namespace std;
int n, m;
int a[100005], b[100005];
int main()
{
	int x, y;
	while(cin >> n >> m)
	{
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		int sum, x, y;
		if(n % 2)
			sum = 2 * n -1;
		else
			sum = 2 * n;
		while(m--)
		{
			scanf("%d %d", &x, &y);
			a[x] = 1;
			b[y] = 1;
		}
		for(int i = 1; i <= n; i++)
		{
			if(a[i])
				sum--;	
			if(b[i])
				sum--;
		}
		if(n % 2 && a[n / 2 + 1] == 1 && b[n / 2 + 1] == 1)
				sum++;
		printf("%d\n", sum);
	}
    return 0;
}