USACO 1.4.1 Packing Rectangles

刚开始看到这题有点虚。。第6种情况实在是坑爹

看了些代码,最后决定舍弃别人的思路自己重新想,总算想清楚了,因为我取高度是max(左上+左下,右上+右下),宽度是max(左下+右下,左上+右上),所以重叠情况只能发生在左上和右下 或者 左下和右上。这样就清晰很多了。


代码实现我参考了nocow的第一个程序,那人写的真心漂亮。


一次AC,庆祝一下~!


/*
PROG:packrec
LANG:C++
*/

#include <cstdio>
#include <algorithm>
#include <cstring>

#define reset res_rec.x = res_rec.y = 0
using namespace std;

struct rectangle
{
   int x, y;
}rec[4], res_rec;

int res = ~0U>>1;
bool exist[101];

void update()
{
   if (res_rec.x * res_rec.y < res)
   {
       memset(exist, 0, sizeof(exist));
       res = res_rec.x * res_rec.y;
   }
   if (res_rec.x * res_rec.y == res)
   {
       exist[min(res_rec.x, res_rec.y)] = 1;
   }
}

void calc()
{
   //case 1
   reset;
   for (int i = 0;i < 4;++i)
   {
       res_rec.x += rec[i].x;
       res_rec.y = max(rec[i].y, res_rec.y);
   }
   update();

   //case2
   reset;
   for (int i = 0;i < 3;++i)
   {
       res_rec.x += rec[i].x;
       res_rec.y = max(rec[i].y, res_rec.y);
   }
   res_rec.x = max(res_rec.x, rec[3].x);
   res_rec.y += rec[3].y;
   update();

   //case3
   reset;
   res_rec.x = max(rec[0].x+rec[1].x, rec[2].x)+rec[3].x;
   res_rec.y = max(max(rec[0].y, rec[1].y)+rec[2].y, rec[3].y);
   update();

   //case4,5
   reset;
   res_rec.x = max(rec[0].x, rec[1].x)+rec[2].x+rec[3].x;
   res_rec.y = max(max(rec[0].y+rec[1].y, rec[2].y), rec[3].y);
   update();

   //case6
   reset;
   res_rec.x = max(rec[0].x+rec[1].x, rec[2].x+rec[3].x);
   res_rec.y = max(rec[0].y+rec[2].y, rec[1].y+rec[3].y);
   if (rec[0].x>rec[2].x && rec[0].y>rec[1].y &&
       rec[3].x>rec[1].x && rec[3].y>rec[2].y) res_rec.x = rec[0].x+rec[3].x;
   if (rec[1].x>rec[3].x && rec[1].y>rec[0].y &&
       rec[2].x>rec[0].x && rec[2].y>rec[3].y) res_rec.x = rec[1].x+rec[2].x;
   update();
}

void rotate(int k)
{
   if (k == 4) calc();
   else
   {
       rotate(k+1);
       swap(rec[k].x, rec[k].y);
       rotate(k+1);
       swap(rec[k].x, rec[k].y);
   }
}

void dfs(int k)
{
   if (k == 4) rotate(0);
   else
   for (int i = k;i < 4;++i)
   {
       swap(rec[i], rec[k]);
       dfs(k+1);
       swap(rec[i], rec[k]);
   }
}
int main()
{
   freopen("packrec.in", "r", stdin);
   freopen("packrec.out", "w", stdout);
   for (int i = 0;i < 4;++i) scanf("%d%d", &rec[i].x, &rec[i].y);
   dfs(0);
   printf("%d\n", res);
   for (int i = 1;i <= 100;++i)
   {
       if (exist[i])
       printf("%d %d\n", i, res/i);
   }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值