牛客网暑期ACM多校训练营(第二场)I - car(思维)

链接:https://www.nowcoder.com/acm/contest/140/I
来源:牛客网
 

时间限制: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 的网格里,A 选择在边上防止汽车,汽车会沿着某一方向(行或列)前进,直到到达对面,两车如果同一时间到达同一格子会相撞,现在格子上被破坏了M个点,问,要使所有汽车都能到达对面,最多能放多少辆车。

 

思路:对于n为偶数个的时候,若没有被破坏的点。则可放n*2辆车。奇数时可放2*n-1辆,每次破坏一个点,则个点的行和列最开始就都能放了,所以我们标记一下,各使ans-- ,当n为奇数的时候有种特殊情况。特判即可(if(n%2==1&&x[(n/2)+1]==1&&y[(n/2+1)]==1))

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

const int MAXN = 1e5+10;
int n,m;
int x[MAXN],y[MAXN];
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        int u,v;
        for(int i=0;i<m;i++){
            scanf("%d%d",&u,&v);
            x[u] = 1;
            y[v] = 1;
        }
        int ans ;
        if(n%2==0)  ans = n*2;
        else ans = 2*n-1;
        for(int i=1;i<=n;i++){
            if(x[i]==1) ans--;
            if(y[i]==1) ans--;
        }
        if(n%2==1&&x[(n/2)+1]==1&&y[(n/2+1)]==1)
            ans++;
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值