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

备注:

 
 

题意:

车只能在边上,速度相同,不能相撞,问最多多少辆车

分析:

找规律,普通位置有一个叉会少两个车,特殊少一个车,找规律

代码:

#include<bits/stdc++.h>
using namespace std;
int a[100005],b[100005];
int main()
{
    int n,m,i,j,x,y,sum;
    scanf("%d%d",&n,&m);
    if(m==0)
    {
        printf("%d\n",n*2-n%2);
        return 0;
    }
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    while(m--)
    {
        sum=0;
        scanf("%d%d",&x,&y);
        a[x]=1;
        b[y]=1;
    }
    for(i=1;i<=n;i++)
    {
        if(a[i]==0)
            sum++;
    }
    for(i=1;i<=n;i++)
    {
        if(b[i]==0)
            sum++;
    }
    if(n%2&&a[n/2+1]==0&&b[n/2+1]==0)
        sum--;
    printf("%d\n",sum);
}

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/81149648
car
今日推荐