您的位置:首页 > 其它

POJ2082 最大矩形面积 单调栈

2017-01-05 13:41 113 查看
【题目链接】

          点击打开链接

【题意】

          给了一些h*w的矩形,大小可不等,求可组成的最大矩形面积是多少?单调栈经典题,做法和点击打开链接 完全相同。

【AC代码】

//
//Created by just_sort 2016/1/5
//Copyright (c) 2016 just_sort.All Rights Reserved
//

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstdlib>
#include <cstring>
#include <sstream> //isstringstream
#include <iostream>
#include <algorithm>
using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define REP1(i, a, b) for(int i = a; i < b; i++)
#define REP2(i, a, b) for(int i = a; i <= b; i++)
#define REP3(i, a, b) for(int i = a; i >= b; i--)
#define CLR(a, b) memset(a, b, sizeof(a))
#define MP(x, y) make_pair(x,y)
const int maxn = 1e6 + 10;
const int maxm = 2e5;
const int maxs = 10;
const int maxp = 1e3 + 10;
const int INF = 1e9;
const int UNF = -1e9;
const int mod = 1e9 + 7;
int gcd(int x, int y) {return y == 0 ? x : gcd(y, x % y);}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head
struct node{
int height, width;
}s[maxn];
int top;
int getmax(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int n, height, width, tmp, ans, tot, top;
while(scanf("%d", &n) != EOF && n != -1)
{
top = ans = 0;
REP1(i, 0, n){
scanf("%d%d", &width, &height);
tmp = 0;
while(top > 0 && s[top - 1].height >= height)
{
tot = s[top - 1].height * (s[top - 1].width + tmp);
ans = getmax(ans, tot);
tmp += s[top - 1].width;
--top;
}
s[top].height = height;
s[top].width = width + tmp;
top++;
}
tmp = 0;
while(top > 0)
{
tot = s[top - 1].height * (s[top - 1].width + tmp);
ans = getmax(ans, tot);
tmp += s[top - 1].width;
--top;
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: