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

17 篇文章 0 订阅
12 篇文章 0 订阅
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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问。在这个问中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问: CodeForces - 982C 树形DP是什么问?如何解决? 回答: CodeForces - 982C是一个树形动态规划问。在这个问中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值