Princess Celestia, is an Alicorn pony, the former co-ruler of Equestria alongside her younger sister Princess Luna. Princess Celestia raises the sun and governs Equestria, while Princess Luna raises the moon and oversees the realm of dreams.
In early days, Luna felt overshadowed and ignored by the ponies of Equestria, who slept through her beautiful night sky. These negative emotions festered and eventually led her to be consumed by the darkness and power of the mythical creature known as Nightmare Moon.
The darkness was spreading, and could be transformed into the following model:
In an �×�n×m white 2D grid, there are several black grid points.
Every second, all white points that are 4-neighbors of at least two black points will turn black.
What is the minimum number of black points needed to turn the entire grid black in the end?
Input
The first line contains two integers, �n and �m (1≤�,�≤105)(1≤n,m≤105), representing the size of the grid.
Output
Output a single integer representing the minimum number of black points needed.
Sample 1
Inputcopy | Outputcopy |
---|---|
2 2 | 2 |
Note
One of the minimal placement schemes of the sample is shown below.
思维题:不要只考虑对角线,多画画试试就能发现规律了->_<-
#include<iostream>
using namespace std;
#define int long long
int N, M;
signed main()
{
cin >> N >> M;
if (N == 1 || M == 1)return cout << max(N, M) / 2 + 1, 0;
else if (N == M) return cout << N, 0;
else
(N & 1) || (M & 1) ? cout << (N / 2 + M / 2) + 1 : cout << (N / 2 + M / 2);
return 0;
}