牛课第二次多校I

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

备注:

 题解

  如果没有被破坏的点 ans=n*2-(n%2);
  如果有则减去相应的被破坏的行和列,但是奇数行的最中间的那个只影响一个,(此处可以考虑n==3的,最中间的点之影响一个车的填入)
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=1e5+5;
 4 int a[MAXN],b[MAXN];
 5 int main()
 6 {
 7     int ans=0;
 8     int n,m;
 9     scanf("%d%d",&n,&m);
10     int x,y;
11     ans=n*2-(n%2);
12     for(int i=0;i<m;i++)
13     {
14         scanf("%d%d",&x,&y);
15         a[x]=1;
16         b[y]=1;
17     }
18     int k1=0,k2=0;
19     for(int i=1;i<n;i++)
20     {
21         if(a[i]==1 ) ans--;
22         if(b[i]==1) ans--;
23     }
24     if(n%2==1&&a[(n+1)/2]==1&&b[(n+1)/2]==1) ans++;
25     printf("%d\n",ans);
26     return 0;
27 }
View Code

猜你喜欢

转载自www.cnblogs.com/-xiangyang/p/9348387.html