12265 - Selling Land(单调队列)
来源:互联网 发布:红衣小女孩 知乎 编辑:程序博客网 时间:2024/05/24 16:13
G Selling Land
As you may know, the country of Absurdistan is full of abnormalities. For example, the whole country can be divided into unit squares that are either grass or swamp. Also, the country is famous for its incapable bureaucrats. If you want to buy a piece of land (called a parcel), you can only buy a rectangular area, because they cannot handle other shapes. The price of the parcel is determined by them and is proportional to the perimeter of the parcel, since the bureaucrats are unable to multiply integers and thus cannot calculate the area of the parcel.Per owns a parcel in Absurdistan surrounded by swamp and he wants to sell it, possibly in parts, to some buyers. When he sells a rectangular part of his land, he is obliged to announce this to the local bureaucrats. They will first tell him the price he is supposed to sell it for. Then they will write down the name of the new owner and the coordinates of the south-east corner of the parcel being sold. If somebody else already owns a parcel with a south-east corner at the same spot, the bureaucrats will deny the change of ownership.Per realizes that he can easily trick the system. He can sell overlapping areas, because bureaucrats only check whether the south-east corners are identical. However, nobody wants to buy a parcel containing swamp.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 ≤ 1 000): 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 "pi x i". 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 in- and output
InputOutput16 5..#.#.#...#..##...#.#....#..#.
6 x 45 x 65 x 83 x 101 x 12
题意:一块n*m的土地,以每个右下角点为起点,向左向上找一个最大周长的矩形,最后统计个数输出每种周长矩形的个数。
思路:先把每一行对应每个位置竖直方向的最大值预处理出来,然后每一行利用单调队列去维护,因为如果前一个长于后一个,那么后一个在选矩形的时候高度肯定不会超过他本身,就不会达到前一个高度,所以前一个高度等同于没用,然后维护过程中注意最大值的更新方式,看竖直方向和水平方向往那边延伸的收益大,就可以判断出高度是要用他本身还是前一个的高度。
代码:
#include <stdio.h>#include <string.h>#include <queue>using namespace std;const int N = 1005;typedef pair<int, int> pi;int t, n, m, up[N][N], ans[2 * N];char str[N];void solve() {memset(ans, 0, sizeof(ans));for (int i = 1; i <= n; i++) {deque<pi>Q;for (int j = 1; j <= m; j++) {int r = j;while (!Q.empty() && Q.back().first >= up[i][j]) {r = Q.back().second;Q.pop_back();}if (up[i][j] == 0) continue;if (Q.empty() || up[i][j] - Q.back().first > r - Q.back().second) {ans[up[i][j] + j - r + 1]++;Q.push_back(make_pair(up[i][j], r));}else ans[j - Q.back().second + 1 + Q.back().first]++;}}for (int k = 2; k <= n + m; k++)if (ans[k]) printf("%d x %d\n", ans[k], k * 2);}int main() {scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%s", str + 1);for (int j = 1; j <= m; j++) {if (str[j] == '#') up[i][j] = 0;else up[i][j] = up[i - 1][j] + 1;}}solve();}return 0;}
1 0
- 12265 - Selling Land(单调队列)
- UVa 12265:Selling Land(单调栈)
- 12265 - Selling Land
- UVA 12265 Selling Land
- UVA-12265 Selling Land
- UVA 12265 Selling Land
- uva 12265 - Selling Land(dp)
- UVA - 12265 Selling Land 分析
- UVa OJ 12265 - Selling Land
- SPOJ 8628 Selling Land
- uva12265 selling land
- UVALive 4950 Selling Land
- UVa 12265 Selling Land 解题报告(降维)
- 【UVa12265】Selling Land 贩卖土地
- poj - 1156 - A STRIP OF LAND(枚举 + 单调队列 + 输入开挂)
- 单调栈 单调队列
- 单调队列
- 单调队列
- Qt美化界面的心得整理
- [F] Teacher's Problem(处理大数时,优化很重要)
- MVC小知识 读《MVC WEB 开发学习实录》 一书 摘录
- 揭秘:谷歌是如何考核员工的?看看他们的OKR制度
- 【转载】你必须了解的Session的本质(PHP)
- 12265 - Selling Land(单调队列)
- test
- 化学药品注册-化学药品注册分类
- 手机访问网站提示“网络信号不佳 无法打开网页” 服务器解决办法
- Solr 3.5 入门配置应用
- shell脚本中一些特殊符号
- 拷贝代码时没有仔细检查,导致误修改了函数参数
- 对字节对齐的理解
- Implement strStr()