2014 UESTC Training for Data Structures A - Islands

A - Islands

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

Submit Status

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,109] separated
by single spaces, denoting the heights of the respective fields.
Next line contains an integer T (1≤T≤105).
The last line contains Tintegers tj ,
separated by single spaces, such that 0≤t1≤t2≤?≤tT≤109

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 Input Sample 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

题意就是有一个矩阵,求第i天矩阵中大于i的部分连通块的个数。

题目会给出i,i是递增的,所以可以从最后一天做起,每天被昨天淹没且不被今天淹没的大陆浮起来,并将其与已经浮起来的大陆连接起来,再求连通块的个数。连接这里需要用到并查集,而由于i最大是10^9,但i的询问个数不超过10000,所以要离散化。

这道题还有个坑就是时间限制居然是10s,一开始怎么想都是超时=。=


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
int q[1005][1005];
vector<int> w;
vector<int> x[100005];
int found[1005][1005];
int father[1005][1005];
int hang,lie,ans;
int Rank[1100005]={};
int getfather(int j,int k)
{
if (father[j][k]!=j*1000+k-1)
father[j][k]=getfather(father[j][k]/1000,father[j][k]%1000+1);
return father[j][k];
}
void judge(int g,int h,int j,int k)
{
int fx,fy;
if ((fx=getfather(g,h))==(fy=getfather(j,k))) return ;
ans--;
if (Rank[fx]>Rank[fy]) father[fy/1000][fy%1000+1]=fx;
else{
father[fx/1000][fx%1000+1]=fy;
if (Rank[fx]==Rank[fy])
Rank[fx]++;
}
return;
}
void dfs(int g,int h)
{
if (found[g][h]) return;
found[g][h]++;
ans++;
if (found[g+1][h] && g<hang) judge(g,h,g+1,h);
if (found[g-1][h] && g>1) judge(g,h,g-1,h);
if (found[g][h+1] && h<lie) judge(g,h,g,h+1);
if (found[g][h-1] && h>1) judge(g,h,g,h-1);
return ;
}
int main()
{
int g,h,j,k,l,n,T;
cin>>T;
while (T--){
memset(found,0,sizeof(found));
cin>>g>>h;
memset(q,0,sizeof(q));
for (j=1;j<=g;j++)
for(k=1;k<=h;k++)
scanf("%d",&q[j][k]);
cin>>l;
hang=g;
lie=h;
for (j=1;j<=g;j++) for (k=1;k<=h;k++) father[j][k]=j*1000+k-1;
ans=0;
memset(Rank,0,sizeof(Rank));
vector <int>().swap(w);
for (j=0;j<100005;j++) vector <int>().swap(x[j]);
w.push_back(0);
for (j=1;j<=l;j++){
scanf("%d",&n);
w.push_back(n);
}
w.push_back(1000000007);
for (j=1;j<=g;j++)
for(k=1;k<=h;k++)
q[j][k]=lower_bound(w.begin(),w.end(),q[j][k])-w.begin();
for (j=1;j<=g;j++)
for (k=1;k<=h;k++)
if (q[j][k]>=0) x[q[j][k]].push_back(j*1000+k-1);
for (j=w.size()-2;j>0;j--){
for (int k=0;k<x[j+1].size();k++)
dfs((int)x[j+1][k]/1000,(int)x[j+1][k]%1000+1);
w[j]=ans;
}
for (j=1;j<w.size()-1;j++) printf("%d ",w[j]);
cout<<endl;
}
return 0;
}

这道题代码不是很长,我居然前后用了6个小时才AC,看来是我太弱了。。。。。

2014 UESTC Training for Data Structures A - Islands,布布扣,bubuko.com

时间: 2024-10-13 09:51:05

2014 UESTC Training for Data Structures A - Islands的相关文章

2014 UESTC Training for Data Structures H - Cookies Test

H - Cookies Test Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status As chief programmer at a cookie production plant you have many responsibilities, one of them being that the cookies produced and packag

2014 UESTC Training for Data Structures K - 方师傅与栈

K - 方师傅与栈 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 方师傅有一个1?N的排列,排列的顺序是固定的,他想要把这个排列重新排列成他喜欢的顺序. 于是他买了一个栈,他会按顺序将排列扔进栈内,在某些时刻将栈顶元素取出,这样出栈后的排列就可以重新排序啦. 例如,原序列是1,2,他先将1入栈,再将2入栈,然后将2出栈,最后将1出栈,那么新序列就变

2014 UESTC Training for Data Structures J - 方师傅的01串

J - 方师傅的01串 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 方师傅过生日啦,于是蟹毛买了N个01串,想送给方师傅. 但是蟹毛觉得这些01串不够美,于是他想从中选出一些送给方师傅. 蟹毛对于p个01串的美值定义为: 这些01串的最长公共前缀的长度×p 所以蟹毛想从N个01串中选出一些,使得这些01串的美值最高. 请告诉蟹毛最好的美值是多少.

2014 UESTC Training for Data Structures C - 东风不与周郎便

C - 东风不与周郎便 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status "揽二乔于东南兮,乐朝夕之与共" 一首铜雀台赋,将愤怒与恐惧散播在了孙吴大军之中. 对抗曹军,万事俱备,只欠东风. 现在已经找到n个风眼,这些风眼的东风有强有弱,诸葛亮说他每次祈风都能够将一段风眼的东风增强,但需人去帮他布阵.同时他需要时刻掌控风眼的状况,以确定下一步的

2014 UESTC Training for Data Structures F - 天下归晋

F - 天下归晋 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 晋朝统一天下已有十年,曹魏.孙吴.蜀汉这些曾与天下相争的王朝,都成为了过眼云烟,仅留于故事之中. "爷爷,讲嘛讲嘛,再讲一次赤壁之战的故事嘛!" "你个死小子,都讲多少遍了,还听!" "就是想听打仗嘛!" "你小子啊...行,

2014 UESTC Training for Data Structures E - 休生伤杜景死惊开

E - 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 陆伯言军陷八卦阵之中,分明只是一条直路,却怎的也走不到尽头.阵中尽是石堆,以某一石堆为参考,无论向走还是向右,总是会回到出发的石堆,最后幸得一黄姓老翁带路才得脱出. 陆伯言逃离八卦阵后,来到山顶观察此阵,记从左往右第i堆石堆的高度为Ai,发现任何两堆较矮的石堆都能和它们之间的一

2014 UESTC Training for Data Structures D - 长使英雄泪满襟

看出司马懿在等蜀军粮草不济,孔明于是下令分兵屯田以备久战. 司马懿日日只是闭营不出.孔明每日思虑对策至天明,却也无可奈何. 漫天星辰中有一类星叫做将星,它们随着将魂的燃烧会越发明亮. 今夜五丈原的星空格外璀璨. 司马懿一声轻叹.五丈原的星,要落了. Input 第一行输入三个整数,n,W,H,分别表示有n颗星,矩形宽W,高H,. 接下来n行,每行三个整数,xi,yi,wi.表示第i颗星星的在天空的位置是(xi,yi),亮度是wi. 1≤n≤100000,1≤W,H≤1000000,0≤x,y≤1

2014 UESTC Training for Data Structures G - 程序设计竞赛

G - 程序设计竞赛 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status "你动规无力,图论不稳,数据结构松散,贪心迟钝,没一样像样的,就你还想和我同台竞技,做你的美梦!今天这场比赛,就是要让你知道你是多么的无能!!" 不训练,无以为战.有n项能力是ACM竞赛要求的,训练则能提升,忽略则会荒废. 这m天,你能做到如何. Input 第一行两个整

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&