[PKU3658]Artificial Lake [USACO 2008 Jan]

求水淹没每一层的时间。

显然水如果把某一层淹没了,那么它接下来淹没的将会是左边或者右边单调递减序列中最低的一个。而且之前淹没的Level也可以不管。为了加快检索速度,可以建立一个双向链表来维护。

用个变量ans记录到目前为止的总时间,则要淹没当前Level now的时间就是ans+(now->next->lef - now->last->rig),新的ans += (now->next->lef - now->last->rig) * ((now左右中最矮的)->hgt - now->hgt)。之后,从链表中删除now,now变为now向两边找单调递减序列中的最矮的那个。

具体的看代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdio>
#include <algorithm>

struct Link
{
  int lef, rig, hgt;
  Link *left, *right;
} a[100002];
int n, lowest(0x3FFFFFFF);
long long drop[100001], ans;
Link *now;
int main()
{
  scanf("%d", &n);
  a[0].lef = a[0].rig = 0, a[0].hgt = 0x3FFFFFFF, a[0].left = NULL, a[0].right = a+1;
  for (int i = 1, w, h; i <= n; ++i)
  {
    scanf("%d%d", &w, &h);
    a[i].lef = a[i-1].rig, a[i].rig = a[i].lef+w, a[i].hgt = h;
    a[i].left = a+i-1, a[i].right = a+i+1;
    if (h < lowest)
    {
      lowest = h;
      now = a+i;
    }
  }
  a[n+1].lef = a[n+1].rig = a[n].rig, a[n+1].hgt = 0x3FFFFFFF, a[n+1].left = a+n, a[n+1].right = NULL;
  for (Link *lower, *next; now->left && now->right; now = next)
  {
    if (now->left->hgt < now->right->hgt)
    {
      for (next = now->left; next->left != NULL && next->left->hgt <= next->hgt; next = next->left);
      lower = now->left;
    }
    else
    {
      for (next = now->right; next->right != NULL && next->right->hgt <= next->hgt; next = next->right);
      lower = now->right;
    }
    drop[now-a] = ans + now->right->lef - now->left->rig;
    ans += static_cast<long long>(now->right->lef - now->left->rig)*(lower->hgt - now->hgt);
    now->left->right = now->right;
    now->right->left = now->left;
  }
  for (int i = 1; i <= n; ++i)
    printf("%I64dn", drop[i]);
}

Comments