一个并查集问题的优化(CDOJ 203)
2014-07-14 23:15
465 查看
一个多月前在成电OJ上看到一个并查集的问题,当时刚看完一些基础的数据结构,所以就试着做了。当然没有做出来。这题坑就在于:每组测试数据有10^5个,地图还是1000*1000的。挨着个每次都搜一遍不太可能。容易超时。
解决方法:潮水高度是递增的。所以想象一个退潮的过程,把每次退潮露出的岛屿记录下来,从最后最高的潮水高度算起。把露出的岛屿加入集合,每个测试数据对应的答案可以接着在下一组继续使用。
Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a rectangular shape, but is also divided into an n×m grid.
Each grid field has a certain height. Unfortunately, the sea level started to raise and in year i,
the level is i meters.
Another strange feature of the island is that it is made of sponge, and the water can freely flow through it. Thus, a grid field whose height is at most the current sea level is considered flooded.Adjacent unflooded fields (i.e., sharing common edge) create
unflooded areas. Sailors are interested in the number of unflooded areas in a given year.
An example of a 4×5 island
is given below. Numbers denote the heights of respective fields in meters.Unflooded fields are darker; there are two unflooded areas in the first year and three areas in the second year.
Input
Multiple Test Cases
The input contains several test cases. The first line of the input contains a positive integer Z≤20,denoting the number of test cases. Then Z test cases follow, each conforming to the format described in section Single Instance Input. For each test case, your
program has to write an output conforming to the format described in section Single Instance Output.
Single Instance Input
The first line contains two numbers n and m separated by a single space, the dimensions of the island, where 1≤n,m≤1000. Next n lines contain m integers from the range [1,10^9] separated by single spaces, denoting the heights of the respective fields. Next
line contains an integer T (1≤T≤105). The last line contains T integers tj , separated by single spaces, such that 0≤t1≤t2≤⋯≤tT≤10^9
Output
Single Instance Output
Your program should output a single line consisting of T numbers rj , where rj is the number of unflooded areas in year tj . After every number ,you must output a single space
Sample Input
[b]Sample
Output
[/b]
解决方法:潮水高度是递增的。所以想象一个退潮的过程,把每次退潮露出的岛屿记录下来,从最后最高的潮水高度算起。把露出的岛屿加入集合,每个测试数据对应的答案可以接着在下一组继续使用。
Islands
Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a rectangular shape, but is also divided into an n×m grid.Each grid field has a certain height. Unfortunately, the sea level started to raise and in year i,
the level is i meters.
Another strange feature of the island is that it is made of sponge, and the water can freely flow through it. Thus, a grid field whose height is at most the current sea level is considered flooded.Adjacent unflooded fields (i.e., sharing common edge) create
unflooded areas. Sailors are interested in the number of unflooded areas in a given year.
An example of a 4×5 island
is given below. Numbers denote the heights of respective fields in meters.Unflooded fields are darker; there are two unflooded areas in the first year and three areas in the second year.
Input
Multiple Test Cases
The input contains several test cases. The first line of the input contains a positive integer Z≤20,denoting the number of test cases. Then Z test cases follow, each conforming to the format described in section Single Instance Input. For each test case, your
program has to write an output conforming to the format described in section Single Instance Output.
Single Instance Input
The first line contains two numbers n and m separated by a single space, the dimensions of the island, where 1≤n,m≤1000. Next n lines contain m integers from the range [1,10^9] separated by single spaces, denoting the heights of the respective fields. Next
line contains an integer T (1≤T≤105). The last line contains T integers tj , separated by single spaces, such that 0≤t1≤t2≤⋯≤tT≤10^9
Output
Single Instance Output
Your program should output a single line consisting of T numbers rj , where rj is the number of unflooded areas in year tj . After every number ,you must output a single space
Sample Input
1 4 5 1 2 3 3 1 1 3 2 2 1 2 1 3 4 3 1 2 2 2 2 5 1 2 3 4 5
[b]Sample
Output
[/b]
2 3 1 0 0
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; class Field{ public: int height, father; bool under; }; int Z; int row, col, T; int res[100000]; vector<int> height_list[100000]; vector<int> t; int i,j,result,temp; Field field[1000][1000]; int Father(int a, int b){ if (field[a][b].father!=a*col+b) field[a][b].father=Father(field[a][b].father/col, field[a][b].father%col); return field[a][b].father; } void Merge(int a, int b, int c, int d){ int f1, f2, x, y; f1=Father(a, b); f2=Father(c, d); if(f1==f2) return; else{ result--; x=field[a][b].father/col; y=field[a][b].father%col; field[x][y].father=f2; } } void Check(int r, int c){ if (r+1<row && !field[r+1][c].under) {Merge(r, c, r+1, c); } if (r-1>=0 && !field[r-1][c].under) {Merge(r, c, r-1, c); } if (c+1<col && !field[r][c+1].under) {Merge(r, c, r, c+1); } if (c-1>=0 && !field[r][c-1].under) {Merge(r, c, r, c-1); } } void Solve(){ int x, y, m, n; result=0; for(m=t.size()-1; m>=0; m--){ for(n=height_list[m].size()-1; n>=0; n--){ x=height_list[m] /col; y=height_list[m] %col; field[x][y].under=false; result++; Check(x, y); } res[m]=result; } } int main(){ scanf("%d", &Z); while(Z--){ result=0; t.clear(); for (i=0; i<100000; i++) height_list[i].clear(); scanf("%d%d", &row, &col); for(i=0; i<row; i++){ for(j=0; j<col; j++){ scanf("%d", &field[i][j].height); field[i][j].under=true; field[i][j].father=i*col+j; } } scanf("%d", &T); for(i=0; i<T; i++){ scanf("%d", &temp); t.push_back(temp); } for(i=0; i<row; i++){ for(j=0; j<col; j++){ temp=lower_bound(t.begin(), t.end(), field[i][j].height)-t.begin(); if(temp>0) height_list[temp-1].push_back(i*col+j); } } Solve(); for(i=0; i<(int)t.size(); i++) printf("%d ", res[i]); printf("\n"); } return 0; }
相关文章推荐
- 一个简单的优化例子(锁问题)
- [随笔] 6月近况小记 & 一个站点优化问题
- 最近帮人解决一个循环优化的问题
- 一个匹配优化的问题
- 一个关于防止编译器优化特定代码的问题
- 一个简单的优化例子(锁问题)
- Oracle优化一个视图发现的问题。
- 一个Web报表项目的性能分析和优化实践(二):MySQL数据库连接不够用(TooManyConnections)问题的一次分析和解决案例
- 一个Web报表项目的性能分析和优化实践(二):MySQL数据库连接不够用(TooManyConnections)问题的一次分析和解决案例
- 一个关于js 优化的问题
- 碰到一个关于搜索引擎优化的问题
- 关于 "ARM嵌入式系统开发--软件设计与优化" 这本书中的一个问题
- 关于oracle sql优化的一个容易忽略的问题。
- 一个Web报表项目的性能分析和优化实践(二):MySQL数据库连接不够用(TooManyConnections)问题的一次分析和解决案例
- 分享一个性能优化问题
- 一个简单的优化例子(锁问题)
- Matlab优化工具箱(optimization toolbox)使用时出现的一个问题
- [随笔] 6月近况小记 & 一个站点优化问题
- 一次ORA-4030问题诊断及解决【解决思路不错,说明了对象的统计信息与优化器的优化操作(即选择执行一个SQL语句在该优化参数环境下最佳的执行计划)间的关系】
- 一个 Sql语句优化的问题- STATISTICS 统计信息