题目链接:http://codeforces.com/problemset/problem/589/B
题意:题意有点说不清楚,大致意思是从上到下叠放了很多矩形蛋糕,求怎么切长度和宽度才能使切后的蛋糕体积最大。每块蛋糕的长宽可以颠换。
思路:既然每个举行的长宽可以颠换,那就使宽>长,这些矩形肯定是长边与长边对齐,短边与短边在一起时切才会保留最多的蛋糕,使切后蛋糕体积最大。第一次根据宽排序,排好序后遍历每一个宽w,将宽度大于等于w的宽所对应的高再次进行排序,对于每一个高h求出所有蛋糕在宽为w高为h时的总体积取最大值即可。总体思路是两次排序加暴力。看了学长学姐们的代码才看懂,不得不说,我 还差得远呢。未来,我会继续努力的。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<map> #include<vector> #include<cmath> #include<set> #include<queue> using namespace std; typedef long long ll; const int maxn = 4000 + 10; const int maxt = 1e6 + 10; const double eps = 0.00000000000001; const int inf = 0x3f3f3f3f; struct reg { int w,h; bool operator<(const reg& r) const { return w < r.w; } }r[maxn]; int main() { int n; while(~scanf("%d",&n)) { for(int i = 0; i < n; i++) { scanf("%d%d",&r[i].w, &r[i].h); if(r[i].w > r[i].h) swap(r[i].w, r[i].h);//保证每个矩形w > h } sort(r ,r + n); vector<int> hh; ll ans = 0, mxw, mxh; for(int i = 0; i < n; i++)//枚举所有的宽w { hh.clear(); for(int j = i; j < n; j++)//将第i至n个矩形的长储存在集合中 hh.push_back(r[j].h); sort(hh.begin(), hh.end());//将长排序 int len = hh.size(); for(int j = 0; j < hh.size(); ++j,--len) { ll tmp = (ll)r[i].w * hh[j] * len;//暴力所有宽,高对应的体积求最大值 if(tmp > ans) { ans = tmp; mxw = r[i].w; mxh = hh[j]; } } } printf("%I64d\n%I64d %I64d\n",ans, mxw, mxh); } return 0; }