题意:

有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

这个题目用了一些小技巧

可以看作1-n周围的已经被淹没 cnt = 2;

那么 cnt -1 就是答案

当一个岛被淹没 如果他的左右有一块被淹没的区域 那么cnt不变

当周围没被淹 那么cnt++

如果周围都被淹没 那么淹没的区域为cnt--

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<climits>
#include<vector>
using namespace std;
const int N = 5e4+;
int min_h = INT_MAX,max_h = INT_MIN;
struct data
{
int h,id; /* data */
}Q[N];
bool vis[N];
vector<int>v[N],num;
int H[N];
int ans[N];
int getid(int x)
{
x++;
return lower_bound(num.begin(),num.end(),x)-num.begin()-;
}
bool cmp(data a,data b)
{
return a.h<b.h;
}
int cnt = ;
void check(int pos)
{
vis[pos] = true;
if(vis[pos+]&&vis[pos-])
{
cnt--;
}
else{
if(vis[pos+]||vis[pos-])
{
;
}
else{
cnt++;
}
}
}
int main()
{
int n,q,t;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&t);
num.push_back(t);
H[i] = t;
max_h = max(max_h,t);
min_h = min(min_h,t);
}
sort(num.begin(),num.end());
num.erase(unique(num.begin(),num.end()),num.end() );
for(int i=;i<=n;i++)
{
//cout<<getid(H[i])<<endl;
v[getid(H[i])].push_back(i);
}
vis[] = vis[n+] = true; for(int i=;i<q;i++)
{
scanf("%d",&Q[i].h);
Q[i].id = i;
}
sort(Q,Q+q,cmp);
int pre = -;
for(int i=;i<q;i++)
{
if(Q[i].h>=max_h)
{
ans[Q[i].id] = ;
continue;
}
if(Q[i].h<min_h)
{
ans[Q[i].id] = ;
continue;
}
int now = getid(Q[i].h);
//cout<<endl<<"H: "<<Q[i].h<<" id: "<<now<<endl;
for(int j = pre+;j<=now;j++)
{
int len = v[j].size();
for(int k=;k<len;k++)
{
check(v[j][k]);
//cout<<v[j][k]<<" ";
}
}
pre = now;
ans[Q[i].id] = cnt - ;
}
for(int i=;i<q;i++)
{
printf("%d\n",ans[i]);
}
return ;
}

AC代码

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

  1. 51nod 1276:岛屿的数量 很好玩的题目

    1276 岛屿的数量 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  取消关注 有N个岛连在一起形成了一个大的岛屿,如果海平 ...

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

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

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

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

  5. 51nod 1276 岛屿的数量

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

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

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

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

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

  9. 51nod 1042数字0-9的数量

    1042 数字0-9的数量  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19 ...

随机推荐

  1. Qt Creator Theme FlatDark 配色

    1.预处理指令,宏定义 颜色 #FF6AAD 2.普通代码 颜色 #D6CF9A 3.头文件 #D69545 4.系统限定符(namespace, class, public, typedef等)  ...

  2. maven:mirrors和repository的关系区别

    原文地址:http://my.oschina.NET/sunchp/blog/100634 1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相 ...

  3. CodeForces - 566D Restructuring Company 并查集的区间合并

    Restructuring Company Even the most successful company can go through a crisis period when you have ...

  4. E20190324-hm

    splice  vt. 绞接; 捻接(两段绳子); 胶接; 粘接(胶片.磁带等); n. 胶接处,粘接处,铰接处;

  5. [Swift 开发] 使用闭包传值(typealias)

    在Swift中使用闭包来实现两个界面的传值 例如:有A类和B类. B类 //声明闭包 typealias valueBlock = (Float)->() var returnPrice: va ...

  6. 51nod1459【二级最短路】

    标签说的是BFS... 太菜,不知道怎么BFS...是不是spfa写,就叫BFS...感觉不是.... 只是二级最短路的写法,直接搞就很容易了,简单题: #include <bits/stdc+ ...

  7. 我叫mt3.0更新公告

    1.增加卡牌: (1)炮姐,种族:牛头人,职业:元素萨满,可在任务中使用万能碎片兑换,最多兑换5张: (2)娘子,种族:德莱尼人,职业:治疗萨满,进化材料为死亡视察者: 详情点击查看>>& ...

  8. 自然语言处理(三)——PTB数据的batching方法

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 从文本文件中读取数据,并按照下面介绍的方案将数据整理成batch. 方法是:先将整个文档切分成若干连续段落,再让b ...

  9. NPOI用WorkbookFactory读写 2007以上格式文件(xlsx)

    //我用的最新的2.2.1版本 //第一步:引用DLL,5个全导入,包括ICSHARP.ZIP,是个开源压缩工具包.XLSX是压缩格式,需要它来解压 //第二部: using NPOI.SS.User ...

  10. 在Mac上安装Ubuntu14.04虚拟机

    1.在macOS High Sierras上安装VMware for mac 下载地址:VMware Fusion 8.5.1  https://pan.baidu.com/s/1skQ1OyL 2. ...