码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-24 21:42:08      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:while   链接   http   ace   code   top   tar   using   char   

题意

题目链接

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

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

标签:while   链接   http   ace   code   top   tar   using   char   

原文地址:https://www.cnblogs.com/zwfymqz/p/10590303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!