USACO 1.4 Packing Rectangles

51 篇文章 0 订阅

我也是醉了! 早知道直接看英文原文了,看了一个版本的翻译漏了2个核心信息:1 矩阵可以旋转  2:输出的p q有 p<=q,我一直在想,这明明会有 13 15 /15 13 这样的两个答案,为啥只有一个答案…… 还是老老实实看原文好,这些不知道谁翻译的东西真是气死我了。

程序虽然长,但是挺无脑的,就是模拟。 而且仔细看应该看的出,结构都是一样的

/*
TASK:packrec
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

struct point
{
	int x, y;
	point(int a = 0, int b = 0)
	{
		if (a > b)	swap(a, b);
		x = a;
		y = b;
	}
}outputbuff[100],a[4];
int t= 0, ans=0x7fffffff;

map<point, int>G;

bool operator < (point A, point B)
{
	if (A.x != B.x)	return A.x < B.x;
	return A.y < B.y;
}

int max(int A, int B,int C,int D)
{
	return max(max(A, B), max(C, D));
}

int max(int A, int B,int C)
{
	return max(max(A, B), C);
}

void f1(point A, point B, point C, point D)
{
	int w = A.x + B.x + C.x + D.x;
	int h = max(A.y, B.y, C.y, D.y);
	if (w * h < ans)	
	{
		ans = w * h;
		G.clear();
	}
	if (w * h == ans)	G[point(w, h)] = 1;	
}

void f2(point A, point B, point C, point D)
{
	int w = max(A.x, B.x + C.x + D.x);
	int h = max(B.y, C.y, D.y) + A.y;
	if (w * h < ans)	
	{
		ans = w * h;
		G.clear();
	}
	if (w * h == ans)	G[point(w, h)] = 1;	
}

void f3(point A, point B, point C, point D)
{
	int w = max(A.x, B.x + C.x) + D.x;
	int h = max(max(B.y, C.y) + A.y, D.y);
	if (w * h < ans)	
	{
		ans = w * h;
		G.clear();
	}
	if (w * h == ans)	G[point(w, h)] = 1;	
}

void f4(point A, point B, point C, point D)
{
	if (B.x < C.x)	return;
	int w = A.x + B.x + D.x;
	int h = max(A.y, B.y + C.y, D.y);
	if (w * h < ans)	
	{
		ans = w * h;
		G.clear();
	}
	if (w * h == ans)	G[point(w, h)] = 1;	

}

void f6(point A, point B, point C, point D)
{
	int w, h;
	if (B.x > A.x)	return;
	w = A.x + C.x;
	if (D.x < C.x)
	{
		h = max(A.y + B.y, C.y + D.y);	
		if (w * h < ans)	
		{
			ans = w * h;
			G.clear();
		}
		if (w * h == ans)	G[point(w, h)] = 1;	
		return ;
	}
	if (C.y < A.y)	return;
	if (B.x + D.x > A.x + C.x)
	{
		if (A.y+B.y > C.y)	return;
		w = max(A.x + C.x, D.x);
		h = C.y + D.y;
		if (w * h < ans)	
		{
			ans = w * h;
			G.clear();
		}
		if (w * h == ans)	G[point(w, h)] = 1;	
		return ;
	}

	h = max(A.y + B.y, C.y + D.y);	
	if (w * h < ans)	
	{
		ans = w * h;
		G.clear();
	}
	if (w * h == ans)	G[point(w, h)] = 1;	
	return ;
}


int vis[4] = {0};
int p[4];

void dfs(int deep)
{
	if (deep == 4)
	{
		f1(a[p[0]], a[p[1]], a[p[2]], a[p[3]]);	
		f2(a[p[0]], a[p[1]], a[p[2]], a[p[3]]);	
		f3(a[p[0]], a[p[1]], a[p[2]], a[p[3]]);	
		f4(a[p[0]], a[p[1]], a[p[2]], a[p[3]]);	
		f6(a[p[0]], a[p[1]], a[p[2]], a[p[3]]);	
		return ;
	}
	for (int i = 0; i != 4; ++ i)
	{
		if (vis[i])	continue;
		vis[i] = 1;
		p[deep] = i;
		dfs(deep + 1);
		swap(a[i].x, a[i].y);
		dfs(deep + 1);
		swap(a[i].x, a[i].y);
		vis[i] = 0;
		p[deep] = 0;
	}
}


int main()
{
	freopen("packrec.in", "r", stdin);
	freopen("packrec.out", "w", stdout);
	for (int i = 0; i != 4; ++ i)	cin>>a[i].x >> a[i].y;
	dfs(0);
	cout << ans<< endl;
	for (map<point, int>::iterator it = G.begin(); it != G.end(); ++ it)
		cout<<it -> first.x<<" "<< it -> first.y <<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值