题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
 收藏
 取消关注
有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没。原本的大岛屿则会分为多个小岛屿,如果海平面一直上升,则所有岛都会被淹没在水下。
给出N个岛的高度。然后有Q个查询,每个查询给出一个海平面的高度H,问当海平面高度达到H时,海上共有多少个岛屿。例如:
岛屿的高度为:{2, 1, 3, 2, 3}, 查询为:{0, 1, 3, 2}。
当海面高度为0时,所有的岛形成了1个岛屿。
当海面高度为1时,岛1会被淹没,总共有2个岛屿{2} {3, 2, 3}。
当海面高度为3时,所有岛都会被淹没,总共0个岛屿。
当海面高度为2时,岛0, 1, 3会被淹没,总共有2个岛屿{3} {3}。
Input
第1行:2个数N, Q中间用空格分隔,其中N为岛的数量,Q为查询的数量(1 <= N, Q <= 50000)。
第2 - N + 1行,每行1个数,对应N个岛屿的高度(1 <= A[i] <= 10^9)。
第N + 2 - N + Q + 1行,每行一个数,对应查询的海平面高度(1 <= Q[i] <= 10^9)。
Output
输出共Q行,对应每个查询的岛屿数量。
Input示例
5 4
2
1
3
2
3
0
1
3
2
Output示例
1
2
0
2

一开始没什么思路,后来把岛屿和询问都记录位置,然后按照高低排序之后,时间减少了很多。

现在觉得这个题很水了。。。具体思路见代码。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n, q;
int question[50005];
int embz_island[50005];//表示各个位置的岛的淹没情况。embz_island[i]=1,表示位置为i的岛已经被淹没了。 struct qu
{
int va;
int pos;
}query[50005]; struct is
{
int height;
int pos;
}island[50005]; bool cmp1(is no1, is no2)
{
return no1.height < no2.height;
} bool cmp2(qu no1, qu no2)
{
return no1.va < no2.va;
} void work()
{
int i, j, ans, pos;
memset(embz_island, 0, sizeof(embz_island)); j = 0;//从高度为0的岛开始搜索
ans = 1;//一开始有1个岛站立
for (i = 0; i < q; i++)
{
for (; j < n; j++)
{
if (query[i].va >= island[j].height)
{
pos = island[j].pos;
embz_island[pos] = 1; if (pos == 1)//第一个岛屿与第n个岛屿在边上,需要特殊判断
{
if (embz_island[pos + 1] == 1)
ans--;
continue;
}
if (pos == n)
{
if (embz_island[pos - 1] == 1)
ans--;
continue;
}
if (embz_island[pos - 1] == 0 && embz_island[pos + 1] == 0)//左右两边都没有被淹,ans加一
{
ans++;
}
else if (embz_island[pos - 1] == 1 && embz_island[pos + 1] == 1)//左右两边都被淹了,ans减一
{
ans--;
}
}
else
{
break;
}
}
pos = query[i].pos;
question[pos] = ans;
}
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
{
scanf("%d", &island[i].height);
island[i].pos = i + 1;
}
sort(island, island + n, cmp1); for (i = 0; i < q; i++)
{
scanf("%d", &query[i].va);
query[i].pos = i;
}
sort(query, query + q, cmp2); work(); for (i = 0; i < q; i++)
{
printf("%d\n", question[i]);
} //system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

51nod 1276:岛屿的数量 很好玩的题目的更多相关文章

  1. 51nod 1276 岛屿的数量

    题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没 ...

  2. 51nod 1276 1276 岛屿的数量 (很好玩的题目

    题意: 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没.原本的大岛屿则会分为多个小岛屿,如果海平面一直上升,则所有岛都会被淹没在水下. 给出N个岛的高度.然后有 ...

  3. 51nod 1276

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1276 1276 岛屿的数量 题目来源: Codility 基准时间限制: ...

  4. [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 ...

  5. [LeetCode] 305. Number of Islands II 岛屿的数量 II

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

  6. Leetcode 200.岛屿的数量 - DFS、BFS

    Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7M ...

  7. 51nod 1378:夹克老爷的愤怒 很好玩的一道树状dp

    1378 夹克老爷的愤怒 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  取消关注 夹克老爷逢三抽一之后,由于采用了新师爷的策略,乡民们叫苦不堪,开始组织 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. python中sys和os的区别

    <os和sys的官方解释> ➤os os: This module provides a portable way of using operating system dependent ...

  2. Mark Grover

    https://www.ibm.com/developerworks/cn/data/library/bd-zookeeper/

  3. centos6.5安装图形操作界面

    yum -y install xorg-x11-fonts-Type1 #安装Xwindow yum -y groupinstall "X Window System" #安装GN ...

  4. B. Uniqueness 删除最小区间内的元素使得剩余元素唯一

    B. Uniqueness time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Referenced file contains errors

    Referenced file contains errors (file:/D:/TONG/tong/eclipse/config_/xsd/spring-context-4.2.xsd). For ...

  6. 奇异值分解(SVD)和主成分分析(PCA)

    参考: 奇异值分解:https://www.cnblogs.com/endlesscoding/p/10033527.html 主成分分析:https://blog.csdn.net/program_ ...

  7. LeetCode刷题--基础知识篇--KMP算法

    KMP算法 关于字符串匹配的算法,最知名的莫过于KMP算法了,尽管我们日常搬砖几乎不可能去亲手实现一个KMP算法,但作为一种算法学习的锻炼也是很好的,所以记录一下. KMP算法是根据三位作者(D.E. ...

  8. Day2-L-棋盘问题-POJ1321

    在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...

  9. Redis详解(一)——RDB

    Redis详解(一)--RDB 前言 由于 Redis 是一个内存数据库,所谓内存数据库,就是将数据库中的内容保存在内存中,这与传统的MySQL,Oracle等关系型数据库直接将内容保存到硬盘中相比, ...

  10. Link Analysis_1_Basic Elements

    1. Edge Attributes 1.1 Methods of category 1.1.1 Basic three categories in terms of number of layers ...