POJ 3658 Artificial Lake (单调栈)
题意:
析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度。
代码如下:
#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 (单调栈)的更多相关文章
- POJ - 3658 Artificial Lake
题意:向N个连续且高度不同的平台灌水,平台各有宽度,且高度各不相同.一开始,先向高度最低的平台灌水,直到灌满溢出,流向其他的平台,直至所有平台都被覆盖.已知每分钟注入高度为1且宽度为1的水,问每个平台 ...
- poj 2559 Largest Rectangle(单调栈)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26549 ...
- poj 2796 Feel Good单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20408 Accepted: 5632 Case T ...
- POJ 3415 后缀数组+单调栈
题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- poj 3250 Bad Hair Day (单调栈)
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- poj 3415 Common Substrings - 后缀数组 - 二分答案 - 单调栈
题目传送门 传送点I 传送点II 题目大意 给定串$A, B$,求$A$和$B$长度大于等于$k$的公共子串的数量. 根据常用套路,用一个奇怪的字符把$A$,$B$连接起来,然后二分答案,然后按mid ...
随机推荐
- python--软件规范和反射
软件开发规范 写一个作业的时候,要将写的代码分开 bin文件夹里面第一个是start文件 核心代码都在core文件夹里面 文件core最好也是固定名字 BaseDir=os.path.dirna ...
- C++中字符数组和字符串string
字符数组 C++中字符数组用char str[]能够用来表示一个字符串. (1) 数组的大小和字符串的长度. 数组的大小一定要大于字符串的长度,由于系统会自己主动补上一个'\0'作为字符串的结束标 ...
- 【转】git在公司内部的使用实践
版本定义: 版本号使用x.x.x进行定义,第一个x代表大版本只有在项目有重大变更时更新 第二个x代表常规版本有新需求会更新第三个x代表紧急BUG修正一个常见的版本号类似于:0.11.10 分支定义: ...
- centos6.4中文输入法安装和切换(转载)
1.用root登录,或者切换到root账户(su root): 2.yum install "@Chinese Support"; 3.exit: 4.System→prefere ...
- 【BZOJ1414/3705】[ZJOI2009]对称的正方形 二分+hash
[BZOJ1414/3705][ZJOI2009]对称的正方形 Description Orez很喜欢搜集一些神秘的数据,并经常把它们排成一个矩阵进行研究.最近,Orez又得到了一些数据,并已经把它们 ...
- libcurl理解和使用
1 libcurl是一个很好的客户端库 2 CURLOPT_URL 就是普通的url. 3 CURLOPT_HTTPHEADER 3.1 http get 4 CURLOPT_WRITEFUNCTIO ...
- linux环境下远程访问ftp
sftp ftpuser@127.0.0.1 使用sftp命令 ftpuser是用户名 127.0.0.1是目标ip 亲测好使.
- SecureCRT中使用VBs脚本自动telnet登陆
查看SecureCRT帮助文档: Help-> Help Topics->Scripting -> Script Objects Reference -> Session Ob ...
- Double.valueOf()与Double.parseDouble()两者的区别
写代码用到这两个方法,不知道有什么区别,看一下源码: Double.parseDouble(String str) public static double parseDouble(String s) ...
- HDU2896 病毒侵袭 —— AC自动机
题目链接:https://vjudge.net/problem/HDU-2896 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...