P6007 [USACO20JAN]Springboards G
[USACO20JAN]Springboards G
题目描述
Bessie 在一个仅允许沿平行于坐标轴方向移动的二维方阵中。她从点 ( 0 , 0 ) (0,0) (0,0) 出发,想要到达 ( N , N ) (N,N) (N,N)( 1 ≤ N ≤ 1 0 9 1 \leq N \leq 10^9 1≤N≤109)。为了帮助她达到目的,在方阵中有 P P P( 1 ≤ P ≤ 1 0 5 1 \leq P \leq 10^5 1≤P≤105)个跳板。每个跳板都有其固定的位置 ( x 1 , y 1 ) (x_1,y_1) (x1,y1),如果 Bessie 使用它,会落到点 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)。
Bessie 是一个过程导向的奶牛,所以她仅允许她自己向上或向右行走,从不向左或向下。类似地,每个跳板也设置为不向左或向下。Bessie 需要行走的距离至少是多少?
输入格式
输入的第一行包含两个空格分隔的整数 N N N 和 P P P。
以下 P P P 行每行包含四个整数 x 1 , y 1 , x 2 , y 2 x_1,y_1,x_2,y_2 x1,y1,x2,y2,其中 x 1 ≤ x 2 x_1 \leq x_2 x1≤x2 且 y 1 ≤ y 2 y_1 \leq y_2 y1≤y2。
所有跳板的位置和目标位置均不相同。
输出格式
输出一个整数,为 Bessie 到达点 ( N , N ) (N,N) (N,N) 需要行走的最小距离。
样例 #1
样例输入 #1
3 2
0 1 0 2
1 2 2 3
样例输出 #1
3
提示
样例解释
Bessie 的最佳路线为:
- Bessie 从 (0,0) 走到 (0,1)(1 单位距离)。
- Bessie 跳到 (0,2)。
- Bessie 从 (0,2) 走到 (1,2)(1 单位距离)。
- Bessie 跳到 (2,3)。
- Bessie 从 (2,3) 走到 (3,3)(1 单位距离)。
Bessie 总共走过的路程为 3 单位距离。
子任务
- 测试点 2 ∼ 5 2 \sim 5 2∼5 满足 P ≤ 1000 P \leq 1000 P≤1000。
- 测试点 6 ∼ 15 6 \sim 15 6∼15 没有额外限制。
分析
最优化问题,考虑DP
那么,这个空间明显只能开下一维数组,若我们设
f
[
i
]
f[i]
f[i]表示使用前
i
i
i个跳板且必须使用第
i
i
i个跳板,能够节省的最大距离(因为直接判断到底是多少比较困难,正难则反,考虑维护这玩意)
设木板
i
i
i在
(
x
i
,
1
,
y
i
,
1
)
(x_{i,1},y_{i,1})
(xi,1,yi,1),可以跳到
(
x
i
,
2
,
y
i
,
2
)
(x_{i,2},y_{i,2})
(xi,2,yi,2)
则考虑转移,先按
x
x
x为第一关键字
y
y
y为第二关键字排序,那么转移即为
f [ i ] = max { f [ j ] + s [ i ] } ( x j , 2 ≤ x i , 1 , y j , 2 ≤ y i , 1 ) f[i]=\max{\lbrace f[j]+s[i]\rbrace}(x_{j,2}\le x_{i,1},y_{j,2}\le y_{i,1}) f[i]=max{f[j]+s[i]}(xj,2≤xi,1,yj,2≤yi,1)
其中
s
[
i
]
=
x
i
,
2
+
y
i
,
2
−
x
i
,
1
−
x
i
,
2
s[i]=x_{i,2}+y_{i,2}-x_{i,1}-x_{i,2}
s[i]=xi,2+yi,2−xi,1−xi,2
考虑如何优化,我们发现,一个跳板可以拆成两个点分别统计影响,假设我们拆成三元组
(
1
,
x
1
,
y
1
)
,
(
−
1
,
x
2
,
y
2
)
(1,x_1,y_1),(-1,x_2,y_2)
(1,x1,y1),(−1,x2,y2)
那么因为排序满足了转移的第一维条件,而时间复杂度只能是 n log n n\log n nlogn级别左右,这启发我们考虑优化条件 y j , 2 ≤ y i , 1 y_{j,2}\le y_{i,1} yj,2≤yi,1
这个条件的维护扔进一只BIT就好了
那么算法流程为:
- 离散化坐标,统计 s s s数组,拆点
- 将 2 p 2p 2p个点排序
- DP,从小到大扫描各个点
(1). 当目前的点属于 1 1 1类型,此时我们统计这个点的id的答案,在树状数组中查询 ≤ y \le y ≤y的最大值即可
(2). 否则,将这个点的答案扔进树状数组的 y y y位置
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define int long long
#define scanf scanf_s
#define N 1000500
struct node {
int id, x, y, lz;
}a[N << 1];
int b[N << 1], c[N << 2], n, m, f[N], cnt, s[N], tot;
bool cmp1(node a, node b) {
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
#define lowbit(x) (x & - x)
void add(int x, int k) {
while (x <= cnt) {
c[x] = max(c[x], k);
x += lowbit(x);
}
}
int ask(int x) {
int ans = -0x3f3f3f3f;
while (x) {
ans = max(ans, c[x]);
x -= lowbit(x);
}
return ans;
}
void init() {
memset(c, 0, sizeof c);
memset(f, 0xcf, sizeof f);
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%lld%lld%lld%lld", &b[cnt + 1], &b[cnt + 2], &b[cnt + 3], &b[cnt + 4]);
cnt += 4;
a[++tot] = { i,b[cnt - 3],b[cnt - 2] ,1 };
a[++tot] = { i,b[cnt - 1],b[cnt] ,-1 };
s[i] = f[i] = b[cnt - 1] + b[cnt] - b[cnt - 2] - b[cnt - 3];
// printf("%lld\n",f[i]);
}
}
void lsh() {
sort(b + 1, b + cnt + 1);
cnt = unique(b + 1, b + cnt + 1) - b - 1;
for (int i = 1; i <= m << 1; i++) {
// printf("%d %d\n",a[i].x,a[i].y);
a[i].x = lower_bound(b + 1, b + cnt + 1, a[i].x) - b;
a[i].y = lower_bound(b + 1, b + cnt + 1, a[i].y) - b;
// printf("%d %d\n",a[i].x,a[i].y);
}
sort(a + 1, a + m + m + 1, cmp1);
}
void solve() {
for (int i = 1; i <= m << 1; i++) {
if (a[i].lz == 1) {
// puts("A");
f[a[i].id] = max(f[a[i].id], ask(a[i].y) + s[a[i].id]);
}
else {
// puts("B");
add(a[i].y, f[a[i].id]);
}
}
int ans = 0;
for (int i = 1; i <= m; i++)ans = max(ans, f[i]);
printf("%lld\n", n + n - ans);
}
signed main() {
// freopen("P6007_2.in","r",stdin);
init();
lsh();
solve();
}