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;
}
posted @ 2019-03-24 20:55  自为风月马前卒  阅读(399)  评论(0编辑  收藏  举报

Contact with me