题意:

有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. 525. Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  2. WindApi2 , WindOriginalApiLibrary 突然不兼容问题

    1. 在新的电脑上从tfs拉下代码后编译, windoriginalapilibrary 这个工程弹出对话框,要求转为vs2013编译,选择同意,编译成功 2.WindApi2 的Reference列 ...

  3. uoj#79. 一般图最大匹配(带花树)

    传送门 带花树 不加证明的说一下过程好了:每次从一个未匹配点\(S\)出发bfs,设\(S\)为\(1\)类点,如果当前点\(v\)在本次bfs中未经过,分为以下两种情况 1.\(v\)是未匹配点,那 ...

  4. [Xcode 实际操作]九、实用进阶-(17)使用CGBlendMode改变UIImage颜色,实现对图片进行混合着色

    目录:[Swift]Xcode实际操作 本文将演示如何使用CGBlendMode改变UIImage颜色,实现对图片进行混合着色. 在项目文件夹[DemoApp]上点击鼠标右键 ->[New Fi ...

  5. Mybatis源码解读-设计模式总结

    虽然我们都知道有26个设计模式,但是大多停留在概念层面,真实开发中很少遇到,Mybatis源码中使用了大量的设计模式,阅读源码并观察设计模式在其中的应用,能够更深入的理解设计模式. Mybatis至少 ...

  6. 大数据技术之_27_电商平台数据分析项目_02_预备知识 + Scala + Spark Core + Spark SQL + Spark Streaming + Java 对象池

    第0章 预备知识0.1 Scala0.1.1 Scala 操作符0.1.2 拉链操作0.2 Spark Core0.2.1 Spark RDD 持久化0.2.2 Spark 共享变量0.3 Spark ...

  7. pytest入门学习(2)

    pytest的hello world pyt1.py def func(x): print (x+1); return x+1; def test_answer(): assert func(3) = ...

  8. CF1175E Minimal Segment Cover 题解

    题意:给出\(n\)个形如\([l,r]\)的线段.\(m\)次询问,每次询问区间\([x,y]\),问至少选出几条线段,使得区间\([x,y]\)的任何一个部位都被至少一条线段覆盖. 首先有一个显然 ...

  9. 51Nod 1126 求递推序列的第N项(矩阵快速幂)

    #include <iostream> #include <algorithm> #include <cmath> #define MOD 7 #define N ...

  10. Python-1-基础

    获取用户输入 >>> x = input("x: ") x: 34 >>> y = input("y: ") y: 42 & ...