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
Print the minimum possible value of Smax?Smin.
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