Solution
题目链接。
全场最优解题解
主要记录一下个人的做题路径以及想法。
- 我们不会选取一个地方采两次蘑菇。因为如果我们在一个位置采两次蘑菇,可以只保留最后一次在此处采蘑菇,答案不变。
- 设依次采蘑菇的位置的下标序列为 ,那么有 ,如果不满足这个式子,我们一定可以找到一对下标 满足 ,且 ,我们交换 和 之后,答案一定不劣。
所以我们可以按照蘑菇地可能的选择顺序排序,也就是 递增序。
不太一样的做法
接下来我发现了一个结论,假设我们在所有蘑菇地中选 块得到的最优选择集合为 ,那么选 块的最优集合就是在 中插入了一个元素。
反证:如果最集合不能是上述的方式得到的,那么一定存在转移是删除了 中的 个元素集合 ,并且加入了 个元素集合 。那么我们选取 ,,并且不存在 满足 ,我们设当前状态下的答案为 ,交换 所属的集合之后的答案为 ,如果 那么 集合就不是最优决策,和题设矛盾,如果 那么 集合就不是最优决策,我们可以把 在 集合中替换为 答案会更优。所以不存在这种假设。
那么其实问题就转化为了每次如何找到这个最优的元素。
显然,第一次的最优解就是 最大的那个元素,设这个元素下标为 ,接下来如果我们插入一个在 之前的元素 ,那么它对答案的贡献在原来 的基础上增加 ,如果插入一个在 之后的元素 ,那么它对答案的贡献在原来 的基础上加上 ,后面每次插入一个元素,都对未加入的元素加上 或者 即可。
这个复杂度是 。
考虑用平衡树优化这个过程,有区间加,区间加上自身并查询最大值操作,可以用 解决,复杂度 。
标准做法
考虑 做法。
先按照 升序排序。 我们可以设 表示考虑排序后的前 个蘑菇地,选了 块的最优答案。那么 ,这个转移方程就是在考虑 选或者不选。
考虑优化这个式子。 根据不太一样的做法中的结论,考虑前 个蘑菇地依次选 块蘑菇地,那么也一定是每次在原有的基础上增加一个,所以 满足 都不选第 块蘑菇地, 都选了第 块蘑菇地。 可以发现 是满足 的最小的 。 所以 可以通过平衡树二分得到。
我们可以写出 到 的转移。
这里其实已经可以写了,但是因为要维护等差数列,有些难写,而且抢不到最优解。所以一个自然的想法是维护 的差分,这样等差数列就成了一个常数。
我们设 那么有 的转移
这样就只要区间加,单点插入就可以了。
时间复杂度 。
代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <vector>
#include <set>
#include <cstring>
#include <unordered_map>
#include <bitset>
#include <cassert>
#include <random>
using namespace std;
int lastans;
const int maxn = 1 << 20;
char in[maxn],out[maxn],*p1=in,*p2=in,*p3=out;
#define getchar() (p1==p2&&(p2=(p1=in)+fread(in,1,maxn,stdin),p1==p2)?EOF:*p1++)
#define flush() (fwrite(out,1,p3-out,stdout))
#define putchar(x) (p3==out+maxn&&(flush(),p3=out),*p3++=(x))
template<typename type>
void read(type &x)
{
x = 0;
int f = 1;
char c = getchar();
while (c < 48 || c > 57)
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= 48 && c <= 57)
x = x * 10 + c - 48, c = getchar();
x *= f;
x ^= lastans;
}
template<typename type, typename ...T>
void read(type &x, T &...y)
{
read(x), read(y...);
}
template<typename type>
inline void write(type x,bool mode=1)//0为空格,1为换行
{
x < 0 ? x =- x, putchar('-') : 0;
static short Stack[50], top(0);
do Stack[++top]=x%10,x/=10; while(x);
while(top) putchar(Stack[top--]|48);
mode?putchar('\n'):putchar(' ');
}
#define ll long long
#define pll pair<long long, long long>
mt19937 ci(time(0));
const int N = 1000005;
const int INF = 0x3f3f3f3f;
int n, idx, rt;
pll p[N];
struct Node {
int ls, rs, sz, fix;
ll val, tag;
}t[N];
int create(ll v) {
t[++idx] = {0, 0, 1, (int)ci(), v, 0};
return idx;
}
void pushup(int u) {
t[u].sz = t[t[u].ls].sz + t[t[u].rs].sz + 1;
}
void oper(int u, ll x) {
t[u].val += x;
t[u].tag += x;
}
void pushdown(int u) {
if (t[u].tag == 0) return;
if (t[u].ls) oper(t[u].ls, t[u].tag);
if (t[u].rs) oper(t[u].rs, t[u].tag);
t[u].tag = 0;
}
void split(int u, int k, int &x, int &y) {
if (!u) {
x = y = 0;
return;
}
pushdown(u);
if (t[t[u].ls].sz < k) {
x = u;
split(t[u].rs, k - t[t[u].ls].sz - 1, t[x].rs, y);
} else {
y = u;
split(t[u].ls, k, x, t[y].ls);
}
pushup(u);
}
int merge(int x, int y) {
if (!x || !y) return x | y;
if (t[x].fix < t[y].fix) {
pushdown(x);
t[x].rs = merge(t[x].rs, y);
pushup(x);
return x;
} else {
pushdown(y);
t[y].ls = merge(x, t[y].ls);
pushup(y);
return y;
}
}
void binary(int u, pll now, int &x, int &y) {
if (!u) {
x = y = 0;
return;
}
pushdown(u);
if (t[u].val <= t[t[u].ls].sz * now.first + now.second) {
y = u;
binary(t[u].ls, now, x, t[y].ls);
} else {
x = u;
now.second += now.first * (t[t[u].ls].sz + 1);
binary(t[u].rs, now, t[x].rs, y);
}
pushup(u);
}
ll output(int u, ll sum) {
pushdown(u);
ll add = 0;
if (t[u].ls) add += output(t[u].ls, sum);
add += t[u].val;
write(sum + add);
if (t[u].rs) add += output(t[u].rs, sum + add);
return add;
}
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(p[i].first, p[i].second);
}
sort(p + 1, p + 1 + n);
for (int i = 1; i <= n; i++) {
int x, y;
binary(rt, p[i], x, y);
ll v = t[x].sz * p[i].first + p[i].second;
oper(y, p[i].first);
rt = merge(merge(x, create(v)), y);
}
output(rt, 0);
flush();
return 0;
}