题目链接
题目大意:现在给你一块n*m的巧克力,然后让你把巧克力分成三块,还要要求这三块的最大值和最小值的差值是最小的,问你最小是多少。。。
思路:这个题目的话,一开始是想的这个题目是固定的,就是分半,先去一半后取一半的一班,但是总感觉不是很好或者是有些情况没有考虑到,看了数据范围,觉得写一个暴力的解法。
就拿我们枚举的h来说吧,我们先枚举一个高度,然后就是分成两种情况,就是全都横着切和横着一刀竖着一刀,然后第二刀的话就一定是在中间的了,都用这种方法枚举就可以做了这题目,
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const ll inf=1e11+10;
ll minn=inf;
ll h,w;
int main()
{
scanf("%lld%lld",&h,&w);
ll mins,maxs;
ll temp1,temp2,temp3;
ll pos;
for(int i=1;i<h;i++)
{
temp1=i*w;
pos=(h-i)/2;
temp2=pos*w;
temp3=(h-i-pos)*w;
mins=min(min(temp1,temp2),temp3);
maxs=max(max(temp1,temp2),temp3);
minn=min(minn,maxs-mins);
temp1=i*w;
pos=w/2;
temp2=(h-i)*pos;
temp3=(h-i)*(w-pos);
mins=min(min(temp1,temp2),temp3);
maxs=max(max(temp1,temp2),temp3);
minn=min(minn,maxs-mins);
}
for(int i=1;i<w;i++)
{
temp1=i*h;
pos=(w-i)/2;
temp2=pos*h;
temp3=(w-i-pos)*h;
mins=min(min(temp1,temp2),temp3);
maxs=max(max(temp1,temp2),temp3);
minn=min(minn,maxs-mins);
temp1=i*h;
pos=h/2;
temp2=(w-i)*pos;
temp3=(w-i)*(h-pos);
mins=min(min(temp1,temp2),temp3);
maxs=max(max(temp1,temp2),temp3);
minn=min(minn,maxs-mins);
}
printf("%lld\n",minn);
}