首页 > 代码库 > poj 2559 DP
poj 2559 DP
两种解法。
我想到的是最大的矩形,中间一定有个最矮的某个单位矩形,所以用两个数组记录histogram[i]左右两边第一个比它小的单位矩形的序号leftLowerId[i]和rightLowerId[i],那么对于histogram[i],它自己的最大矩形面积就是(rightLowerId[i] - leftLowerId[i] - 1) * histogram[i]。
这里找leftLowerId和rightLowerId的时候用DP加速。以rightLowerId为例,找到右边比histogram[i]矮的矩形,停止,遇到比histogram[i]高的矩形j,直接跳到比histogram[j]矮的矩形rightLowerId[j].
#include<iostream> using namespace std; //the histogram stored from left to right long histogram[100001]; int rightLowerId[100001]; int leftLowerId[100001]; //from right to left void FindRightSideLowerRec(int n) { rightLowerId[n - 1] = n; // there is no rectangle on its right for (int i = n - 2; i >= 0; i--){ int cid = i + 1; while (histogram[cid] >= histogram[i] && cid < n){ cid = rightLowerId[cid]; // the key } rightLowerId[i] = cid; } } //from left to right void FindLeftSideLowerRec(int n) { leftLowerId[0] = -1; // there is no rectangle on its left for (int i = 1; i < n; i++){ int cid = i - 1; while (histogram[cid] >= histogram[i] && cid > -1){ cid = leftLowerId[cid]; // the key } leftLowerId[i] = cid; } } long long CalLargestRectangle(int n) { long long largestArea = 0; for (int i = 0; i < n; i++) { long long width = rightLowerId[i] - leftLowerId[i] - 1; long long height = histogram[i]; long long area = width * height; if (area > largestArea) largestArea = area; } return largestArea; } int main() { int n; while (scanf("%d", &n)){ if (n == 0) return 0; for (int i = 0; i < n; i++) { scanf("%d", &histogram[i]); } FindRightSideLowerRec(n); FindLeftSideLowerRec(n); long long larea = CalLargestRectangle(n); printf("%I64d\n", larea); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。