目录
题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少。
析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选,如果大,那么数量少,如果小,数量就多,
用一个multiset来排序,这样时间复杂度会低一点,每一个都算一下比它的大矩阵的数量,然后算体积,不断更新,最大值。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步