Chocolate Bar(暴力)

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 Smax−Smin.

Constraints

  • 2≤H,W≤105

Input

Input is given from Standard Input in the following format:

H W

Output

Print the minimum possible value of Smax−Smin.


Sample Input 1

3 5

Sample Output 1

0

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

2a9b2ef47b750c0b7ba3e865d4fb4203.png

Sample Input 2

4 5

Sample Output 2

2

In the division below, Smax−Smin=8−6=2.

a42aae7aaaadc4640ac5cdf88684d913.png

Sample Input 3

5 5

Sample Output 3

4

In the division below, Smax−Smin=10−6=4.

eb0ad0cb3185b7ae418e21c472ff7f26.png

Sample Input 4

100000 2

Sample Output 4

 
1

Sample Input 5

100000 100000

Sample Output 5

50000

 

 

//问有一块 h*w 的木板,要恰好切成 3 份,且,边长为整数,问切出来的最大面积减最小面积的最小值是多少?

 

//竟然是一个暴力题,枚举所有切割情况

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define INF (1LL<<62)
 5 
 6 LL slv(LL x,LL y,LL s)
 7 {
 8     LL X = x/2,Y = y/2;
 9     return min (
10         max( max(abs(X*y-s),abs((x-X)*y-s)), abs(X*y-(x-X)*y) ),
11         max( max(abs(x*Y-s),abs((y-Y)*x-s)), abs(Y*x-(y-Y)*x) )
12     );
13 }
14 
15 int main()
16 {
17     LL h,w;
18     cin>>h>>w;
19     LL ans = INF;
20     for (int i=1;i<=h;i++)
21         ans = min (ans,slv(h-i,w,i*w));
22     for (int i=1;i<=w;i++)
23         ans = min (ans, slv(w-i,h,i*h));
24     cout<<ans<<endl;
25     return 0;
26 }
View Code

 

 

 

posted @ 2017-05-21 13:05  happy_codes  阅读(395)  评论(0编辑  收藏  举报