poj-3658 Artificial Lake(模拟)

http://poj.org/problem?id=3658

Description

The oppressively hot summer days have raised the cows' clamoring to its highest level. Farmer John has finally decided to build an artificial lake. For his engineering studies, he is modeling the lake as a two-dimensional landscape consisting of a contiguous sequence of N soon-to-be-submerged levels (1 ≤ N ≤ 100,000) conveniently numbered 1..N from left to right.

Each level i is described by two integers, its width Wi (1 ≤ Wi ≤ 1,000) and height (like a relative elevation) Hi (1 ≤ Hi ≤ 1,000,000). The heights of FJ's levels are unique. An infinitely tall barrier encloses the lake's model on the left and right. One example lake profile is shown below.

         *             *  :

* * :
* * 8
* *** * 7
* *** * 6
* *** * 5
* ********** 4 <- height
* ********** 3
*************** 2
*************** 1
Level | 1 |2| 3 |

In FJ's model, he starts filling his lake at sunrise by flowing water into the bottom of the lowest elevation at a rate of 1 square unit of water per minute. The water falls directly downward until it hits something, and then it flows and spreads as room-temperature water always does. As in all good models, assume that falling and flowing happen instantly. Determine the time at which each elevation's becomes submerged by a single unit of water.


WATER WATER OVERFLOWS
| |
* | * * | * * *
* V * * V * * *
* * * .... * *~~~~~~~~~~~~*
* ** * *~~~~** : * *~~~~**~~~~~~*
* ** * *~~~~** : * *~~~~**~~~~~~*
* ** * *~~~~**~~~~~~* *~~~~**~~~~~~*
* ********* *~~~~********* *~~~~*********
*~~~~********* *~~~~********* *~~~~*********
************** ************** **************
************** ************** **************

After 4 mins After 26 mins After 50 mins
Lvl 1 submerged Lvl 3 submerged Lvl 2 submerged

Warning: The answer will not always fit in 32 bits.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 describes level i with two space-separated integers: Wi and Hi

Output

* Lines 1..N: Line i contains a single integer that is the number of minutes that since sunrise when level #i is covered by water of height 1.

Sample Input

3
4 2
2 7
6 4

Sample Output

4
50
26

题意:

给出N个连续的平台,他们各有宽度,且高度不同。先向高度最低的平台灌水,直到灌满溢出,流向其他的平台,直至所有平台都被覆盖。已知每分钟注入高为1宽为1的水,求每个平台恰好被高为1的水覆盖的时间。

思路:

先记录每个平台的高度与宽度以及他们的左、右平台,找到最低的平台,开始灌水。灌满最低平台后,相当于该平台消失,寻找下一流向哪个平台,再更新左右平台,直到最高的平台被水覆盖。

分析:

1、对于每个平台,L代表其左边的平台标号,R代表其右边的平台标号,随着平台被淹没,R,L会随之改变。

2、平台标号为1~n,其两侧平台0和n+1高度初始化为正无穷作为界线,因此只需统计淹没了n个平台的情况即可。

3、先找到最低的平台标号。淹没当前平台后,更新水流溢出后,即将流向的平台标号。

4、然后找要淹没的那个平台,此平台要比两侧平台低。

5、将当前平台的水注满到与两侧较矮平台齐平的高度,将较矮的平台宽度更新为加上当前平台后的宽度,与此同时,更新当前平台两侧的平台的左右平台标号。

注意:数据不保证答案全部在32位整型变量的范围内,要用long long 。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 const int INF=0x3f3f3f3f;
10 using namespace std;
11 #define maxn 100010
12 
13 struct node{
14     long long wide;
15     long long high;
16     int left;
17     int right;
18 }a[maxn];
19 int n;
20 long long ans[maxn];//存答案,注意是long long 
21 
22 int main()
23 {
24     scanf("%d",&n);
25     for(int i=1;i<=n;i++)
26     {
27         scanf("%lld %lld",&a[i].wide,&a[i].high);
28     }
29     for(int i=0;i<=n+1;i++)//左右平台初始化
30     {
31         a[i].left=i-1;
32         a[i].right=i+1;
33     }
34     
35     a[0].high=INF;//高度设为无限高
36     a[n+1].high=INF;
37     
38     int MIN=INF;
39     int m;
40     for(int i=1;i<=n;i++)//寻找高度最低的平台
41     {
42         if(a[i].high<MIN)
43         {
44             MIN=a[i].high;
45             m=i;
46         }
47     }
48     
49     int L,R;
50     long long sum=0;//计时器,注意是long long 
51     int num=1;//已经淹没平台个数
52     while(num<=n)
53     {
54         num++;
55         sum+=a[m].wide;//根据题意,先加一层作为答案 
56         ans[m]=sum;
57         
58         L=a[m].left;
59         R=a[m].right;
60         sum+=(min(a[L].high,a[R].high)-a[m].high-1)*a[m].wide;//填满该坑,-1是因为开始先加了一层
61         
62         a[L].right=R;//更新左平台
63         a[R].left=L;//更新右平台
64         if(a[L].high<a[R].high)//更新宽度
65         {
66             a[L].wide+=a[m].wide;
67             m=L;    
68         }
69         else
70         {
71             a[R].wide+=a[m].wide;
72             m=R;    
73         }
74         
75         //更新下一流向平台标号
76         while(1)
77         {
78             R=a[m].right;
79             L=a[m].left;
80             if(a[L].high<a[m].high)
81                 m=L;
82             else if(a[R].high<a[m].high)
83                 m=R;
84             else
85                 break;
86         }
87     }
88     for(int i=1;i<=n;i++)
89     {
90         printf("%lld
",ans[i]);
91     }
92     return 0;
93 }
原文地址:https://www.cnblogs.com/jiamian/p/11268731.html