AtCoder - 2565 枚举+贪心

探讨了如何将一块矩形巧克力条均匀分成三块,使各块面积之差最小,提出了一种通过枚举切割位置并贪心选择剩余部分中间区域的算法。

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.

2a9b2ef47b750c0b7ba3e865d4fb4203.png
Sample Input 2
4 5
Sample Output 2
2

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

a42aae7aaaadc4640ac5cdf88684d913.png
Sample Input 3
5 5
Sample Output 3
4

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

eb0ad0cb3185b7ae418e21c472ff7f26.png
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;
}

 


转载于:https://www.cnblogs.com/zxyqzy/p/10319198.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值