# 洛谷P1848 书架

n <= 100000

 1 #include <cstdio>
2 #include <algorithm>
3 #include <queue>
4 #define mp std::make_pair
5
6 typedef long long LL;
7 const int N = 100010;
8
9 LL f[N], sum[N];
10 int st[N][25], pow[N], n, to[N], p[N], top;
11 bool in_stk[N];
12 std::priority_queue<std::pair<LL, int> > Q;
13
14 inline void STinit() {
15     int j = 1, lm = 0;
16     while((1 << lm) <= n) {
17         while(j < (1 << (lm + 1)) && j <= n) {
18             pow[j] = lm;
19             j++;
20         }
21         lm++;
22     }
23     for(int j = 1; j < lm; j++) {
24         for(int i = 1; i + (1 << j) - 1 <= n; i++) {
25             st[i][j] = std::max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
26         }
27     }
28     return;
29 }
30
31 inline int getmax(int l, int r) {
32     int t = pow[r - l + 1];
33     return std::max(st[l][t], st[r - (1 << t) + 1][t]);
34 }
35
36 int main() {
37     int L;
38     scanf("%d%d", &n, &L);
39     for(int i = 1; i <= n; i++) {
40         scanf("%d%lld", &st[i][0], &sum[i]);
41         sum[i] += sum[i - 1];
42         f[i] = 1ll << 60;
43     }
44
45     STinit();
46     st[0][0] = 0x7f7f7f7f;
47     to[0] = -1;
48
49     int pos = 0;
50     for(int i = 1; i <= n; i++) {
51         while(st[i][0] >= st[p[top]][0]) {
52             in_stk[p[top]] = 0;
53             top--;
54         }
55         to[i] = p[top];
56         p[++top] = i;
57         in_stk[i] = 1;
58
59         Q.push(mp(-1 * (f[to[i]] + getmax(to[i] + 1, i)), i));
60
61
62         while(sum[i] - sum[pos] > L) {
63             pos++;
64         }
65         while((!Q.empty()) && (to[Q.top().second] < pos || (!in_stk[Q.top().second]))) {
66             Q.pop();
67         }
68         if(!Q.empty()) {
69             f[i] = -1 * Q.top().first;
70         }
71         f[i] = std::min(f[i], f[pos] + getmax(pos + 1, i));
72     }
73     /*for(int i = 1; i <= n; i++) {
74         printf("%lld ", f[i]);
75     }*/
76     printf("%lld", f[n]);
77     return 0;
78 }
AC代码

posted @ 2018-10-08 15:53  garage  阅读(...)  评论(...编辑  收藏