Codeforces 629D Babaei and Birthday Cake

Description

As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party’s cake.

Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.

However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.

Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.

(1≤n≤100000)

(1≤ri,hi≤10000)

Solution

题目要求就是求一个LIS。

朴素DP的LIS复杂度为/(O(n^2)/)的,如果我们用平衡树优化一下的话就会变成/(O(nlogn)/)的啦!

Code

 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <cstdio>
#include <cmath>
const double pi = acos(-1);
int getint() {
	int r = 0; char c = getchar();
	for (; c < '0' || c > '9'; c = getchar());
	for (; '0' <= c && c <= '9'; c = getchar()) r=r*10-'0'+c;
	return r;
}
const int maxn = 100050;
const double inf = 1e10+7;
int n, r, h;
double v[maxn];
int root, son[maxn][2], fa[maxn], tot = 0;
double idx[maxn], dp[maxn], mx[maxn];
double max(double a, double b) { return a > b ? a : b; }
void init() {
	root = 1;
	tot = 2;
	son[1][0] = 0;
	son[1][1] = 2;
	dp[1] = 0;
	mx[1] = 0;
	fa[1] = 0;
	idx[1] = -inf;
	son[2][0] = 0;
	son[2][1] = 0;
	dp[2] = 0;
	mx[2] = 0;
	fa[2] = 1;
	idx[2] = inf;
	idx[0] = dp[0] = mx[0] = 0;
}
void pushup(int x) { if (x) mx[x] = max(dp[x], max(mx[son[x][0]], mx[son[x][1]])); }
int ws(int x) { if (x == son[fa[x]][0]) return 0; return 1; }
void rotate(int x) {
	int y = fa[x], a = ws(x), z = fa[y], b = ws(y);
	son[z][b] = x; fa[x] = z;
	son[y][a] = son[x][!a]; fa[son[x][!a]] = y;
	son[x][!a] = y; fa[y] = x;
	pushup(y);
	pushup(x);
}
void splay(int x, int pos = 0) {
	int y = fa[x], a = ws(x), z = fa[y], b = ws(y);
	while (y != pos) {
		if (z == pos) rotate(x);
		else {
			if (a == b) { rotate(y); rotate(x); }
			else { rotate(x); rotate(x); }
		}
		y = fa[x], a = ws(x), z = fa[y], b = ws(y);
	}
	if (pos == 0) root = x;
}
double ans = 0;
void ins(double key) {
	int now = root, lst;
	double dpn = key;
	while (now) {
		lst = now;
		if (idx[now] < key) {
			dpn = max(dpn, dp[now] + key);
			dpn = max(dpn, mx[son[now][0]] + key);
			now = son[now][1];
		} else {
			now = son[now][0];
		}
	}
	int np = ++tot;
	idx[np] = key;
	dp[np] = mx[np] = dpn;
	ans = max(ans, dpn);
	son[np][0] = son[np][1] = 0;
	if (idx[lst] < key) now = 1;
	else now = 0;
	son[lst][now] = np;
	fa[np] = lst;
	splay(np);
}
int main () {
	n = getint();
	for (int i = 0; i < n; ++i) {
		r = getint(); h = getint();
		v[i] = pi * r * r * h;
	}
	init();
	for (int i = 0; i < n; ++i) ins(v[i]);
	printf("%.5lf", ans);
}