思路
也只有我会来发这种水题了吧.
给定一个(n imes n)的方阵,每个格子上可以放两种颜色,每次只能在已经填好色的方格的四周(上下左右)填色,求使这个方阵变成颜色相间的棋盘的最小操作数。
一道结论题。
手推一下可以发现:
- 当(n)为奇数时,(n+2)的情况是(n)的情况的次数加(1)
- (n+1)的情况不会超过(n+2)的情况
又因为:
- (n=1)时摆放的次数为(1),
- (n=2,3)时摆放的次数为(2),
- (n=4,5)时摆放的次数为(3)……
由此可以大胆的猜测答案就是(dfrac{lfloor n+2 floor}{2}),交上去测一发,发现的确是对的,在代码里即为(n/2+1)
那么我们就要想了,为什么是这样的呢?
其实就是因为在(n)的状态下进行一次摆放,尽可能摆放最多棋子的情况下,得到的就是(n-2)的情况下未摆放时的状态
以此类推,一直到(n=1,2)的情况,就可以一步步再倒着推回去了,这样的话这道题就做完了,最后给出代码。
代码
/*
Author:loceaner
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
int main() {
int T = read();
while (T--) {
int n = read();
cout << n / 2 + 1 << '
';
}
}