【CF】-Chips(思维)

Chips

Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:

At least one of the chips at least once fell to the banned cell.
At least once two chips were on the same cell.
At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).
In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.

Input
The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct.

Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.

Output
Print a single integer — the maximum points Gerald can earn in this game.

Example
Input
3 1
2 2
Output
0
Input
3 0
Output
1
Input
4 3
3 1
3 2
3 3
Output
1
Note
In the first test the answer equals zero as we can’t put chips into the corner cells.

In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.

In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).

分析:
题目理解超级难。。。
有n*n的方格,只能在最外围的一圈上并且除了四个角落里的四个小方格放置碎片,在接下来的(n-1)分钟,每分钟移动碎片到相邻的一格,直到将此碎片移动到对面。
有三个条件:
(1)不能将碎片移动到禁止格里;
(2)两个碎片不能同时在同一格(比如,行数是奇数时,如果从左右两端点都开始移动碎片,那么会同时到达正中间的那个);
(3)两个碎片不能交换位置(比如,行数是偶数时,如果从左右两端都开始移动碎片,那么到中间两个的时候,两个再移动就是交换了);

由此可得出的解题思路就是:
被禁止格的所在行和所在列的两端的四个小方格不能放置碎片,并且,同一行的两端点只有其中一个能放置,同一列也是如此。
所以碎片最多可以放置(n-2)*2个,如果有禁止的方格,那么再相应的减去,所在行和列其中一端的两个。
n是奇数时,要特别注意最中间的一行和一列。

代码如下:

#include<cstdio>
#include<cstring> 
int a[1000+11][1000+11];
int b[1000+11];
int c[1000+11];
int main()
{

    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        //a[1][1]=a[1][n]=a[n][1]=a[n][n]=1;
        c[1]=c[n]=1;
        b[1]=b[n]=1;
        int num=2*n-4;
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[x][1]=a[x][n]=1;
            a[1][y]=a[n][y]=1;
            if(b[x]==0)
            {
                num--;
                b[x]++;
            }
            if(c[y]==0)
            {
                num--;
                c[y]++;
            }
        }
        if(n&1)
        {
            if(a[1][n/2+1]==0&&a[n/2+1][1]==0)
                num--;
        }
        printf("%d\n",num);
    }
    return 0;
}//理解清楚题意也并不难呢,就是不好理解哦 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值