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

分析:

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

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

3、Find_lowest();---得到最低的平台标号。

4、update_lowest(cur);---淹没当前平台后,cur更新为水流溢出后,即将流向的平台标号。

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

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int N;
struct Node{
int L, R;
LL w, h, ans;
void read(){
scanf("%lld%lld", &w, &h);
}
}num[MAXN];
int Find_lowest(){
LL mi = LL_INF, cur = 0;
for(int i = 1; i <= N; ++i){
if(num[i].h < mi){
mi = num[i].h;
cur = i;
}
}
return cur;
}
int update_lowest(int cur){
while(1){
int l = num[cur].L, r = num[cur].R;
if(num[l].h < num[cur].h) cur = l;
else if(num[r].h < num[cur].h) cur = r;
else return cur;
}
}
void solve(int cur){
int cnt = 0;
LL sum = 0;
while(cnt < N){
sum += num[cur].w;//将水注满到刚好覆盖该平台
num[cur].ans = sum;
++cnt;
int l = num[cur].L, r = num[cur].R;
sum += (Min(num[l].h, num[r].h) - num[cur].h - 1) * num[cur].w;//将当前平台的水注满到与两侧较矮平台齐平的高度
num[l].R = r;//更新两侧平台的左右平台
num[r].L = l;
if(num[l].h < num[r].h){
num[l].w += num[cur].w;
cur = l;
}
else{
num[r].w += num[cur].w;
cur = r;
}
cur = update_lowest(cur);
}
}
int main(){
scanf("%d", &N);
for(int i = 1; i <= N; ++i){
num[i].read();
}
for(int i = 0; i <= N + 1; ++i){
num[i].L = i - 1;
num[i].R = i + 1;
}
num[0].w = num[N + 1].w = 1;
num[0].h = num[N + 1].h = LL_INF;
int cur = Find_lowest();
solve(cur);
for(int i = 1; i <= N; ++i){
printf("%lld\n", num[i].ans);
}
return 0;
}

  

POJ - 3658 Artificial Lake的更多相关文章

  1. POJ 3658 Artificial Lake (单调栈)

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

  2. poj-3658 Artificial Lake(模拟)

    http://poj.org/problem?id=3658 Description The oppressively hot summer days have raised the cows' cl ...

  3. POJ No.2386 Lake Counting

    题目链接:http://poj.org/problem?id=2386 分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下 ...

  4. POJ 之2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20003   Accepted: 10063 D ...

  5. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  6. POJ3658Matrix( 双重二分+负数+死循环)

    POJ 3658 Matrix 双重二分,wa了一下午,实在不太明白为啥一写二分就会进入死循环. INF要设的大一些,本题设0x3f3f3f3f会wa. 本题有负数, 二分时(l+r)/2与(l+r) ...

  7. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  8. DFS:Lake Counting(POJ 2386)

    好吧前几天一直没更新博客,主要是更新博客的确是要耗费一点精力 北大教你数水坑 最近更新博客可能就是一点旧的东西和一些水题,主要是最近对汇编感兴趣了嘻嘻嘻 这一题挺简单的,没什么难度,简单深搜 #inc ...

  9. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

随机推荐

  1. imple-unpack---攻防世界

    拿到题目查壳没有发现.题目已经明确说了,基本上是有壳的,Linux下面看看 应该就是upx的壳了,下载upx进行脱壳,https://sourceforge.net/projects/upx/file ...

  2. C++面试常见问题——12虚函数

    虚函数 虚函数的工作原理 虚函数的实现要求对象携带额外的信息,这些信息用于确定运行时调用哪一个虚函数,这一信息具有一种被称为虚函数表指针(vptr)的指针形式.vptr指向一个被称为虚函数表(vtbl ...

  3. git下载安装、配置及idea初始化

    安装 wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.19.0.tar.gz git 安装依赖 yum -y insta ...

  4. 用 Python 编写一个天气查询应用

    效果预览: 一.获取天气信息 使用python获取天气有两种方式. 1)是通过爬虫的方式获取天气预报网站的HTML页面,然后使用xpath或者bs4解析HTML界面的内容. 2)另一种方式是根据天气预 ...

  5. 006、MySQL取当前系统时间

    #取当前时间文本格式 SELECT curdate( ) , now( ); 效果如下图: 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:3824772 ...

  6. 学会C#

    一.字符串插值 (String Interpolation) C# 6之前我们拼接字符串时需要这样 var Name = "Jack"; var results = "H ...

  7. 如何用naviecat批量创建mysql数据

    1.参考博文:https://blog.csdn.net/lelly52800/article/details/87267096 2.excel要与表结构一致 3.右键,导入向导,选择相应版本,点击“ ...

  8. Flutter Windows下AndroidStudio环境搭建

    目前同类产品比较知名的有ReactNative,Flutter还有国内那家了uniapp了,流畅度理论上Flutter最快 官网:https://flutter.dev/docs/get-starte ...

  9. java核心-多线程-零碎知识收集

    1.不能使用Integer作为并发锁 原因:synchronized(Integer)时,当值发生改变时,基本上每次锁住的都是不同的对象实例,想要保证线程安全,推荐使用AtomicInteger之类会 ...

  10. 七、JavaScript之console.log输出和document.write输出

    一.代码如下 二.运行效果如下 三.点击之后,效果如下 四.按一下F12,在控制台中可以看到