A New Beginning题解

· · 题解

提供一种自认为比较简单的做法。

题意

从 (0,0) 每次向上或者向右走一步,可以选择在任意时刻花费与某个点切比雪夫距离的代价把这个点删除。最小化删除所有点的代价。

思路

首先转化成曼哈顿距离,(x + y, x - y)。

注意,我们此时的移动 :

比如(a,b) -> (a+1,b)。

就变成了曼哈顿坐标系下的。

向上变成了向右上,向右变成了向右下。 这样我们发现每次移动一定会导致横坐标 $+1$。 可以考虑 $dp$。 我们考虑对于 $(x,y)$ 我们什么时候删除这个点。感性理解是在走到横坐标相等的时候:为什么呢? 因为我们的每次移动横坐标都会 $+1$, 如果走到了横坐标相同的位置我们不删除,而是继续往后走,那么每走一次,横坐标的曼哈顿距离都会 $+1$,而纵坐标的曼哈顿距离就算缩小,每次也只能缩小 $1$, 所以肯定不优。 没走到横坐标相同就删除也是同理。 $f_{i,j}$表示到达第 $i$ 个点的横坐标,纵坐标为 $j$, 只考虑前i个点时的最小代价。 $f_{i,j} = min(f_{i-1,k}) + |j-ai| |k-j| \le d 考虑使用[Slope trick](https://www.cnblogs.com/cccomfy/p/17743031.html)。 剩下的其实和[这道题](https://www.luogu.com.cn/problem/AT_abc217_h)基本一样了。 相当于把左边的堆和右边的堆分别向左向右平移 $d$,谷底延长。打标机维护即可。 还有一个小点:就是在横坐标为奇数的列,我们的纵坐标也只能走到奇数。偶数同理。而我们绝对值函数的交点会不会不合法? 不会:因为转化后的点$(x+y,x-y)$横纵坐标奇偶性相同,是合法坐标。 ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar(); } return x * f; } const int N = 8e5 + 10; int n; struct Node { ll x, y; friend bool operator < (Node a, Node b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; } }a[N]; priority_queue<ll> q1; priority_queue<ll, vector<ll>, greater<ll> > q2; ll tg1, tg2; ll stk[N*3], top, ans; int main() { n = read(); for (int i = 1; i <= n; i++) { int x = read(), y = read(); a[i].x = x + y, a[i].y = x - y; } sort(a + 1, a + n + 1); q1.push(0); q2.push(0);//防止访问空 for (int i = 1; i <= n; i++) { ll d = a[i].x - a[i-1].x; tg1 -= d; tg2 += d; ll x = a[i].y; if (x < q1.top() + tg1) { ans += q1.top() + tg1 - x;//q1.top两次都是谷底 q1.push(x - tg1); q1.push(x - tg1); q2.push(q1.top() + tg1 - tg2); q1.pop(); } else { //如果x在谷底: q1.top~q2.top,谷底是不变的,因为谷底的增加的函数是|x-x| = 0 q2.push(x - tg2); q2.push(x - tg2); ans += x - (q2.top() + tg2); q1.push(q2.top() + tg2 - tg1); q2.pop(); } } //由于 x + y和x - y奇偶性相同,所以一定是奇数列只有奇数y ,所以没必要特判 cout << ans / 2; return 0; } ```