欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2018牛客多校训练----car

程序员文章站 2024-02-27 12:49:09
...

链接: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

 

2018牛客多校训练----car

legal

 

2018牛客多校训练----car

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

 

2018牛客多校训练----car

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个格子被损坏,车开到这上面也将被损坏,两车相撞也会被损坏。问在不损坏车辆的情况下,这个矩阵最多放多少辆车。

思路:

  找规律,大胆猜测规律。

#include<bits/stdc++.h>
using namespace std;

const int N=1e5+100;

bool vis1[N],vis2[N];

int main()
{
    int n,m,i;
    scanf("%d%d",&n,&m);
    if(m==0)
    {
        printf("%d\n",(2*n-n%2));
        return 0;
    }
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        vis1[x]=1;
        vis2[y]=1;
    }
    int sum=0;
    for(i=1;i<=n;i++)
    {
        if(!vis1[i])
            sum++;
        if(!vis2[i])
            sum++;
    }
    if(n%2==1&&vis1[n/2+1]==0&&vis2[n/2+1]==0)
        sum--;
    printf("%d\n",sum);
    return 0;
}