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 SmaxSmin.

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 SmaxSmin.

Sample Input 1

3 5

Sample Output 1

0

In the division below, SmaxSmin=5−5=0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, SmaxSmin=8−6=2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, SmaxSmin=10−6=4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000

样例倒是十分良心;
我们枚举第一步切的情况;
然后贪心地切剩下部分的中间部分,分为横、竖两种情况;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int h, w;
ll minn = 99999999999;
int main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> h >> w;
for (ll i = 1; i < h; i++) {
ll tmps = i * w;
ll pos = (h - i) / 2;
ll tmps1 = (pos)*w;
// cout << ">>" << tmps1 << endl;
ll tmps2 = (h - i - pos)*w;
// cout << ">>"<<tmps2 << endl;
ll mins = min(min(tmps, tmps1), tmps2);
ll maxs = max(max(tmps, tmps2), tmps1);
minn = min(minn, maxs - mins); pos = w / 2;
tmps1 = pos * (h - i);
// cout << ">>" << tmps1 << endl;
tmps2 = (w - pos)*(h - i);
// cout << ">>" << tmps2 << endl;
mins = min(min(tmps, tmps1), tmps2);
maxs = max(max(tmps, tmps2), tmps1);
minn = min(minn, maxs - mins);
// cout << minn << endl;
}
for (ll i = 1; i < w; i++) {
ll tmps = i * h;
ll pos = (w - i) / 2;
ll tmps1 = (pos)*h;
ll tmps2 = (w - i - pos)*h;
ll mins = min(min(tmps, tmps1), tmps2);
ll maxs = max(max(tmps, tmps2), tmps1);
minn = min(minn, maxs - mins);
// cout << minn << endl;
pos = h / 2;
tmps1 = (w - i)*pos;
tmps2 = (w - i)*(h - pos);
mins = min(min(tmps, tmps1), tmps2);
maxs = max(max(tmps, tmps2), tmps1);
minn = min(minn, maxs - mins);
// cout << minn << endl;
}
cout << minn << endl;
return 0;
}

AtCoder - 2565 枚举+贪心的更多相关文章

  1. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. 51nod1625(枚举&贪心)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...

  3. 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...

  4. [BZOJ 1028] [JSOI2007] 麻将 【枚举+贪心判断】

    题目链接:BZOJ - 1028 题目分析 枚举听的是哪种牌,再枚举成对的是哪种牌,再贪心判断: 从1到n枚举每一种牌,如果这种牌的个数小于0,就返回不合法. 将这种牌的张数 % 3, 剩下的只能和 ...

  5. 【枚举+贪心】【TOJ3981】【ICPC Balloons】

    给你N种不同颜色气球,每种气球有个数目 count[i],给的同种颜色气球可能是L尺寸,或M尺寸. M个问题,每个问题有个解决人数ac[i]. 每个问题 要分配一种颜色的气球,尺寸要一样 现在 这些气 ...

  6. 【枚举+贪心】【ZOJ3715】【Kindergarten Electiond】

    题目大意: n 个人 在选取班长 1号十分想当班长,他已经知道其他人选择了谁,但他可以贿赂其他人改选他,问贿赂的最小值 ps.他自己也要投一个人 要处理一个问题是,他自己投谁 其实这个问题在这种局面下 ...

  7. FZU 2252 Yu-Gi-Oh!(枚举+贪心)

    Problem 2252 Yu-Gi-Oh! Accept: 105    Submit: 628 Time Limit: 1000 mSec    Memory Limit : 32768 KB   ...

  8. UVALive 6912 Prime Switch 暴力枚举+贪心

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  9. bzoj1050[HAOI2006]旅行comf(枚举+贪心+并查集)

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...

随机推荐

  1. 可变、不可变数据类型和hash

    一.可变和不可变数据类型 在python中,我们对数据类型除了分为数字类型.字符串类型.列表类型.元组类型.字典类型和集合类型外, 还有另外一种分类方式,我们给数据类型分为可变数据类型和不可变数据类型 ...

  2. C++中使用TCP传文件

    在两个文件中都定义文件头和用到的宏: #define MAX_SIZE 10 #define ONE_PAGE 4096 struct FileHead { ]; int size; }; 在客户端发 ...

  3. 【poj1679】The Unique MST

    [题目大意] 共T组数据,对于每组数据,给你一个n个点,m条边的图,设图的最小生成树为MST,次小生成树为ans,若MST=ans,输出Not Unique!,否则输出MST [题解] 很明确,先求M ...

  4. 生成ico格式图标

    ico格式可参考如下链接: http://msdn.microsoft.com/en-us/library/ms997538.aspx http://en.wikipedia.org/wiki/ICO ...

  5. 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况

    [抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...

  6. c语言实践 给三个数输出最大的那个数

    我是怎么想的,我前面学过两个数比大小,比如有三个数,a b c,先比较a和b的大小,然后用那个较大的和c比较就得出最大的那个了.这个求三个数比大小的问题最后变化成 了两个数比大小了. int main ...

  7. 第八课 ROS的空间描述和变换

    1.tf的实际应用 1)在机器人的配置中 从上面可以看出激光雷达中心距离机器人底座的中心有20cm,激光雷达的中心距机器人底座中心有10cm,如果激光雷达在障碍物前面0.3米,那么机器人底座离障碍物多 ...

  8. 黑盒测试实践-任务进度-Day02

    使用工具 selenium 小组成员 华同学.郭同学.穆同学.沈同学.覃同学.刘同学 任务进度 在经过了昨天的基本任务分配之后,今天大家就开始了各自的内容,以下是大家任务的进度情况汇总. 华同学(任务 ...

  9. Chrome浏览器控件安装方法

    说明:只需要安装up6.exe即可,up6.exe为插件集成安装包. 1.以管理员身份运行up6.exe.up6.exe中已经集成Chrome插件.  

  10. iOS编程——Objective-C KVO/KVC机制[转]

    这两天在看和这个相关的的内容,全部推翻重写一个版本,这是公司内做技术分享的文档总结,对结构.条理做了更清晰的调整.先找了段代码,理解下,网上看到最多的一段的关于KVC的代码 先上代码 1.     1 ...