noi.ac #289. 電梯(單調佇列)

自為風月馬前卒發表於2019-03-24

題意

題目連結

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;
}

相關文章