• POJ2559 Largest Rectangle in a Histogram


    传送


    单调栈经典题,以前总写不对,有些心里阴影,故在此放一个代码。


    其主要思路就是保持栈一直单调递增,然后只有在被弹栈的时候才统计以这个竖块为左边界的矩形的面积。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<queue>
    #include<assert.h>
    #include<ctime>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    #define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e5 + 5;
    In ll read()
    {
    	ll ans = 0;
    	char ch = getchar(), las = ' ';
    	while(!isdigit(ch)) las = ch, ch = getchar();
    	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    	if(las == '-') ans = -ans;
    	return ans;
    }
    In void write(ll x)
    {
    	if(x < 0) x = -x, putchar('-');
    	if(x >= 10) write(x / 10);
    	putchar(x % 10 + '0');
    }
    In void MYFILE()
    {
    #ifndef mrclr
    	freopen(".in", "r", stdin);
    	freopen(".out", "w", stdout);
    #endif
    }
    
    int n;
    struct Node
    {
    	int h, w;
    }st[maxn];
    int top = 0;
    ll ans = 0;
    
    In void Push(int h)
    {
    	if(!top || h > st[top].h) st[++top] = (Node){h, 1};
    	else
    	{
    		int tp = 0;
    		while(top && st[top].h >= h)
    		{
    			ans = max(ans, 1LL * st[top].h * (st[top].w + tp));
    			tp += st[top--].w;
    		}
    		st[++top] = (Node){h, tp + 1};
    	}
    }
    
    int main()
    {
    //	MYFILE();
    	while(scanf("%d", &n) && n)
    	{
    		ans = top = 0;
    		for(int i = 1; i <= n; ++i) Push(read());
    		Push(0);
    		write(ans), enter;
    	}
    	return 0;
    }
    
  • 相关阅读:
    最新版-Python和Java实现Aes相互加解密
    tasker 实现短信监听和转发 解决短信验证码登录问题
    某宁 价格爬取 python
    python 获取 某东商品评论数
    xpath 使用
    frida hook 重载函数的几种写法
    Python 爬虫遇到形如 &#x5c0f;&#x8bf4; 的编码如何转换为中文?
    python 使用 pymysql 存,改dic类型数据
    Excel甘特图制作
    关于sunlike ERP的问题解决集
  • 原文地址:https://www.cnblogs.com/mrclr/p/15040698.html
Copyright © 2020-2023  润新知