其他分享
首页 > 其他分享> > nowcoder假日团队赛8 D-Artificial Lake

nowcoder假日团队赛8 D-Artificial Lake

作者:互联网

链接:https://ac.nowcoder.com/acm/contest/1069/D

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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 W_i (1 <= W_i <= 1,000) and height (like a relative elevation) H_i (1 <= H_i <= 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.

输入描述:

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

输出描述:

* 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.
示例1

输入

3
4 2
2 7
6 4

输出

4
50
26

说明

Three levels just as in the example above.  Water will fill the first level because it is the lowest.
As in the illustrations above.


模拟
  每次找到当前最低的可以被灌水的平台填灌
  然后合并更新

代码
  1 #include <bits/stdc++.h>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <stack>
  5 #include <cstdlib>
  6 #include <queue>
  7 #include <cmath>
  8 #include <cstdio>
  9 #include <algorithm>
 10 #include <string>
 11 #include <vector>
 12 #include <list>
 13 #include <iterator>
 14 #include <set>
 15 #include <map>
 16 #include <utility>
 17 #include <iomanip>
 18 #include <ctime>
 19 #include <sstream>
 20 #include <bitset>
 21 #include <deque>
 22 #include <limits>
 23 #include <numeric>
 24 #include <functional>
 25 #define gc getchar()
 26 #define mem(a) memset(a,0,sizeof(a))
 27 #define mod 1000000007
 28 #define sort(a,n,int) sort(a,a+n,less<int>())
 29 #define fread() freopen("in.in","r",stdin)
 30 #define fwrite() freopen("out.out","w",stdout)
 31  
 32 #define PI acos(-1.0)
 33 #define N 100005
 34 #define MOD 2520
 35 #define E 1e-12
 36  
 37 typedef long long ll;
 38 typedef char ch;
 39 typedef double db;
 40 const long long  INF = 0x3f3f3f3f3f3f3f3f;
 41  
 42 using namespace std;
 43  
 44  
 45 struct node
 46 {
 47     ll w , h;
 48     int l , r;
 49 }a[N];
 50  
 51 int n = 0;
 52 long long res[N] = {0};
 53  
 54 int lowest()
 55 {
 56     ll minv = INF , pos = 0;
 57     for(int i = 1;i <= n;i++)
 58         if(a[i].h < minv)
 59         {
 60             minv = a[i].h;
 61             pos = i;
 62         }
 63     return pos;
 64 }
 65 int update(int position)
 66 {
 67     for(int i = 0;i<n;i++)
 68     {
 69         int left = a[position].l , right = a[position].r;
 70          
 71         if(a[left].h < a[position].h)
 72             position = left;
 73         else if(a[right].h < a[position].h)
 74             position = right;
 75         else
 76             return position;
 77     }
 78 }
 79 void guanshui(int position)
 80 {
 81     int counter = 1;
 82     long long  sum = 0;
 83     while(counter <= N)
 84     {
 85         counter++;
 86         sum += a[position].w;
 87         res[position] = sum;
 88         int l = a[position].l , r = a[position].r;
 89         sum += (min(a[l].h , a[r].h) - a[position].h - 1) * a[position].w;
 90   
 91         a[l].r = r;
 92         a[r].l = l;
 93          
 94         if(a[l].h < a[r].h)
 95         {
 96             a[l].w += a[position].w;
 97             position = l;
 98         }
 99         else
100         {
101             a[r].w += a[position].w;
102             position = r;
103         }
104         position = update(position);
105     }
106 }
107 int main()
108 {
109     cin >> n;
110     int position = 0;
111     for(int i = 1;i <= n;i++)
112     {      
113         cin >> a[i].w >> a[i].h;
114     }
115     for(int i = 0;i <= n+1;i++)
116     {
117         a[i].l = i-1;
118         a[i].r = i+1;
119     }
120     a[0].w = 1;    
121     a[0].h = INF;  
122      
123     a[n+1].w = 1;  
124     a[n+1].h = INF;
125      
126     position = lowest();
127     guanshui(position);
128     for(int i = 1;i <= n;i++)
129     {
130         cout << res[i] << endl;
131     }
132     return 0;
133 }

 

标签:level,int,ll,Lake,Artificial,long,nowcoder,include,define
来源: https://www.cnblogs.com/SutsuharaYuki/p/11274832.html