AtCoder:Chocolate Bar(数学)

C - Chocolate Bar


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

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 SmaxSmin.

Constraints

  • 2H,W105

Input

Input is given from Standard Input in the following format:

H W

Output

Print the minimum possible value of SmaxSmin.


Sample Input 1

Copy
3 5

Sample Output 1

Copy
0

In the division below, SmaxSmin=55=0.

2a9b2ef47b750c0b7ba3e865d4fb4203.png

Sample Input 2

Copy
4 5

Sample Output 2

Copy
2

In the division below, SmaxSmin=86=2.

a42aae7aaaadc4640ac5cdf88684d913.png

Sample Input 3

Copy
5 5

Sample Output 3

Copy
4

In the division below, SmaxSmin=106=4.

eb0ad0cb3185b7ae418e21c472ff7f26.png

Sample Input 4

Copy
100000 2

Sample Output 4

Copy
1

Sample Input 5

Copy
100000 100000

Sample Output 5

Copy
50000
题意:将一个矩形分成三份,使得三份面积的极差最小,输出极差。

思路:割法无非就两种,平行割三刀,或者先割一刀分成两半1和2,再在其中一半横着割一刀分成1,3,4,那么比较这俩情况就行了,对于第二种情况,最小的矩形要么是1要么是3或4,再分这两种情况计算就行了。


# include <bits/stdc++.h>
using namespace std;
 
typedef long long LL;
int cal(int a, int b)
{
    int c = a/2;
    if((LL)a+c > (LL)b*c)
         return (int)((LL)a+c-(LL)b*c);
    int x = (int)ceil(b*c*1.0/(a+c));
    return max((a+c)*x-b*c, (b-x)*(a-2*c));
}
int cal2(int a, int b)
{
    int c = (int)ceil(a/2.0);
    if(a+c > (LL)b*c) return 1e9;
    int x = (LL)b*c/(c+a);
    return max((int)((LL)b*c-(LL)x*(a+c)), (int)((LL)(b-x)*(2*c-a)));
}
int cal3(int a, int b)
{
    double x = floor(b/3.0);
    double y = ceil(b/3.0);
    return (int)(y*a-x*a);
}
int main()
{
    int a, b;
    scanf("%d%d",&a,&b);
    int ans = min(cal(a, b), min(cal(b, a), min(cal2(a, b), min(cal2(b, a), min(cal3(a, b), cal3(b, a))))));
    printf("%d\n",ans);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值