题意:

有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. HrrpClient使用

    使用HttpClient获取网页内容的过程 1.创建一个CloseableHttpClient类的实例: 2.使用这个实例执行HTTP请求,得到一个HttpResponse的实例: 3.最后,通过Ht ...

  2. 2.3 Hive的数据类型讲解及实际项目中如何使用python脚本对数据进行ETL

    一.hive Data Types https://cwiki. apache. org/confluence/display/HiveLanguageManual+Types Numeric Typ ...

  3. python-format函数

    #通过位置 print '{0},{1}'.format('chuhao',20) print '{},{}'.format('chuhao',20) print '{1},{0},{1}'.form ...

  4. Flutter实战视频-移动电商-30.列表页_商品列表UI界面布局

    30.列表页_商品列表UI界面布局 小程序里面的布局方式 小程序的图片上这里使用的是warp布局,因为首页里面火爆专区,已经用过了warp来布局了. 所以这里我们没有必要再讲一遍,这里我们使用List ...

  5. Fitnesse 访问日志配置

    1. 在build.xml中修改Finesse运行时的参数 <target name="run" depends="compile, compile-bootstr ...

  6. Go语言之父谈Go:大道至简

    http://www.csdn.net/article/2012-07-05/2807113-less-is-exponentially-more 摘要:导读:这篇文章是Google首席工程师.Go语 ...

  7. Nacos深入浅出(一)

    Nacos代码第一次给我的感觉有点小清新,下面就带大家抽丝剥茧看看源代码,看看阿里大神的东东: 建议大家先把Nacos跑起来,网上有很多教程,最好直接去git里面拉代码,在IDEA里面运行: cons ...

  8. mybatis-trim标签说明

    trim标签使用1.trim 有四个属性 2.prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容(注意:是没有prefixOverrides,suffixOverrides ...

  9. C 语言实例 - 字符串复制

    C 语言实例 - 字符串复制 C 语言实例 C 语言实例 将一个变量的字符串复制到另外一个变量中. 实例 - 使用 strcpy() #include <stdio.h> #include ...

  10. bzoj3196:Tyvj1730二逼平衡树

    传送门 暴力啊,直接树套树上啊 线段树套splay,卡卡常就直接A了 代码: #include<cstdio> #include<iostream> #include<a ...