Problem UVA12265-Selling Land

Accept: 137  Submit: 782
Time Limit: 3000 mSec

Problem Description

Input

On the first 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 profit. More specifically, 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 = + ;
  6.  
  7. int n, m;
  8. int height[maxn], ans[maxn << ];
  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 = ; i < n; i++) {
  25. scanf("%s", gra[i]);
  26. }
  27. memset(height, , sizeof(height));
  28. memset(ans, , sizeof(ans));
  29.  
  30. for (int i = ; i < n; i++) {
  31. stack<Node> sta;
  32. while (!sta.empty()) sta.pop();
  33. for (int j = ; j < m; j++) {
  34. if (gra[i][j] == '#') {
  35. while (!sta.empty()) sta.pop();
  36. height[j] = ;
  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 + ]++;
  55. }
  56. }
  57. }
  58.  
  59. for (int i = ; i <= n + m; i++) {
  60. if (ans[i]) printf("%d x %d\n", ans[i], i * );
  61. }
  62. }
  63. return ;
  64. }

UVA12265-Selling Land(单调栈)的更多相关文章

  1. uva12265 贩卖土地 单调栈

    输入一个n*m的矩阵,每个格子可能是空地,也可能是沼泽.对于每个空地格子,求出以它为右下角的空矩形的最大周长,然后统计每个周长出现了多少次. 输入包含多组测试数据,第一行输入一个正整数N,表示输入样例 ...

  2. uva12265 Selling Land

    见紫书.(c,h)的更新策略://前面的高度为0了,直接插入因为ans==-c+h,c大,h还小,那么肯定不是最优左上角,更新新加入列的列//新的一列高度最小,就删掉了其他的,只留这个高度从上到下,从 ...

  3. UVa 12265 (单调栈) Selling Land

    紫书上分析了很多很多,超详细,= ̄ω ̄= 每扫描一行可以计算一个height数组,表示从这块空地向上延伸多少块空地,而且这个数组可以逐行递推. 首先对于每一行来说维护一个单调栈,栈里放的是矩形的左上角 ...

  4. 【洛谷 P2900】 [USACO08MAR]土地征用Land Acquisition(斜率优化,单调栈)

    题目链接 双倍经验 设\(H\)表示长,\(W\)表示宽. 若\(H_i<H_j\)且\(W_i<W_j\),显然\(i\)对答案没有贡献. 于是把所有点按\(H\)排序,然后依次加入一个 ...

  5. DP的各种优化(动态规划,决策单调性,斜率优化,带权二分,单调栈,单调队列)

    前缀和优化 当DP过程中需要反复从一个求和式转移的话,可以先把它预处理一下.运算一般都要满足可减性. 比较naive就不展开了. 题目 [Todo]洛谷P2513 [HAOI2009]逆序对数列 [D ...

  6. zjnu1735BOB (单调队列,单调栈)

    Description Little Bob is a famous builder. He bought land and wants to build a house. Unfortunately ...

  7. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  8. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

  9. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1054[Submit][Status ...

随机推荐

  1. Spring核心——Bean的定义与控制

    在Sring核心与设计模式的文章中,分别介绍了Ioc容器和Bean的依赖关系.如果阅读过前2文就会知道,Spring的整个运转机制就是围绕着IoC容器以及Bean展开的.IoC就是一个篮子,所有的Be ...

  2. Java简单介绍及Java生态

    核心思想:面向对象编程,继承,高兼容(代码移植性强),开源,避免重复造轮子(使用mybatis,spring,redis等技术只需要将jar包依赖添加到项目中即可,jar包内就是技术核心代码,而这些框 ...

  3. 点到圆弧的距离(csu1503)+几何

    1503: 点到圆弧的距离 Time Limit: 1 Sec  Memory Limit: 128 MB  Special JudgeSubmit: 325  Solved: 70[Submit][ ...

  4. Redis配置解读

  5. Java并发编程-CountDownLatch

    基于AQS的前世今生,来学习并发工具类CountDownLatch.本文将从CountDownLatch的应用场景.源码原理解析来学习这个并发工具类. 1. 应用场景 CountDownLatch是并 ...

  6. img图像标签和超链接标签a

    图像标签语法:<img src="" alt="".../> img属性:src=""  显示图像的URLalt="& ...

  7. Bootstrap 按钮颜色

    上述按钮CSS规则 .btn { display: inline-block; padding: 6px 12px; margin-bottom: 0; font-size: 14px; font-w ...

  8. MySQL数据库锁类型

    锁概念 : 当高并发访问同一个资源时,可能会导致数据不一致,需要一种机制将用户访问数据的顺序进行规范化,以保证数据库数据的一致性.锁就是其中的一种机制. 一个栗子 :以买火车票为例,火车票可面向广大消 ...

  9. 如何用ABP框架快速完成项目(14) - 结尾? 当然不是, 这只是开始!

    此文当前版本号: 3 最近更新时间: 2018-12-9 04:52   本课程是方向性课程, 目的是避免南辕北辙. 方向盘一旦打正确, 还得需要以下文章去写好具体程序: 前面每篇文章里面的链接, 比 ...

  10. 使用MUI/html5plus集成微信支付需要注意的几点问题

    1)需要在服务器根目录放上证书,从微信开放平台下载 2)客户端组件目录名一定要按照微信要求