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


Sample Output


题意:

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

思路:

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

分析:

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

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

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

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

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

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

代码:

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
const int INF=0x3f3f3f3f;
using namespace std;
#define maxn 100010 struct node{
long long wide;
long long high;
int left;
int right;
}a[maxn];
int n;
long long ans[maxn];//存答案,注意是long long int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%lld %lld",&a[i].wide,&a[i].high);
}
for(int i=;i<=n+;i++)//左右平台初始化
{
a[i].left=i-;
a[i].right=i+;
} a[].high=INF;//高度设为无限高
a[n+].high=INF; int MIN=INF;
int m;
for(int i=;i<=n;i++)//寻找高度最低的平台
{
if(a[i].high<MIN)
{
MIN=a[i].high;
m=i;
}
} int L,R;
long long sum=;//计时器,注意是long long
int num=;//已经淹没平台个数
while(num<=n)
{
num++;
sum+=a[m].wide;//根据题意,先加一层作为答案
ans[m]=sum; L=a[m].left;
R=a[m].right;
sum+=(min(a[L].high,a[R].high)-a[m].high-)*a[m].wide;//填满该坑,-1是因为开始先加了一层 a[L].right=R;//更新左平台
a[R].left=L;//更新右平台
if(a[L].high<a[R].high)//更新宽度
{
a[L].wide+=a[m].wide;
m=L;
}
else
{
a[R].wide+=a[m].wide;
m=R;
} //更新下一流向平台标号
while()
{
R=a[m].right;
L=a[m].left;
if(a[L].high<a[m].high)
m=L;
else if(a[R].high<a[m].high)
m=R;
else
break;
}
}
for(int i=;i<=n;i++)
{
printf("%lld\n",ans[i]);
}
return ;
}

poj-3658 Artificial Lake(模拟)的更多相关文章

  1. POJ 3658 Artificial Lake (单调栈)

    题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...

  2. POJ - 3658 Artificial Lake

    题意:向N个连续且高度不同的平台灌水,平台各有宽度,且高度各不相同.一开始,先向高度最低的平台灌水,直到灌满溢出,流向其他的平台,直至所有平台都被覆盖.已知每分钟注入高度为1且宽度为1的水,问每个平台 ...

  3. HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)

    Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...

  4. poj 2632 Crashing Robots 模拟

    题目链接: http://poj.org/problem?id=2632 题目描述: 有一个B*A的厂库,分布了n个机器人,机器人编号1~n.我们知道刚开始时全部机器人的位置和朝向,我们可以按顺序操控 ...

  5. POJ 2014 Flow Layout 模拟

    http://poj.org/problem?id=2014 嘻嘻2014要到啦,于是去做Prob.ID 为2014的题~~~~祝大家新年快乐~~ 题目大意: 给你一个最大宽度的矩形,要求把小矩形排放 ...

  6. POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)

    题目链接:http://poj.org/problem?id=2632 先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- - 本来2632是道略微恶心点的模拟 ...

  7. poj 1888 Crossword Answers 模拟题

    Crossword Answers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 869   Accepted: 405 D ...

  8. POJ 2632 Crashing Robots 模拟 难度:0

    http://poj.org/problem?id=2632 #include<cstdio> #include <cstring> #include <algorith ...

  9. poj 3253 Fence Repair(模拟huffman树 + 优先队列)

    题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...

随机推荐

  1. UVA - 10118 Free Candies(免费糖果)(dp---记忆化搜索)

    题意:桌上有4堆糖果,每堆有N(N<=40)颗.佳佳有一个最多可以装5颗糖的小篮子.他每次选择一堆糖果,把最顶上的一颗拿到篮子里.如果篮子里有两颗颜色相同的糖果,佳佳就把它们从篮子里拿出来放到自 ...

  2. 使用super调用被子类覆盖的父类方法

    1.没有super方法 /* * 子类方法覆盖父类方法,用super方法可以调用父类被覆盖的方法 */ class fruit{ public fruit() { System.out.println ...

  3. “帮你”校园资讯平台app使用体验

    该app由我的17级学长学姐编写而成,主要功能失物招领,二手市场,表白墙.该软件目前只是面向本校的各专业学生,为内测版本.该软件的注册流程简单,只需要学号确定身份后即可登陆,并且发布各种信息,或者与丢 ...

  4. List、Set和Map详解及其区别和他们分别适用的场景

    Java中的集合包括三大类 它们是Set(集).List(列表)和Map(映射),它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现类.Set的实现类主要有HashS ...

  5. Python pip安装时You are using pip version 9.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

    在我们安装第三方库的时候会在结尾出现如下两行内容 You are using pip version 9.0.1, however version 18.0 is available. You sho ...

  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 事务

    MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成 ...

  7. PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  8. HDU - 1251 字典树模板题

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  9. Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之字符串类型(string)

    关于Python的字符串处理也如其他语言一样属于重点中的重点,主要是牵扯到的函数和内容较为多和乱一些.关于什么是字符串,Python中的定义是:以单引号或者双引号括起来的任意文本. 1.   字符串的 ...

  10. ubuntu下查询网络的常用命令

    1.查看无线路由器(网关)的IP地址 在terminal中输入如下命令,即可查询到当前使用的路由器的IP地址(我电脑是连着一个无线网络还有一个DSL连接): route|grep -i default ...