UVA12265-Selling Land(单调栈)

Posted npugen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA12265-Selling Land(单调栈)相关的知识,希望对你有一定的参考价值。

Problem UVA12265-Selling Land

Accept: 137  Submit: 782
Time Limit: 3000 mSec

技术分享图片 Problem Description

技术分享图片

 

 

技术分享图片 Input

On the ?rst line a positive integer: the number of test cases, at most 100. After that per test case:

? One line with two integers n and m (1 ≤ n,m ≤ 1000): the dimensions of Per’s parcel.

? n lines, each with m characters. Each character is either ‘#’ or ‘.’. The j-th character on the i-th line is a ‘#’ if position (i,j) is a swamp, and ‘.’ if it is grass. The north-west corner of Per’s parcel has coordinates (1, 1), and the south-east corner has coordinates (n,m).

 

技术分享图片 Output

Per test case:
? Zero or more lines containing a complete list of how many parcels of each perimeter Per needs to sell in order to maximize his pro?t. More speci?cally, if Per should sell pi parcels of perimeter i in the optimal solution, output a single line ‘pixi’. The lines should be sorted in increasing order of i. No two lines should have the same value of i, and you should not output lines with pi = 0.
 

技术分享图片 Sample Input

1
6 5
..#.#
.#...
#..##
...#.
#....
#..#.
 

技术分享图片 Sample Output

6 x 4
5 x 6
5 x 8
3 x 10
1 x 12

 

题解:很多类似的有障碍物的关于矩形的题都会用到单调栈,算是个小经验吧,以后再碰到这类题多往这边想想。预处理一个最高延伸高度的height数组是非常自然的,在处理每一列时,如果当前列高度大于栈顶元素的高度,只用分析当前列是否可能成为最优解,如果可能就压入栈中,否则继续遍历。如果当前列高度小于等于栈顶元素,那就需要弹栈了,直到栈顶元素高度小于当前列高度,这时判断是否时最优解时,用的不是当前列的列数,而是弹栈的最后一个元素的列数,这是因为要最大化h-c,相同的方法判断能否成为最优解,决定是否压入栈中即可。

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int maxn = 1000 + 10;
 6 
 7 int n, m;
 8 int height[maxn], ans[maxn << 1];
 9 char gra[maxn][maxn];
10 
11 struct Node {
12     int c, h;
13     Node() {}
14     Node(int _c, int _h) : c(_c), h(_h) {}
15 };
16 
17 int main()
18 {
19     //freopen("input.txt", "r", stdin);
20     int iCase;
21     scanf("%d", &iCase);
22     while (iCase--) {
23         scanf("%d%d", &n, &m);
24         for (int i = 0; i < n; i++) {
25             scanf("%s", gra[i]);
26         }
27         memset(height, 0, sizeof(height));
28         memset(ans, 0, sizeof(ans));
29 
30         for (int i = 0; i < n; i++) {
31             stack<Node> sta;
32             while (!sta.empty()) sta.pop();
33             for (int j = 0; j < m; j++) {
34                 if (gra[i][j] == #) {
35                     while (!sta.empty()) sta.pop();
36                     height[j] = 0;
37                 }
38                 else {
39                     height[j]++;
40                     Node tmp(j, height[j]);
41                     while (!sta.empty() && sta.top().h >= tmp.h) {
42                         tmp.c = sta.top().c;
43                         sta.pop();
44                     }
45                     
46                     if (sta.empty()) {
47                         sta.push(tmp);
48                     }
49                     else {
50                         Node top = sta.top();
51                         if (top.h - top.c < tmp.h - tmp.c) sta.push(tmp);
52                     }
53                     Node top = sta.top();
54                     ans[j - top.c + top.h + 1]++;
55                 }
56             }
57         }
58 
59         for (int i = 1; i <= n + m; i++) {
60             if (ans[i]) printf("%d x %d
", ans[i], i * 2);
61         }
62     }
63     return 0;
64 }

 













以上是关于UVA12265-Selling Land(单调栈)的主要内容,如果未能解决你的问题,请参考以下文章

uva12265 Selling Land

uva12265 贩卖土地 单调栈

uva 12265 贩卖土地

UVA10213 How Many Pieces of Land数学+大数

UVa 10213 - How Many Pieces of Land ?(欧拉公式)

UVA - 10213 How Many Pieces of Land?(欧拉公式 + 高精度)