【ATcode】 Chocolate Bar(暴力)

题目链接

题目大意:现在给你一块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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值