CF629D 题解
思路
不难推出暴力 dp 式子:
以体积为第一关键字从小到大排序,然后依次枚举数组,最后还是用树状数组解决(此时树状数组的下标代表原数组下标)。
注意事项
必须以下标从大到小作为第二关键字,否则你会和我一样 WA#37。
原因:当你出现两个体积相同的蛋糕时,如果你下标从小到大排序了,那你就可以把前一个蛋糕放到后一个上,但事实上是不可以这么做的。
代码
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const double pi = acos (-1); //众所周知 π=acos(-1)
struct node {
int id;
ll s;
bool operator < (const node& x) const {
return s != x.s ? s < x.s : id > x.id;
}
} a[100005];
int n, x, y;
ll maxx, s, tr[100005];
void add (int x, ll y) {
while (x <= n)
tr[x] = max (tr[x], y), x += x & -x;
return ;
}
ll find (int x) {
ll maxx = 0;
while (x)
maxx = max (maxx, tr[x]), x ^= x & -x;
return maxx;
}
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> x >> y, a[i] = {i, (ll) x * x * y};
sort (a + 1, a + 1 + n);
for (int i = 1; i <= n; ++ i) {
s = find (a[i].id - 1) + a[i].s;
maxx = max (maxx, s);
add (a[i].id, s);
}
cout << fixed << setprecision (10) << pi*maxx;
return 0;
}