单调栈裸题

如果矩形高度从左到右是依次递增,那我们枚举每个矩形高度,宽度拉到最优,计算最大面积即可

当有某个矩形比前一个矩形要矮的时候,这块面积的高度就不能大于他本身,所以之前的所有高于他的矩形多出来的部分都没用了,不会再计算第二次。

因此我们只需要用单调栈维护矩形高度即可,当有高度较低的矩形进栈时,先将之前比他高的每个矩形弹出,宽度累加,更新答案。然后我们要进栈的矩形的宽度也要变成之前累加的宽度+1,因为要保留有用部分。。

有个小技巧:为了方便程序,在末尾加一个高度为0的矩形

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C yql){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % yql)if(p & 1)ans = 1LL * x * ans % yql;
return ans;
}
ll h[100005], s[100005], w[100005], tot;
int main(){ int n;
while(cin >> n && n){
memset(h, 0, sizeof h);
memset(s, 0, sizeof s);
memset(w, 0, sizeof w);
ll ans = 0; w[n + 1] = 0; tot = 0;
for(int i = 1; i <= n; i ++) cin >> h[i];
for(int i = 1; i <= n + 1; i ++){
if(s[tot] > h[i]){
ll width = 0;
while(tot > 0 && s[tot] > h[i]){
width += w[tot];
ans = max(ans, (ll)width * s[tot]);
tot --;
}
s[++tot] = h[i], w[tot] = width + 1;
}
else{
s[++tot] = h[i], w[tot] = 1;
}
}
printf("%lld\n", ans);
}
return 0;
}

再来个笛卡尔树做法

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define __fastIn ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
using namespace std;
typedef long long LL;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int ret = 0, w = 0; char ch = 0;
while(!isdigit(ch)){
w |= ch == '-', ch = getchar();
}
while(isdigit(ch)){
ret = (ret << 3) + (ret << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -ret : ret;
}
template <typename A>
inline A __lcm(A a, A b){ return a / __gcd(a, b) * b; }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 200005;
LL h[N], ans;
int n;
struct Node{
int i, size;
LL val;
Node *fa, *lf, *rf;
Node(){}
Node(int i, LL val): i(i), val(val){
fa = lf = rf = nullptr;
size = 0;
}
}*root; Node *buildTree(){
stack<Node*> st;
Node *last = nullptr;
for(int i = 1; i <= n; i ++){
Node *cur = new Node(i, h[i]);
while(!st.empty()){
if(st.top()->val < cur->val){
Node *up = st.top();
if(up->rf) cur->lf = up->rf, up->rf->fa = cur;
up->rf = cur, cur->fa = up;
break;
}
last = st.top(); st.pop();
}
if(st.empty() && last) last->fa = cur, cur->lf = last;
st.push(cur);
}
Node *root = nullptr;
while(!st.empty()) root = st.top(), st.pop();
return root;
} void dfs(Node *rt){
if(rt == nullptr) return;
rt->size = 1;
dfs(rt->lf), dfs(rt->rf);
if(rt->lf) rt->size += rt->lf->size;
if(rt->rf) rt->size += rt->rf->size;
ans = max(ans, 1LL * rt->size * rt->val);
} int main(){ while(~scanf("%d", &n) && n){
for(int i = 1; i <= n; i ++) scanf("%lld", &h[i]);
root = buildTree();
ans = 0, dfs(root);
printf("%lld\n", ans);
}
return 0;
}

HDU1506 Largest Rectangle in a Histogram(算竞进阶习题)的更多相关文章

  1. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. hdu1506——Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. hdu1506 Largest Rectangle in a Histogram

    Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...

  4. HDU1506 Largest Rectangle in a Histogram (动规)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  5. NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!

                                         Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...

  6. 【题解】hdu1506 Largest Rectangle in a Histogram

    目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的 ...

  7. HDU-1506 Largest Rectangle in a Histogram【单调栈】

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  8. HDU1506 ( Largest Rectangle in a Histogram ) [dp]

    近期情绪太不稳定了.可能是由于在找实习这个过程碰壁了吧.第一次面试就跪了,可能是我面的是一个新公司,制度不完好,我感觉整个面试过程全然不沾编程,我面试的还是软件开发-后来我同学面试的时候.说是有一道数 ...

  9. 【单调栈】hdu1506 Largest Rectangle in a Histogram

    单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是 ...

随机推荐

  1. Westore 1.0 正式发布 - 小程序框架一个就够

    世界上最小却强大的小程序框架 - 100多行代码搞定全局状态管理和跨页通讯 Github: https://github.com/dntzhang/westore 众所周知,小程序通过页面或组件各自的 ...

  2. flask 单元测试

    程序开发过程中,代码是为了完成需求,当代码编译通过后,能不能保证功能的正常实现,需要我们编写测试代码,模拟程序运行过程,检验功能是否符合预期. 单元测试主要面向一些功能单一的模块进行. 单元测试,实际 ...

  3. VS2012添加数据库连接时报错,未能加载文件或程序集microsoft.sqlserver.management.sdk.sfc

    今天在VS2012中添加数据库连接时报错 未能加载文件或程序集microsoft.sqlserver.management.sdk.sfc,Version=11.0 查了很多资料,最后下载安装了Sha ...

  4. 把玩Alpine linux(二):APK包管理器

    导读 Alpine Linux非常精简,开机内存占用也在二三十兆大,没有拆箱即用,就需要我们自己去做一些了解和配置 Alpine Linux的优劣 优势 Alpine Linux的Docker镜像特点 ...

  5. Django admin参数配置

    admin参数配置 一. admin 启动 admin: Django的后台数据管理的web版本 针对Food表,url: http://127.0.0.1:8000/admin/app02/food ...

  6. ocrosoft 程序设计提高期末复习问题M 递归求猴子吃桃

    http://acm.ocrosoft.com/problem.php?cid=1172&pid=12 题目描述 猴子吃桃问题.猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个. ...

  7. c# winform导出Excel

    //需要注意添加引用Microsoft.Office.Interop.Excel.dll string fileName =DateTime.Now.Year+ DateTime.Now.Month+ ...

  8. js-canvas(基本用法)

    ###1. canvas(画布) <canvas>是HTML 5 新增的元素,可用于通过使用JavaScript中的脚本来绘制图形 默认宽高为300px*150px 基本概念和方法入门推荐 ...

  9. laravel打印sql

    DB::connection()->enableQueryLog(); print_r(DB::getQueryLog());

  10. python爬虫之git的使用(windows下pycharm使用)

    相信很多同学学会了git或者github以后都不知道怎么跟windows上的pycharm连在一起工作,那么下面我们开始介绍简单的安装和使用方法. 一.安装 1.首先你的有一个github的账户.注册 ...