[BZOJ2678][Usaco2012 Open]Bookshelf

P.S. 偶然间发现可以用 markdown。。。

[BZOJ2678][Usaco2012 Open]Bookshelf

试题描述

When Farmer John isn't milking cows, stacking haybales, lining up his cows, or building fences, he enjoys sitting down with a good book. Over the years, he has collected \(N\) books (\(1 \leq N \leq 10^5\)), and he wants to build a new set of bookshelves to hold them all. Each book \(i\) has a width \(W(i)\) and height \(H(i)\). The books need to be added to a set of shelves in order; for example, the first shelf should contain books \(1...k\) for some \(k\), the second shelf should start with book \(k+1\), and so on. Each shelf can have a total width of at most \(L\) (\(1 \leq L \leq 10^9\)). The height of a shelf is equal to the height of the tallest book on that shelf, and the height of the entire set of bookshelves is the sum of the heights of all the individual shelves, since they are all stacked vertically. Please help FJ compute the minimum possible height for the entire set of bookshelves. PROBLEM NAME: bookshelf

输入
  • Line 1: Two space-separated integers: \(N\) and \(L\).

  • Lines 2..1+N: Line \(i+1\) contains two space-separated integers: \(H(i)\) and \(W(i)\). (\(1 \leq H(i) \leq 10^6\); \(1 \leq W(i) \leq L\)).

输出
  • Line 1: The minimum possible total height for the set of bookshelves. SAMPLE
输入示例
5 10 //五本书,每个书架的宽度不超过10
5 7 //第一本书的高度及宽度
9 2
8 5
13 2
3 8
输出示例
21
数据规模及约定

见“试题描述”和“输入

题解
算法1

这题总体思路肯定是 dp,设 \(f_i\) 表示前 \(i\) 本书所需的最小高度和,则容易得到 \(f_i = min\{ f_j + max\{ H_{j+1}, H_{j+2}, ... , H_i \} | \sum_{k=j+1}^i \leq L \}\)

那么我们只需要考虑从哪个 \(j\) 转移到 \(i\) 就好了。

于是我们可以维护一个 \(H_i\) 下降的单调栈,然后每次计算 \(f_i\) 的时候用前缀和 \(S_i - S_j \leq L\) 的限制二分出 \(j\) 的位置(数组 \(S\) 表示高度的前缀和),然后就是查询单调栈中一段区间的最小值,可以用线段树维护。栈的插入、删除操作都可以当做线段树的点修改。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 100010
#define oo 2147483647
#define ool (1ll << 60)
#define LL long long

int n, hei[maxn], wid[maxn], lim;
LL Sw[maxn];

int S[maxn], mx[maxn], top;
LL f[maxn];

LL minv[maxn<<2];
void update(int o, int l, int r, int p, LL v) {
    if(l == r) minv[o] = v;
    else {
        int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
        if(p <= mid) update(lc, l, mid, p, v);
        else update(rc, mid + 1, r, p, v);
        minv[o] = min(minv[lc], minv[rc]);
    }
    return ;
}
LL query(int o, int l, int r, int ql, int qr) {
    if(l > r) return ool;
    if(ql <= l && r <= qr) return minv[o];
    int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
    LL res = ool;
    if(ql <= mid) res = min(res, query(lc, l, mid, ql, qr));
    if(qr > mid) res = min(res, query(rc, mid + 1, r, ql, qr));
    return res;
}

int main() {
    n = read(); lim = read();
    for(int i = 1; i <= n; i++) hei[i] = read(), wid[i] = read(), Sw[i] = Sw[i-1] + wid[i];
    
    f[0] = 0; S[0] = 0;
    S[top = 1] = mx[1] = 0;
    update(1, 1, n + 1, 1, 0);
    for(int i = 1; i <= n; i++) {
        while(top && hei[i] > mx[top]) top--;
        S[++top] = i; mx[top] = hei[i];
//      printf("stack: "); for(int j = 1; j <= top; j++) printf("%d%c", S[j], j < top ? ' ' : '\n');
        update(1, 1, n + 1, top, f[S[top-1]] + hei[i]);
        int l = 1, r = top, pos = lower_bound(Sw, Sw + n + 1, Sw[i] - lim) - Sw;
        while(l < r) {
            int mid = l + r >> 1;
            if(Sw[i] - Sw[S[mid]] > lim) l = mid + 1; else r = mid;
        }
        f[i] = min(query(1, 1, n + 1, l + 1, top), f[pos] + mx[l]);
//      printf("%d %d | %d: %lld\n", l, S[l], i, f[i]);
    }
    
    printf("%lld\n", f[n]);
    
    return 0;
}
算法2

还是维护单调栈,我们发现这其实是一个双头队列,偶然在 UOJ 上发现了一个 \(O(n)\) 的做法。

其实就是用两个栈模拟一个双头队列,注意到每个栈的前缀最小值是可以 \(O(1)\) 修改和询问的,所以用这种方法就可能将算法总复杂度降到 \(O(n)\)

我觉得最妙的地方在于重构,当某个栈空的时候将另一个栈分一半给这个已经空的栈(即暴力重构),复杂度有保证,详见上面链接(读者也不妨自己思考思考)。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 100010
#define oo 2147483647
#define ool (1ll << 60)
#define LL long long

int n, lim, hei[maxn], wid[maxn], lst[maxn]; // lst: last bigger height
LL Sw[maxn];

struct Info {
    int pos, mxv; LL val;
    Info() {}
    Info(int _1, int _2, LL _3): pos(_1), mxv(_2), val(_3) {}
} lft[maxn], rgt[maxn];
int Sl, Sr;
LL mnl[maxn], mnr[maxn];
int rebuild() {
    if(!Sl && !Sr) return -1;
    if(!Sl) {
        int mid = Sr + 1 >> 1;
        for(int i = mid; i; i--) lft[++Sl] = rgt[i], mnl[Sl] = min(mnl[Sl-1], lft[Sl].val);
        for(int i = mid + 1; i <= Sr; i++) rgt[i-mid] = rgt[i], mnr[i-mid] = min(mnr[i-mid-1], rgt[i-mid].val);
        Sr -= mid;
        return 0;
    }
    int mid = Sl + 1 >> 1;
    for(int i = mid; i; i--) rgt[++Sr] = lft[i], mnr[Sr] = min(mnr[Sr-1], rgt[Sr].val);
    for(int i = mid + 1; i <= Sl; i++) lft[i-mid] = lft[i], mnl[i-mid] = min(mnl[i-mid-1], lft[i-mid].val);
    Sl -= mid;
    return 0;
}

int q[maxn], hd, tl;

LL f[maxn];

int main() {
    n = read(); lim = read();
    for(int i = 1; i <= n; i++) hei[i] = read(), wid[i] = read(), Sw[i] = Sw[i-1] + wid[i];
    
    hei[0] = oo;
    for(int i = 1; i <= n; i++) {
        lst[i] = i - 1;
        while(hei[lst[i]] <= hei[i]) lst[i] = lst[lst[i]];
    }
    mnl[0] = mnr[0] = ool;
    rgt[++Sr] = Info(lst[1], hei[1], hei[1]);
    mnr[1] = f[1] = hei[1];
    q[hd = tl = 1] = 1;
    int lft_edge = 0;
    for(int i = 2; i <= n; i++) {
        while(1) {
            if(!Sr && rebuild()) break;
            if(rgt[Sr].mxv > hei[i]) break;
            Sr--;
        }
        rgt[++Sr] = Info(lst[i], hei[i], f[lst[i]] + hei[i]);
        mnr[Sr] = min(mnr[Sr-1], rgt[Sr].val);
        while(1) {
            if(!Sl && rebuild()) break;
            if(Sw[i] - Sw[lft[Sl].pos] <= lim) break;
            Sl--;
        }
        
        while(Sw[i] - Sw[lft_edge] > lim) lft_edge++;
        while(hd <= tl && hei[i] >= hei[q[tl]]) tl--; q[++tl] = i;
        while(lft_edge > q[hd]) hd++;
        f[i] = min(min(mnl[Sl], mnr[Sr]), f[lft_edge] + hei[q[hd]]);
//      printf("f[%d] = %lld\n", i, f[i]);
    }
    
    printf("%lld\n", f[n]);
    
    return 0;
}

P.S. markdown 好难用。。。

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/7617024.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 牛牛和她的朋友们正在玩一个有趣的游戏,他们需要构建一个有 $n$ 个节点的无向图,每个节点都有一个唯一的编号并且编号从 $1$ 到 $n$。他们需要从节点 $1$ 到节点 $n$ 找到一条最短路径,其中路径长度是经过的边权的和。为了让游戏更有趣,他们决定在图上添加一些额外的边,这些边的权值都是 $x$。他们想知道,如果他们添加的边数尽可能少,最短路径的长度最多会增加多少。 输入格式 第一行包含两个正整数 $n$ 和 $m$,表示节点数和边数。 接下来 $m$ 行,每行包含三个整数 $u_i,v_i,w_i$,表示一条无向边 $(u_i,v_i)$,权值为 $w_i$。 输出格式 输出一个整数,表示最短路径的长度最多会增加多少。 数据范围 $2 \leq n \leq 200$ $1 \leq m \leq n(n-1)/2$ $1 \leq w_i \leq 10^6$ 输入样例 #1: 4 4 1 2 2 2 3 3 3 4 4 4 1 5 输出样例 #1: 5 输入样例 #2: 4 3 1 2 1 2 3 2 3 4 3 输出样例 #2: 2 算法 (BFS+最短路) $O(n^3)$ 我们把问题转化一下,假设原图中没有添加边,所求的就是点 $1$ 到点 $n$ 的最短路,并且我们已经求出了这个最短路的长度 $dis$。 接下来我们从小到大枚举边权 $x$,每次将 $x$ 加入图中,然后再次求解点 $1$ 到点 $n$ 的最短路 $dis'$,那么增加的最短路长度就是 $dis'-dis$。 我们发现,每次加入一个边都需要重新求解最短路。如果我们使用 Dijkstra 算法的话,每次加入一条边需要 $O(m\log m)$ 的时间复杂度,总的时间复杂度就是 $O(m^2\log m)$,无法通过本题。因此我们需要使用更优秀的算法。 观察到 $n$ 的范围比较小,我们可以考虑使用 BFS 求解最短路。如果边权均为 $1$,那么 BFS 可以在 $O(m)$ 的时间复杂度内求解最短路。那么如果我们只是加入了一条边的话,我们可以将边权为 $x$ 的边看做 $x$ 条边的组合,每次加入该边时,我们就在原始图上添加 $x$ 条边,边权均为 $1$。这样,我们就可以使用一次 BFS 求解最短路了。 但是,我们不得不考虑加入多条边的情况。如果我们还是将边权为 $x$ 的边看做 $x$ 条边的组合,那么我们就需要加入 $x$ 条边,而不是一条边。这样,我们就不能使用 BFS 了。 但是,我们可以使用 Floyd 算法。事实上,我们每次加入边时,只有边权等于 $x$ 的边会发生变化。因此,如果我们枚举边权 $x$ 时,每次只需要将边权等于 $x$ 的边加入图中,然后使用 Floyd 算法重新计算最短路即可。由于 Floyd 算法的时间复杂度为 $O(n^3)$,因此总的时间复杂度为 $O(n^4)$。 时间复杂度 $O(n^4)$ 空间复杂度 $O(n^2)$ C++ 代码 注意点:Floyd算法计算任意两点之间的最短路径,只需要在之前的路径基础上加入新的边构成的新路径进行更新即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值