题目描述
小H有n个碗需要放进橱柜,她希望将他们叠起来放置。你知道每个碗都是规则的圆柱体,并且都是上宽下窄,你已经测量出了每个碗的两个半径及高,请你帮小H找出一种叠放顺序,使得叠放出来的碗堆的高度尽量小,比如:
100%数据满足n < = 9。所有输入的数绝对值不超过1000。
输入描述:
第一行一个整数n,表示碗的数目。
以下n行,每行三个整数h,r1,r2。分别表示碗高及两个半径。其中r1<r2
输出描述:
仅一个数,表示最小的高度。答案四舍五入取整。
示例1
输入
3
50 30 80
35 25 70
40 10 90
输出
55
解题思路
#pragma GCC target("avx,sse2,sse3,sse4,popcnt") #pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math") #include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define mk(__x__,__y__) make_pair(__x__,__y__) #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x) { if (!x) { putchar('0'); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } inline int lowbit(int x) { return x & (-x); } const int N = 9 + 7; const double eps = 1e-6; double h[N], r1[N], r2[N], f[N]; int id[N]; double fun(int x) { //算梯形斜率的绝对值 return (r2[x] - r1[x]) / h[x]; } double calc(int x, int y) { if (r1[x] > r2[y]) return h[y]; if (r2[x] < r1[y]) return 0; if (fun(x) <= fun(y)) { if (r1[x] <= r1[y]) return 0; return h[y] * (r1[x] - r1[y]) / (r2[y] - r1[y]); } if (r2[x] > r2[y]) return max(0.0, h[y] - (r2[y] - r1[x]) / (r2[x] - r1[x]) * h[x]); return max(0.0, (r2[x] - r1[y]) / (r2[y] - r1[y]) * h[y] - h[x]); } int main() { int n = read(); for (int i = 1; i <= n; ++i) h[i] = read(), r1[i] = read(), r2[i] = read(), id[i] = i; double ans = 1e8, res = 0.0; do { res = 0.0; for (int i = 1; i <= n; ++i) { f[i] = 0; for (int j = 1; j < i; ++j) f[i] = max(f[i], f[j] + calc(id[i], id[j])); res = max(res, f[i] + h[id[i]]); } ans = min(ans, res); } while (next_permutation(id + 1, id + 1 + n)); //全排列枚举各个地方放什么碗 print(ans + 0.5); return 0; }