中大 9095. Islands

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年每周一赛第⑨场

这道题的思路是一个岛如果它的左边右边都是水则它的总数就减去1

左边或者右边有一边淹则不变,

两边都不淹则加1;

最边上的两个初始化为1

这道题的难点主要是分析问题

#include<iostream>
#include<stdio.h>
#include <algorithm> 
#include<cstring>
using namespace std;
struct island
{   int h;
int flage;
int num;
};
island point[100008];//这里有个小技巧point[0],跟point[n+1]初始化为淹
bool operator <(const island &a,const island &b)
{ 
    return a.h<b.h;
}
int main()
{
	int i,j,max,n,k,c;
	memset(point ,0,sizeof(point));
	while(scanf("%d",&n)!=EOF)
	{

		memset(point ,0,sizeof(point));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&point[i].h);
			point[i].num=i;
		}
		point[0].flage=1;
		point[n+1].flage=1;
		sort(point,point+n+1);
		j=1,max=1,k=1,c=1;
		
		for(j=1;j<=n;)
		{
		int temp;
		temp=point[j].h;
			
		while(point[j].h==temp)
			{
				point[point[j].num].flage =1;
				if( point[point[j].num-1].flage==0&&point[point[j].num+1].flage==0)
					max++;
				if(point[point[j].num-1].flage!=0&&point[point[j].num+1].flage!=0)
					max--;
				j++;

			}
			if(c<=max)
				c=max;
			
		}
		printf("%d\n",c);
	}
	return 0;
	
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值