题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少。
析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选,如果大,那么数量少,如果小,数量就多,
用一个multiset来排序,这样时间复杂度会低一点,每一个都算一下比它的大矩阵的数量,然后算体积,不断更新,最大值。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <stack> using namespace std ; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 4e3 + 5; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; int n, m; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct node{ int x, y; bool operator < (const node &p) const{ return x > p.x || (x == p.x && y < p.y); } }; node a[maxn]; multiset<int> sets; int main(){ while(scanf("%d", &n) == 1){ for(int i = 0; i < n; ++i){ scanf("%d %d", &a[i].x, &a[i].y); if(a[i].x < a[i].y) swap(a[i].x, a[i].y); } sort(a, a+n); sets.clear(); int num = 0, ansx, ansy; LL ans = 0; for(int i = 0; i < n; ++i){ sets.insert(a[i].y); ++num; int cnt = 0; for(auto &it : sets){ LL tmp = (LL)a[i].x * (LL)it * (LL)(num-cnt); if(ans < tmp){ ans = tmp; ansx = a[i].x; ansy = it; } ++cnt; } } printf("%I64d\n", ans); printf("%d %d\n", ansx, ansy); } return 0; }
时间: 2024-10-12 16:48:59