我宣布我手机报废了… 这道枚举是IOI95的题目。虽说是枚举但是情况要考虑5种清楚(图示中的4,5图是同样的情况),尤其是第6图的一系列if-else非常非常叫我恶心! 【packing rectangles】 /* ID: wanqia6 TASK: packrec LANG: C++ */ #include <fstream> using namespace std; int getmax(int a,int b,int c = -1,int d = -1) { if (b > a) a = b; if (c > a) a = c; if (d > a) a = d; return a; } void swapq(int &a,int &b) { int t = a; a = b; b = t; return; } ifstream infile; struct { int a,b; } rect[5],list[100000]; void initdata() { infile.open("packrec.in"); for (int i = 1; i <= 4; i++) infile >> rect[i].a >> rect[i].b; infile.close(); return; } const int CHART[24][4] = { {1,2,3,4},{1,2,4,3},{1,3,2,4},{1,3,4,2}, {1,4,2,3},{1,4,3,2},{2,1,3,4},{2,1,4,3}, {2,3,1,4},{2,3,4,1},{2,4,1,3},{2,4,3,1}, {3,1,2,4},{3,1,4,2},{3,2,1,4},{3,2,4,1}, {3,4,1,2},{3,4,2,1},{4,1,2,3},{4,1,3,2}, {4,2,1,3},{4,2,3,1},{4,3,1,2},{4,3,2,1} }; const int INF = 20000; int fa,fb,fc,fd,k,ans = INF,tot; void modify_ans(int len,int wid) { int tmp = len * wid; if (len > wid) swapq(len,wid); if (tmp == ans) { tot++; list[tot].a = len; list[tot].b = wid; } else if (tmp < ans) { ans = tmp; tot = 0; list[tot].a = len; list[tot].b = wid; } return; } void calc_sqr() { int xa,xb,xc,xd,ya,yb,yc,yd,len,wid; xa = fa == 0 ? rect[CHART[k][0]].a:rect[CHART[k][0]].b; ya = fa == 0 ? rect[CHART[k][0]].b:rect[CHART[k][0]].a; xb = fb == 0 ? rect[CHART[k][1]].a:rect[CHART[k][1]].b; yb = fb == 0 ? rect[CHART[k][1]].b:rect[CHART[k][1]].a; xc = fc == 0 ? rect[CHART[k][2]].a:rect[CHART[k][2]].b; yc = fc == 0 ? rect[CHART[k][2]].b:rect[CHART[k][2]].a; xd = fd == 0 ? rect[CHART[k][3]].a:rect[CHART[k][3]].b; yd = fd == 0 ? rect[CHART[k][3]].b:rect[CHART[k][3]].a; len = xa + xb + xc + xd; wid = getmax(ya,yb,yc,yd); modify_ans(len,wid); wid = getmax(ya + yc,yb + yd); if (yc == yd) len = getmax(xc + xd,xa + xb); else if ((yc > yd) && (yc < yb + yd)) len = getmax(xa + xb,xc + xb,xc + xd); else if ((yd > yc) && (yd < ya + yc)) len = getmax(xa + xb,xa + xd,xc + xd); else if (yc > yb + yd) len = getmax(xa,xb + xc,xd + xc); else if (yd > ya + yc) len = getmax(xb,xa + xd,xc +xd); modify_ans(len,wid); len = getmax(xa + xb + xc,xd); wid = yd + getmax(ya,yb,yc); modify_ans(len,wid); len = getmax(xa + xb,xc,xd); wid = yc + yd + getmax(ya,yb); modify_ans(len,wid); len = xa + getmax(xb + xc,xd); wid = getmax(ya,yd + getmax(yb,yc)); modify_ans(len,wid); return; } void solve() { for (fa = 0; fa <= 1; fa++) for (fb = 0; fb <= 1; fb++) for (fc = 0; fc <= 1; fc++) for (fd = 0; fd <= 1; fd++) for (k = 0; k < 24; k++) calc_sqr(); return; } ofstream outfile; void outitdata() { outfile.open("packrec.out"); for (int i = 0; i < tot; i++) for (int j = i + 1; j <= tot; j++) if (list[i].a > list[j].a) { swapq(list[i].a,list[j].a); swapq(list[i].b,list[j].b); } outfile << ans << endl; outfile << list[0].a << ' ' << list[0].b << endl; for (int i = 1; i <= tot; i++) if (list[i].a != list[i - 1].a) outfile << list[i].a << ' ' << list[i].b << endl; outfile.close(); return; } int main() { initdata(); solve(); outitdata(); return 0; }