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,相同的方法判断能否成为最优解,决定是否压入栈中即可。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int n, m;
int height[maxn], ans[maxn << ];
char gra[maxn][maxn]; struct Node {
int c, h;
Node() {}
Node(int _c, int _h) : c(_c), h(_h) {}
}; int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++) {
scanf("%s", gra[i]);
}
memset(height, , sizeof(height));
memset(ans, , sizeof(ans)); for (int i = ; i < n; i++) {
stack<Node> sta;
while (!sta.empty()) sta.pop();
for (int j = ; j < m; j++) {
if (gra[i][j] == '#') {
while (!sta.empty()) sta.pop();
height[j] = ;
}
else {
height[j]++;
Node tmp(j, height[j]);
while (!sta.empty() && sta.top().h >= tmp.h) {
tmp.c = sta.top().c;
sta.pop();
} if (sta.empty()) {
sta.push(tmp);
}
else {
Node top = sta.top();
if (top.h - top.c < tmp.h - tmp.c) sta.push(tmp);
}
Node top = sta.top();
ans[j - top.c + top.h + ]++;
}
}
} for (int i = ; i <= n + m; i++) {
if (ans[i]) printf("%d x %d\n", ans[i], i * );
}
}
return ;
}

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. 12. ReadWriteLock 读写锁

    package com.gf.demo11; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent. ...

  2. OOP设计模式在路上(一)——简单工厂模式

    前言 目前以LabVIEW为主要开发工具,熟悉常规开发框架(队列+状态机),个人用得比较多也感觉比较好用和强大的(JKI,AMC),也用它们开发过一些测试平台,但感觉到了一个瓶颈期,想寻求突破,提升L ...

  3. mvc中查询字符串请求过长

    最近在mvc中做导出Excel功能,通过页面把字段id和对应的中文名称通过a标签传给控制器的过程中,总是报错. 1.第一次错误截图 具体解决方案: 可以配置 IIS 服务器以拒绝查询字符串长度大于指定 ...

  4. spring boot之hello

    自己使用springboot也已经写过一段时间的代码,但是对springboot真正运行的流程还是有点模糊,今天写出自己对springboot的认识,如有不对,还请各位大佬不吝赐教,话不多说,直接上代 ...

  5. input属性为number时,如何去掉+、-号?

    直接上答案 <style> input[type='number']{-moz-appearance:textfield;} input[type=number]::-webkit-inn ...

  6. RPC理论以及Dubbo的使用介绍

    RPC 的主要功能目标是让构建分布式应用更容易,在提供强大的远程调用能力时不损失本地调用的语义简洁性. 为实现该目标,RPC 框架需提供一种透明调用机制让使用者不必显式的区分本地调用和远程调用. RP ...

  7. Android为TV端助力 比较完善json请求格式

    public static String getHttpText(String url) { if (MyApplication.FOR_DEBUG) { Log.i(TAG, "[getH ...

  8. verilog实现红黄蓝三秒灯

    代码如下 test.v文件 led.v文件 module test(); wire led_r,led_g,led_b; ; clk <= ~clk; led c1 ( .clk(clk), . ...

  9. The process could not read file xxx due to OS error 53

      在不同地域的两个SQL Server服务器上配置了复制(Replication)用于同步数据(生产环境配置有Replication,测试环境也配有Replication),两地通过专线连接起来,这 ...

  10. [20180823]IMU与db link.txt

    [20180823]IMU与db link.txt --//当使用db link查看远程表时,实际上会产生小小的日志.--//当时如果与IMU结合在一起,可以导致IMU的失效. 1.环境:SCOTT@ ...