码迷,mamicode.com
首页 > 其他好文 > 详细

Chocolate Bar

时间:2018-07-10 21:28:44      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:sample   lse   span   pre   ace   nbsp   sam   输入   block   

题目描述

There is a bar of chocolate with a height of H blocks and a width of W blocks. Snuke is dividing this bar into exactly three pieces. He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.
Snuke is trying to divide the bar as evenly as possible. More specifically, he is trying to minimize Smax - Smin, where Smax is the area (the number of blocks contained) of the largest piece, and Smin is the area of the smallest piece. Find the minimum possible value of Smax?Smin.

Constraints
2≤H,W≤105

 

输入

Input is given from Standard Input in the following format:
H W

 

输出

Print the minimum possible value of Smax?Smin.

 

样例输入

3 5

 

样例输出

0

 

提示

In the division below, Smax?Smin=5?5=0.

技术分享图片
提示:
一开始想的有点多,仔细想想规律就是先最大化的分成三分之一,然后在分成二分之一,从m和n两个不同的角度分,然后记录x,y,z然后取其中最大最小值做差即可;
技术分享图片
#include <bits/stdc++.h>
#define ll long long
const int mod=1e9+7;
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ll i,j,k,m,n;
    ll ans=9999999999,cnt,x,y,z;
    ll a,b;
    ll minn,maxn;
    cin>>m>>n;
    a=m,b=n;
    if(m%3==0||n%3==0)
    {
        cout<<0<<endl;
        return 0;
    }
    for(i=0; i<2; i++)
    {
        m=a;
        n=b;
        if(i==0&&m!=1)
        {
            x=(m+1)/3*n;
            m-=(m+1)/3;
        }
        else if(i==1&&n!=1)
        {
            x=((n+1)/3)*m;
            n-=(n+1)/3;
        }
        for(j=0; j<2; j++)
        {
            if(j==0&&n!=1)
            {
                y=m*(n/2);
                z=m*(n-n/2);
            }
            else if(j==1&&m!=1)
            {
                y=n*(m/2);
                z=n*(m-m/2);
            }
            maxn=max(x,max(y,z));
            minn=min(x,min(y,z));
            ans=min(ans,maxn-minn);
        }
    }
    cout<<ans<<endl;
    return 0;
}
View Code

 

Chocolate Bar

标签:sample   lse   span   pre   ace   nbsp   sam   输入   block   

原文地址:https://www.cnblogs.com/nublity/p/9291137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!