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; }

中大 9095. Islands的更多相关文章

  1. 中大东校小米路由器mini实现inode上网,ipv6 wifi【中大】【东校】【inode】【ipv6】

    还有不到4个月就要毕业了,前几天半夜没事捣鼓小米路由没想到竟然实现了wifi的ipv6. 正好又安利了同学一台小米路由mini,从刷机到inode到ipv6全搞了一遍. 这里将教程写出来,服务学弟妹. ...

  2. jdbc java数据库连接 11)中大文本类型的处理

    1. Jdbc中大文本类型的处理 Oracle中大文本数据类型, Clob    长文本类型   (MySQL中不支持,使用的是text) Blob    二进制类型 MySQL数据库, Text   ...

  3. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  4. [LeetCode] Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. Leetcode 200. number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  6. vim中大小写转换

    转自:http://www.cnblogs.com/fortran/archive/2010/07/25/1784513.html vim中大小写转化的命令是:gu或者gU,形象一点的解释就是小u意味 ...

  7. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Javascript中大括号“{}”的多义性

    摘要:本文主要介绍JavaScript中大括号有四种语义作用. JS中大括号有四种语义作用 语义1,组织复合语句,这是最常见的 if( condition ) { //... }else { //.. ...

  9. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

随机推荐

  1. 关于redis和memcached的一些想法

    看了redis和memcached,想到自己前几年以前会把内存分配及调用和程序写在一起.确实是不合理的.这样的话,主进程就会越来越大,而且模块也不是完全独立.不能做到松耦合. 实质就是把内存的读写I/ ...

  2. Homestead 修改 Homestead.yaml 文件后 vagrant up 报错的问题

    一般情况是 TAB 和空格的问题. 虽然表面看来,缩进是一致的. 但是 TAB 没能替换为空格,从而导致问题. 解决: $ sudo vim /etc/vim/vimrc.local syntax o ...

  3. Internet

    0x01 URL的解析/反解析/连接 解析 urlparse()--分解URL # -*- coding: UTF-8 -*- from urlparse import urlparse url = ...

  4. Ionic学习

    1. 原来Http不能直接加在普通类里,下面的报错 import { Component } from '@angular/core'; import { NavController } from ' ...

  5. Python3实战系列之三(获取印度售后数据项目)

    问题:续接上一篇.说干咱就干呀,勤勤恳恳写程序呀! 目标:实现第一个python程序的“Hello world!” 解决方案:新建一个项目Test,创建一个Test.py文件.在文件中实现打印出“He ...

  6. centos 6.5 配置ssh免登录

    生成密匙: ssh-keygen -t rsa 会生成 id_rsa  id_rsa.pub id_rsa:私匙 id_rsa.pub:公匙 配置当前机器免登录: cp id_rsa.pub auth ...

  7. jvm gc 算法

    1标记-清除法 他是现代垃圾回收算法的思想基础. 标记-清除算法将垃圾回收分为两个阶段:标记阶段和清除阶段. 在标记阶段,首先通过根节点,标记所有从根节点开始的可达对象(根搜索算法).而未被标记的对象 ...

  8. 使用 CXF 做 webservice 简单例子(转载)

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  9. 泛型c#(深入理解c#)

    1.泛型带来的好处非常像静态语言较之动态语言的优点:更好的编译时检查,更多在代码中能直接表现的信息,更多的IDE支持,更好的性能.泛型的好处之一就是在编译时执行更多的检查,所以等到编译不在报错时,就极 ...

  10. python文件打包

    python文件打包 先安装 pip3 install --upgrade pyinstaller 进入到文件的当前目录 ,在命令行中输入: pyinstaller -F -w[文件名].py 其中- ...