欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

noi.ac #289. 电梯(单调队列)

程序员文章站 2023-01-02 18:31:29
题意 "题目链接" 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 defi ......

题意

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