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

题目描述 
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

输入描述:
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.

输入
2 0
输出
4

来自:https://blog.csdn.net/weixin_41156591/article/details/81150959

题意:给出一个n阶方正,上面有m个坑,随后是m行,每行一个x,y表示该位置存在一个坑,现在问你最多能在该方正格子中最多放置多少量车,使得这些车开始行驶后不会发生相撞或者掉坑(当一辆车遇到边界就停下来了,且车的行驶速度都是每秒一格),还要求车只能安排在边界,并且行驶方向面向于该边界平行的边界.

思路:按照一次画出2*2,3*3,4*4,5*5的就得出看出角上,和边是按照怎么放才能最大,然后分析

 对于N为奇数的情况,我们无非就是要考虑下最中间那行和列的情况,因为其他的结果都和偶数的时候一样处理即可,对于最中间那行和列,存在3中情况: 

        情况一:这行和列都不存在坑,那么结果不变,就是2*N-1;

        情况二:这行或列存在一个有坑,可是我们在和偶数一样处理的时候会ans-=1,都是实际我们只需要选择没有坑的那条路放置车即可,因此答案不变还是2*N-1,所以我们需要ans++;

        情况三:这行且列都存在坑,那么我们不可能在此放置车,可是我们在和偶数一样处理的时候会ans-=2,我们需要ans++,表示该中间行和列都不存在放置车辆的情况.

         综合以上得到:当N为奇数的时候,若是行或列存在坑都需要ans++即可.

代码:
const int maxn=200010;
int n,m,k;
int a[maxn];
int c[maxn],f,d[maxn];
int ans,ct,cnt,tmp,flag;
int main()
{
    int T,cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;   flag=1; tmp=0;f=0;
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        if(n&1) ans=2*n-1;
        else ans=2*n;
        for(int i=0;i<m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            if(!c[x]&&!d[y])
            {ans-=2;
            if((n&1)&&(x==(n+1)/2||y==(n+1)/2))ans++;
            }
            else if(c[x]+d[y]==1) ans--;
            c[x]=1;
            d[y]=1;
        }
        if(n==1&&m){puts("0");continue;}
        else if(n==1){puts("1");continue;}
        else if(n<=0){puts("0");continue;}
        printf("%d\n",ans);
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/snayf/article/details/81161777