noi.ac #289. 电梯(单调队列)
题意
Sol
傻叉的我以为给出的\(t\)是单调递增的,然后\(100\rightarrow0\)
首先可以按\(t\)排序,那么转移方程为
\(f[i] = min_{j=0}^{i-1}(max(t[i], f[j]) + 2 * max_{k=j+1}^i x[k])\)
不难发现,若\(i < j\)且\(x[i] < x[j]\),那么从\(i\)转移过来一定是不优的,一定是从\(i\)之前的某个位置转移过来。(f单增)
然后直接单调队列搞一搞就行了,
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define fi first
#define se second
#define LL long long
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
char buf[(1 << 22)], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 1e6 + 10;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, q[MAXN], val[MAXN], top;
LL f[MAXN];
Pair a[MAXN];
signed main() {
N = read();
for(int i = 1; i <= N; i++) a[i].fi = read(), a[i].se = read();
sort(a + 1, a + N + 1);
for(int i = 1; i <= N; i++) {
while(top && a[i].se > a[top].se) top--;
a[++top] = a[i];
}
memset(f, 0x7f, sizeof(f));
N = top; f[0] = 0;
int l = 1, r = 0, las = 0;
for(int i = 1; i <= N; i++) {
while(l <= r && a[i].fi >= f[q[l]]) las = q[l++];
if(las < i) chmin(f[i], a[i].fi + 2ll * a[las + 1].se);
if(l <= r) chmin(f[i], val[l]);
int cur = f[i] + 2ll * a[i + 1].se;
while(l <= r && cur <= val[r]) r--;
q[++r] = i; val[r] = cur;
}
cout << f[N];
return 0;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。