题意:

析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 1e5 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int w, h, id;
};
Node a[maxn], sta[maxn];
LL ans[maxn]; int main(){
while(scanf("%d", &n) == 1){
int minh = INF;
int id;
for(int i = 1; i <= n; ++i){
scanf("%d %d", &a[i].w, &a[i].h);
if(minh > a[i].h){
minh = a[i].h;
id = i;
}
a[i].id = i;
// printf("%d\n", a[i].w);
}
int top = 0;
a[0] = a[n+1] = (Node){INF, INF, INF};
sta[++top] = a[0];
sta[++top] = a[id];
// printf("%d\n", sta[top].w);
int l = id; int r = id;
LL all = 0;
for(int i = 1; i <= n; ++i){
int tmp;
if(a[l-1].h < a[r+1].h) tmp = --l;
else tmp = ++r;
int add = 0;
while(top && a[tmp].h > sta[top].h){
sta[top].w += add;
ans[sta[top].id] = all + sta[top].w;
all += (LL)(min(a[tmp].h, sta[top-1].h) - sta[top].h) * sta[top].w;
//printf("%lld\n", all);
add = sta[top].w;
--top;
}
a[tmp].w += add;
sta[++top] = a[tmp];
}
for(int i = 1; i <= n; ++i)
printf("%lld\n", ans[i]);
}
return 0;
}

POJ 3658 Artificial Lake (单调栈)的更多相关文章

  1. POJ - 3658 Artificial Lake

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

  2. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

  3. poj 2796 Feel Good单调栈

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20408   Accepted: 5632 Case T ...

  4. POJ 3415 后缀数组+单调栈

    题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...

  5. Poj 3250 单调栈

    1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...

  6. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  7. poj 3250 Bad Hair Day (单调栈)

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  8. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  9. poj 3415 Common Substrings - 后缀数组 - 二分答案 - 单调栈

    题目传送门 传送点I 传送点II 题目大意 给定串$A, B$,求$A$和$B$长度大于等于$k$的公共子串的数量. 根据常用套路,用一个奇怪的字符把$A$,$B$连接起来,然后二分答案,然后按mid ...

随机推荐

  1. maven命令学习-发布上传jar包-deploy

    Maven学习六之利用mvn deploy命令上传包 转http://blog.csdn.net/woshixuye/article/details/8133050 mvn:deploy在整合或者发布 ...

  2. SQL查询刚開始学习的人指南读书笔记(三)值表达式

    CHAPTER 5 Getting More Than Simple Columns Intro Value  expression,it contains column names, literal ...

  3. LookAround开元之旅

    http://blog.csdn.net/lancees/article/details/17696805

  4. elk 日志分析系统Logstash+ElasticSearch+Kibana4

    elk 日志分析系统 Logstash+ElasticSearch+Kibana4 logstash 管理日志和事件的工具 ElasticSearch 搜索 Kibana4 功能强大的数据显示clie ...

  5. wpf 模板选择器DataTemplateSelector及动态绑定使用教程

    其实也说不上算是教程了,只是把自己学习的代码拿出来分享一下,同时方便以后遇到类似问题的时候翻一下.MSDN里如是说:通常,如果有多个 DataTemplate 可用于同一类型的对象,并且您希望根据每个 ...

  6. 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.3引发的血案

    1.下载了一个开源项目,是用netcore开发的 2.VS2015打不开解决方案 3.于是安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.3 4.安装成功,项目顺利 ...

  7. Write Custom Java to Create LZO Files

    https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LZO LanguageManual LZO     Skip to e ...

  8. 6.JS输出

    JavaScript 通常用于操作 HTML 元素. ① document.getElementById(id),可以访问某个 HTML 元素 请使用 "id" 属性来标识 HTM ...

  9. hibernate属性配置

    数据库中一个字段的默认值设为0,当用hibernate插入数据时,没有对该字段进行操作,结果该字段居然不是0,而是空.后来google了一下,发现应该在.hbm.xml文件中添加一些参数定义(示例中的 ...

  10. javascrip中ajax

    移动端对加载速度要求比较高,由于jquery插件有270多k,无形中增加加载的速度,下面整理一下原生js中ajax: 先了解ajax的基础知识 (1)XMLHttpRequest 对象 XMLHttp ...