codeforces-50A-A. Domino piling ( C && greedy && math )

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16542775/article/details/54980971
A. Domino piling
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:

1. Each domino completely covers two squares.

2. No two dominoes overlap.

3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.

Find the maximum number of dominoes, which can be placed under these restrictions.

Input

In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).

Output

Output one number — the maximal number of dominoes, which can be placed.

Examples
input
2 4
output
4
input
3 3
output
4
 
         
 
         
 
         
 
         
 
         
徵羽調_xiao賤的分析很好如下(原文地址:http://blog.csdn.net/xiao_x_miss/article/details/10233163):
 
         

分析:

         题意分析:有一个M*N的大矩形,和一个2*1的小矩形,问最多用多少小矩形覆盖大矩形。

       1、  若有大矩形一边是偶边则让小矩形的边长为2的一边与其平行,这样就可以把大矩形都覆盖。

                                 

                 这时所需的小矩形为 m*n/2;

       2、两个都是奇数边时

                        

              有1,可知:尽量让小矩形的偶数边与大矩形的偶数边平行。

               此时小矩形的个数为:(q2-1)/2*q1+(q1-1)/2; 化简为:(q1*q2-1)/2;  (由此可见开始的使用2与那条奇数平行都没关系)。

 
         
代码:
 
         
#include<iostream>
#include<cstdio>
using namespace std;
int main(){

    int n,m;
    scanf("%d%d",&n,&m);
    if(n%2!=0&&m%2!=0){             //两边都是奇数
        printf("%d\n",(m-1)/2*n+(n-1)/2);
    }else{
        printf("%d\n",m*n/2);
    }

    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_16542775/article/details/54980971
今日推荐