1. 程式人生 > 實用技巧 >CF1393A Rainbow Dash, Fluttershy and Chess Coloring

CF1393A Rainbow Dash, Fluttershy and Chess Coloring

思路

也只有我會來發這種水題了吧.

給定一個\(n\times n\)的方陣,每個格子上可以放兩種顏色,每次只能在已經填好色的方格的四周(上下左右)填色,求使這個方陣變成顏色相間的棋盤的最小運算元。

結論題。

手推一下可以發現:

  1. \(n\)為奇數時,\(n+2\)的情況是\(n\)的情況的次數加\(1\)
  2. \(n+1\)的情況不會超過\(n+2\)的情況

又因為\(n=1\)時,次數為\(1\)

\(n=2,3\)時次數為\(2\)

\(n=4,5\)時次數為\(3\)……

可以大膽的猜測答案就是\(\dfrac{\lfloor n+2\rfloor}{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 << '\n';
  }
}