码迷,mamicode.com
首页 > 其他好文 > 详细

UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>

时间:2015-05-01 01:45:22      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

J - Islands

Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

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 Z20,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 1n,m1000. Next n lines contain m integers from the range [1,109] separated by single spaces, denoting the heights of the respective fields. Next line contains an integer T (1T105). The last line contains T integers tj , separated by single spaces, such that 0t1t2?tT109

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 and output

Sample InputSample Output
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
2 3 1 0 0

 

解题报告

注意到随着时间的增多,原先的集合会分裂,那么倒着来看,集合则会合并!,倒序维护所有时间询问即可,采用并查集来实现集合的合并,注意合并的几种情况(对整体集合数量的增减),即可解决本题.

 

  1 #include <iostream>
  2 #include <cstring>
  3 #include <set>
  4 using namespace std;
  5 const int maxn = 1e6 + 50;
  6 int pre[maxn],n,m,querysize;
  7 int query[maxn],ans[maxn];
  8 int dir[4][2] = {0,1,0,-1,-1,0,1,0};
  9 bool colour[1010][1010];
 10 
 11 inline int getid(int x,int y)
 12 {
 13    return x*m+y;
 14 }
 15 
 16 typedef struct Area
 17 {
 18   int x,y,h;
 19   friend bool operator < (const Area&a,const Area& b)
 20    {
 21          return a.h < b.h;
 22    }
 23   Area(const int &x,const int &y,const int &h)
 24    {
 25          this->x = x ,this-> y = y , this->h = h; 
 26    }    
 27 };
 28 
 29 multiset<Area>s;
 30 
 31 
 32 int getfather(int cur)
 33 {
 34     if (pre[cur] == cur)
 35      return cur;
 36     else
 37      return pre[cur] = getfather(pre[cur]);
 38 }
 39 
 40 
 41 int union_(int tx,int ty)
 42 {
 43    int res = 0;
 44    int id = getid(tx,ty);
 45    int data[4],datasize = 0;
 46    for(int i = 0 ; i < 4 ; ++ i)
 47     {
 48        int newx = tx + dir[i][0];
 49        int newy = ty + dir[i][1];
 50        if (newx < 0 || newx >= n || newy < 0 || newy >= m)
 51         continue;
 52        if (colour[newx][newy])
 53         data[datasize++] = getid(newx,newy);
 54     }
 55    colour[tx][ty] = true;
 56    if (!datasize)
 57     return 1;
 58    else
 59     {
 60        int res = 0;
 61        pre[getfather(id)] = getfather(data[0]);
 62        for(int i = 1 ; i < datasize ; ++ i)
 63         {
 64             if (getfather(data[i]) != getfather(data[0]))
 65               {
 66                    pre[getfather(data[i])] = getfather(data[0]);    
 67                    res++;
 68               }
 69         }
 70        return -res;
 71     }
 72 }
 73 
 74 int main(int argc,char *argv[])
 75 {
 76   int Case;
 77   scanf("%d",&Case);
 78   while(Case--)
 79    {
 80          s.clear();
 81          memset(colour,false,sizeof(colour));
 82          scanf("%d%d",&n,&m);
 83          for(int i = 0 ; i < n ; ++ i)
 84           for(int j = 0 ; j < m ; ++ j)
 85            {
 86               int temp;
 87               scanf("%d",&temp);
 88               s.insert(Area(i,j,temp));
 89         }
 90       for(int i = 0 ; i < n*m ; ++ i)
 91            pre[i] = i;
 92       scanf("%d",&querysize);
 93       for(int i = 0 ; i < querysize ; ++ i)
 94        scanf("%d",&query[i]);
 95       int curans = 0;
 96       for(int i = querysize-1 ; i >= 0 ; -- i)
 97        {
 98              int h = query[i];
 99              set<Area>::iterator it = s.upper_bound(Area(0,0,h));
100           while(it != s.end())
101            {
102                  int tx = it->x , ty = it->y;
103                  curans += union_(tx,ty);
104                  s.erase(it++);
105            }
106           ans[i] = curans;
107        }
108       for(int i = 0 ; i < querysize ; ++ i)
109        printf("%d ",ans[i]);
110       printf("\n");
111    }  
112   return 0;
113 }

 

 

UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>

标签:

原文地址:http://www.cnblogs.com/Xiper/p/4470222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!