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.


Sample Input 2

4 5

Sample Output 2

2

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


Sample Input 3

5 5

Sample Output 3

4

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


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 }

时间: 2024-11-23 22:37:46

Chocolate Bar的相关文章

codeforces 598E E. Chocolate Bar(区间dp)

题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you ma

CodeForces 598E Chocolate Bar

区间DP预处理. dp[i][j][k]表示大小为i*j的巧克力块,切出k块的最小代价. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const long long INF=9999999999999999; const int maxn=60; long long dp[maxn][maxn][maxn]; int n

CodeForces Round 279 D Chocolate

Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a1 × b1 segments large and the second one is a2 × b2 segments large. Polycarpus wants to give Paras

codeforces 490 D Chocolate

题意:给出a1*b1和a2*b2两块巧克力,每次可以将这四个数中的随意一个数乘以1/2或者2/3,前提是要可以被2或者3整除,要求最小的次数让a1*b1=a2*b2,并求出这四个数最后的大小. 做法:非常显然仅仅跟2跟3有关.所以s1=a1*b1,s2=a2*b2,s1/=gcd(s1,s2),s2/=gcd(s1,s2),然后若s1跟s2的质因子都是2跟3,那么就有解.之后暴力乱搞就好了. #include<map> #include<string> #include<cs

codeforces 555 C Case of Chocolate

一开始题目读错了,还以为可以从任意点为起点向上向左吃. 其实是只能从右边的边界为起点吃. 于是很明显,每一个横坐标最多只能出现一次,否则肯定是当前这个起点的巧克力已经被啃食了. 想到这里就更明显了,对于(xi,n+1-xi),若是向上吃,能够影响它的操作(xj,n+1-xj)肯定满足xj>xi,然后又明显一点,最小的xj肯定能影响到它. 我们来考虑操作(xj,n+1-xj), 若它是往左吃,很显然此时操作(xi,n+1-xi)能吃到被(xj,n+1-xj)吃掉的地方为止. 若它是往上吃呢?很显然

Sharing Chocolate - UVa 1099 状压dp

Chocolate in its many forms is enjoyed by millions of people around the world every day. It is a truly universal candy available in virtually every country around the world. You find that the only thing better than eating chocolate is to share it wit

Codeforces Round #257 (Div. 2)449A - Jzzhu and Chocolate(贪心、数学)

题目链接:http://codeforces.com/problemset/problem/449/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

URAL 1639 Chocolate 2 (博弈论)

1639. Chocolate 2 Time limit: 1.0 second Memory limit: 64 MB Little Boy is mad at Karlsson, who ate all the sweets in the flat and even went to the neighbours to eat their sweets too. Now Little Boy's parents brought home a chocolate bar, and, sure e

(CF#257)C. Jzzhu and Chocolate

Jzzhu has a big rectangular chocolate bar that consists of n?×?m unit squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements: each cut should be straight (horizontal or vertical); each cut should go along edg