51nod 1276 岛屿的数量
第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)。
输出共Q行,对应每个查询的岛屿数量。
5 4
2
1
3
2
3
0
1
3
2
1
2
0
2
#include <algorithm>
#include <cstdio>
#define N 50005
using namespace std;
struct node
{
int id,ans,h;
}Q[N];
struct nodee
{
int id,h;
bool operator<(nodee a)const
{
return h<a.h;
}
}A[N];
bool cmp(node a,node b)
{
return a.h<b.h;
}
bool comp(node a,node b)
{
return a.id<b.id;
}
int n,q;
bool ym[N];
int Main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;++i) scanf("%d",&A[i].h),A[i].id=i;
sort(A+,A++n);
for(int i=;i<=q;++i)
{
scanf("%d",&Q[i].h);
Q[i].id=i;
}
sort(Q+,Q++q,cmp);
int j=,sum=;
for(int i=;i<=q;++i)
{
while(A[j].h<=Q[i].h&&j<=n)
{
if(A[j].id==) {if(ym[]) sum--;}
else if(A[j].id==n) {if(ym[n-]) sum--;}
else
{
if(ym[A[j].id-]&&ym[A[j].id+]) sum--;
else if(!ym[A[j].id-]&&!ym[A[j].id+]) sum++;
}
ym[A[j].id]=;
j++;
}
Q[i].ans=sum;
}
sort(Q+,Q++q,comp);
for(int i=;i<=q;++i) printf("%d\n",Q[i].ans);
return ;
}
int sb=Main();
int main(int argc,char *argv[]){;}
51nod 1276 岛屿的数量的更多相关文章
- 51nod 1276 1276 岛屿的数量 (很好玩的题目
题意: 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没.原本的大岛屿则会分为多个小岛屿,如果海平面一直上升,则所有岛都会被淹没在水下. 给出N个岛的高度.然后有 ...
- 51nod 1276:岛屿的数量 很好玩的题目
1276 岛屿的数量 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 有N个岛连在一起形成了一个大的岛屿,如果海平 ...
- 51nod 1276
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1276 1276 岛屿的数量 题目来源: Codility 基准时间限制: ...
- [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 ...
- [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 ...
- Leetcode 200.岛屿的数量 - DFS、BFS
Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7M ...
- [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 ...
- [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 ...
- 51nod 1276 【离线化】
思路1.: 离线处理: 具体就是把岛屿离线然后按照高度排序,把query按照从高到低排序,然后每次query只要从最高的岛屿开始找起,判断条件:如果他旁边都是没有被找过的(也就是默认是海),那么数量+ ...
随机推荐
- DL杂谈
好久不写了,几点这次项目经验吧,本次训练位多任务训练,主要目的训练人脸角度,具体公司项目不细谈. 讲一下主要碰到的坑: 1 最主要问题,网络结构不对称,导致主任务与辅助任务之间的梯度关系不平衡从而导致 ...
- Primer回顾 标准库类型
string类型的输入操作符: 1.读取并忽略开头所有的空白字符(如空格,换行符,制表符). 2.读取字符直至再次遇到空白字符,读取终止. 用getline读取整行文本 getline.接受两个参 ...
- 2、R-reshape2-melt
1.melt: 短数据转长数据 (1).融合的数据为数组.表以及矩阵,melt的表达式为: melt(data, varnames = names(dimnames(data)), . ...
- ural 1500 Pass Licenses (状态压缩+dfs)
1500. Pass Licenses Time limit: 2.5 secondMemory limit: 64 MB A New Russian Kolyan believes that to ...
- 5、opencv中的绘图函数
1.目标 a.学习使用 OpenCV 绘制不同几何图形 b. 你将会学习到这些函数: cv2.line(), cv2.circle(), cv2.rectangle(),cv2.ellipse(),c ...
- php+redis实现高并发模拟下单、秒杀、抢购操作
对于高并发下的场景,一般都是采用redis缓存机制来处理. 当然也不是只有redis可以处理.还有利用mysql事务操作锁住操作的行.文件锁. 不过这些方式都没有redis缓存高效.可靠. 模拟的过程 ...
- 如何快速编写大项目的Makefile文件
在构建C++的后台服务时,经常需要自己来编写makefile文件,而如果没有合适的方法或模板时,编写makefile文件是一件很费时费力的事情.因此,为了帮助程序员高效准确的编写makefile文件, ...
- ue4 bp singleton
.h UCLASS(Blueprintable) class USingletonBP: public UObject { GENERATED_UCLASS_BODY() /** * Singleto ...
- IT兄弟连 Java语法教程 Java语言入门 典面试题
1.请说明JVM.JRE和JDK是什么?它们有什么关系? JVM是Java虚拟机,Java Virtual Machine的缩写,是一个虚构出来的计算机,通过在实际的计算机上仿真模拟各种计算机功能来实 ...
- Visio画图(一):UML用例图
Visio画图(一):UML用例图 1.找到UML用例图 A.有网状态 第一步 在搜索框内输入用例图进行搜索. 第二步,移动鼠标直到找到用例图 B.无网状态 第一步 点击特别推荐旁的类别选项 第二步 ...