9095. Islands
限制条件
时间限制: 2 秒, 内存限制: 256 兆
题目描述
Whenever it rains, Farmer John's field always ends up flooding. However, since the field isn't perfectly level, it fills up with water in a non-uniform fashion, leaving a number of "islands" separated by expanses of water.
FJ's field is described as a one-dimensional landscape specified by N (1 <= N <= 100,000) consecutive height values H(1)...H(n). Assuming that the landscape is surrounded by tall fences of effectively infinite height, consider what happens during a rainstorm: the lowest regions are covered by water first, giving a number of disjoint "islands", which eventually will all be covered up as the water continues to rise. The instant the water level become equal to the height of a piece of land, that piece of land is considered to be underwater.
An example is shown above: on the left, we have added just over 1 unit of water, which leaves 4 islands (the maximum we will ever see). Later on, after adding a total of 7 units of water, we reach the figure on the right with only two islands exposed. Please compute the maximum number of islands we will ever see at a single point in time during the storm, as the water rises all the way to the point where the entire field is underwater.
输入格式
Line 1: The integer N.
Lines 2..1+N: Line i+1 contains the height H(i). (1 <= H(i) <= 1,000,000,000)
输出格式
Line 1: A single integer giving the maximum number of islands that appear at any one point in time over the course of the rainstorm.
样例输入
835231423
样例输出
4
题目来源
2013年每周一赛第⑨场
在比赛时做这道题,我用两重循环。10万*10万。。
果断超时。。
后来想了一下,我每次从最低的岛屿上开始。我更新的只是最小的,然后就可以更新他周围的值。。
不用二重循环。。。
而且最重要的是,原来sicily的oj上是不能用qsort。
害的我调试了一个下午。提交了n的平方次。。
以后还是坚持用sort。。。安全!!
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <algorithm>
using namespace std;
int n;
int b[100004];
int flag[100004];
struct node
{
int x;
int i;
}node[200004];
bool cmp(struct node a,struct node b)
{
//struct node * a = (struct node *)c;
//struct node * b = (struct node *)d;
if(a.x>b.x)
return false;
else
return true;
}
int MAx(int c,int d)
{
return c>d?c:d;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int maxnum = 1;
memset(flag,0,sizeof(flag));
for(int k=0;k<n;k++)
{
scanf("%d",&node[k].x);
node[k].i = k;
b[k] = node[k].x;
}
node[n].x = -1;
sort(node,node+n,cmp);
int var = -1;
int varnum = 1;
for(int i=0;i<n;)
{
var = node[i].x;
while(node[i].x==var)
{
if(flag[node[i].i]==0)
{
flag[node[i].i] = 1;
int f = node[i].i+1;
while(f<n && flag[f]==0 && b[f]==var)
{
flag[f] = 1;
f++;
}
int f1 = node[i].i-1;
while(f1>=0 && flag[f1]==0 && b[f1]==var)
{
flag[f1] = 1;
f1--;
}
if((f>=n || flag[f]==1) && (f1<0 || flag[f1]==1))
varnum--;
if((f<n && flag[f]==0) && (f1>=0 && flag[f1]==0))
varnum++;
}
i++;
}
maxnum = MAx(varnum,maxnum);
}
printf("%d\n",maxnum);
}
return 0;
}

1万+

被折叠的 条评论
为什么被折叠?



