Topic Link: Click to open the link
Test instructions: gives the ground radius and height of n cylinders, requires only one to be placed directly on the table, the other to be placed on top of him, the first I can be placed on the top of the condition is: when and only if the volume of the first I is greater than J and J < I. The maximum volume to be folded up.
Idea: A look is a DP, and the state is very easy to say, D[i] to the first I can get the largest total volume. Transfer to Max (D[j]) + A[i], (J < i && A[i] > A[j]). But n is very large, obviously to be optimized, because the second layer of the loop is to find a maximum dp[j of a[j] > A[i] before I do it. So in order to meet two conditions at the same time, one of the conditions (each volume) as a line tree subscript, disguised maintenance of a condition, and because the small mark must be an integer, and the volume is too large, we think of discretization. Then the problem is very simple, discretization of each volume, using volume as a line segment tree subscript, maintain an interval maximum, this value is already added in the dp[j].
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long long ll;typedef long double ld;const ld EPS = 1e-9, PI = 3.14159265358979323846264 33832795;const int mod = 1000000000 + 7;const int INF = int (1e9); Const LL INF64 = LL (1e18); const int MAXN = 100000 + 10;in T T,n,m;ll d[maxn],b[maxn];ll maxv[maxn<<2];struct Node {ll R, h, v;} a[maxn];void pushup (int o) {Maxv[o] = max (maxv[o<<1], maxv[o<<1|1]);} void build (int l, int r, int o) {int m = (L + r) >> 1; Maxv[o] = 0; if (L = = r) return; Build (L, M, o<<1); Build (M+1, R, o<<1|1);} void Update (int L, int R, int v, int l, int r, int o) {int m = (L + r) >> 1; if (l <= l && R <= R) {if (Maxv[o] < D[v]) {Maxv[o] = D[v]; } return; } if (l <= m) update (L, R, V, L, M, o<<1); if (M < R) Update (L, R, V, M+1, R, O<<1|1); Pushup (o);} ll query (int l, int r, int l, int r, int o) {int m = (L + R) >> 1; if (l <= l && R <= R) {return maxv[o]; } ll ans = 0; if (l <= m) ans = max (ans, query (L, R, L, M, o<<1)); if (M < r) ans = max (ans, query (L, R, M+1, R, O<<1|1)); Pushup (o); return ans;} int main () {scanf ("%d", &n); for (int i=1;i<=n;i++) {scanf ("%i64d%i64d", &a[i].r,&a[i].h); A[I].V = A[I].R * A[I].R * A[I].H; B[i] = A[I].V; } sort (b+1, b+1+n); int m = Unique (b+1, b+1+n)-b-1; Build (1, M, 1); ll cur = 0; for (int i=1;i<=n;i++) {int L = Lower_bound (b+1, B+1+m, A[I].V)-B; ll v; if (L > 1) v = query (1, L-1, 1, M, 1); else v = 0; D[i] = (v + a[i].v); Update (L, L, I, 1, M, 1); cur = max (cur, d[i]); } Double ans = PI * cur; printf ("%.8f\n", ans); return 0;}
Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake (segment tree + discretized optimized DP)