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;
}
点击打开链接
【题意】
给了一些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;
}
相关文章推荐
- hdu 1505 City Game 最大矩形面积 单调队列
- POJ2104 最大矩形面积 单调栈
- 9715 相邻最大矩形面积 单调栈
- Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏
- Poj 2559 最大矩形面积 v单调栈
- POJ2082 最大矩形面积
- POJ 2559 题解 最大矩形面积 单调栈
- POJ2559 最大矩形面积
- 寻找直方图中面积最大的矩形
- opencv——通过面积筛选最大轮廓,并求凸包矩形的长和宽
- 单调栈:平面上N个底部对齐紧靠在一起的矩形,宽高是任意整数,求其最大内切矩形的面积
- sdut 2401 最大矩形面积
- 求最大矩形面积
- lintcode[122]:直方图内最大矩形面积
- 最大矩形面积 --漫漫算法路 刷题篇
- 关于求一段最大的连续矩形面积
- 51Nod 1102 面积最大的矩形 (左右最大上升区间)
- Largest Rectangle in Histogram 直方图中最大的矩形面积
- 面积最大的矩形
- 单调栈:平面上N个底部对齐紧靠在一起的矩形,宽高是任意整数,求其最大内切矩形的面积(版本2)