跳到正文

题解

CF629D 题解

Vitamin_B约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年6月26日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟
题目
CF629D

版本

  1. v1当前

思路

不难推出暴力 dp 式子:dpi=(max⁡j=1i−1dpj)+sidp_i=(\max\limits_{j=1}^{i-1}dp_j)+s_i,sis_i 表示第 ii 个蛋糕的体积。考虑优化,可以用树状数组快速求出 max⁡j=1i−1dpj\max\limits_{j=1}^{i-1}dp_j,但是要离散化。因为我懒,所以我换了种方法。

以体积为第一关键字从小到大排序,然后依次枚举数组,最后还是用树状数组解决(此时树状数组的下标代表原数组下标)。

注意事项

必须以下标从大到小作为第二关键字,否则你会和我一样 WA#37。

原因:当你出现两个体积相同的蛋糕时,如果你下标从小到大排序了,那你就可以把前一个蛋糕放到后一个上,但事实上是不可以这么做的。

代码

C++
# 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;
}
原文