Chocolate Bar

问题 L: Chocolate Bar

时间限制: 1 Sec  内存限制: 128 MB
提交: 54  解决: 19
[提交][状态][讨论版][命题人:admin]

题目描述

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.

 
 
//发现无论怎么分都会有一个贯穿的横条或者贯穿的竖条 然后再尽量将剩下的矩形均分
  遍历每一种情况
如例子:3  5      当i遍历h到1时 可以将剩下的2*5的矩形横着分成两个1*5的 或者两个竖着的2*2和2*3的
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
using namespace std;
#define LL long long
int main()
{
    LL h,w;
    scanf("%lld %lld",&h,&w);
    LL ans = INT_MAX;
    for(int i=1; i<=h; i++)
    {
        LL maxx,minx,maxy,miny;
        LL  a1 = i*w;
        LL b1 = ((h-i)/2)*w;
        LL c1 = h*w - a1-b1;
        maxx = max(a1,max(b1,c1));
        minx = min(a1,min(b1,c1));
        if(abs(maxx-minx)<ans)
            ans = abs(maxx-minx);
        //
        LL a2 = i*w;
        LL b2 = (h-i)*(w/2);
        LL c2 = h*w - a2-b2;
        maxy = max(a2,max(b2,c2));
        miny = min(a2,min(b2,c2));
        if(abs(maxy-miny)<ans)
            ans = abs(maxy-miny);
    }
    for(int i=1; i<=w; i++)
    {
        LL maxx,minx,maxy,miny;
        LL  a1 = h*i;
        LL b1 = ((w-i)/2)*h;
        LL c1 = h*w - a1-b1;
        maxx = max(a1,max(b1,c1));
        minx = min(a1,min(b1,c1));
        if(abs(maxx-minx)<ans)
            ans = abs(maxx-minx);
        //
        LL a2 = i*h;
        LL b2 = (w-i)*(h/2);
        LL c2 = h*w - a2-b2;
        maxy = max(a2,max(b2,c2));
        miny = min(a2,min(b2,c2));
        if(abs(maxy-miny)<ans)
            ans = abs(maxy-miny);
    }
    printf("%lld",ans);
}

 

posted @ 2018-06-14 22:02  zangzang  阅读(481)  评论(0编辑  收藏  举报