USACO 1.4 Packing Rectangles

来源:互联网 发布:ins是什么社交软件 编辑:程序博客网 时间:2024/04/30 13:22

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

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

/*TASK:packrecLANG: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
原创粉丝点击