1. 程式人生 > >牛客網暑期ACM多校訓練營(第二場) I Car 思維

牛客網暑期ACM多校訓練營(第二場) I Car 思維

sizeof from rom src meet pbo nes eno rec

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

備註:

技術分享圖片

分析:首先看沒有陷阱的時候,n*n最多可以在邊界上放幾輛車,然後再思考加每個陷阱被影響到的行和列。

通過爆搜或者手寫所有情況找規律,我們很容易得出n*n的時候邊界放的車的奇數的情況為:(n/2)*4+1,偶數的情況為:2*n

每次加陷阱會影響到陷阱所在的行和列,我們分別用兩個vis數組保存起來被影響到的行和列。

首先看n為偶數

技術分享圖片

圖為n是偶數的時候,此時n為4,最多可以放車子八輛,八輛車的情況我們可以變成圖中這種情況(比如(2,2)我可以直接橫移到(2,1)邊界處就滿足了條件)

然後我們可以發現陷阱處於此圖中任何位置都會影響到兩輛車,所以n為偶數時候陷阱的位置行和列都將受到影響,我們記錄下來就好

接著是n為奇數

技術分享圖片

圖為n為5的時候,最多可以放九輛車,如上面一樣可以畫成這樣的形式。然後我們很容易看出來除了陷阱位於中心點,在其他點都可以影響到兩輛車,中心點影響到一輛車。

下面是我的實現代碼

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 10000007;
typedef long long ll;
int vis1[maxn] ;
int vis2[maxn] ;
struct node{
     int x ;
     int y ;
     friend bool operator < ( node a , node b ) {
         if(a.x != b.x)
         return a.x <= b.x ;
         return a.y <= b.y ;
     }
}e[maxn];
int n , m ;
int main(){
    while( scanf("%d %d",&n,&m) != EOF) {
        memset(vis1,0,sizeof(vis1)) ;
        memset(vis2,0,sizeof(vis2)) ;
        long long int sum = n%2 == 0 ? 2*n : (n/2)*4 + 1 ;
        for( int i = 1 ; i <= m ; i ++ ){
            scanf("%d %d",&e[i].x,&e[i].y) ;
            vis1[e[i].x] = 1 ; vis2[e[i].y] = 1 ;
        }
        long long ans = 0 ;
        for(int i = 1 ; i <= n ; i ++) {
            if(vis1[i]) ans ++ ;
            if(vis2[i]) ans ++ ;
        }
        if( n%2 && ( vis1[(n + 1)/2] || vis2[(n + 1)/2] ) ) {
            ans -- ;
        }
        printf("%lld\n",sum - ans) ;
    }
    return 0 ;
}

  

牛客網暑期ACM多校訓練營(第二場) I Car 思維