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只要从最高的岛屿开始找起,判断条件:如果他旁边都是没有被找过的(也就是默认是海),那么数量+ ...
随机推荐
- Newtonsoft.Json null值不序列化
如果对当前序列化的实体全部生效的话使用如下: var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandl ...
- posix 正则库程序
使用的是posix 正则库,参考: http://see.xidian.edu.cn/cpp/html/1428.html 执行匹配的时: gcc myreg.c ip.pat 内容: ip.*[0- ...
- ASP.NEt ajax 弹出窗口在页面无法关闭
<meta http-equiv="X-UA-Compatible" content="IE=9" /> 今天又有客戶投訴公司的網頁有問題. ...
- C# 写 LeetCode easy #13 Roman to Integer
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and ...
- CV codes代码分类整理合集 《转》
from:http://www.sigvc.org/bbs/thread-72-1-1.html 一.特征提取Feature Extraction: SIFT [1] [Demo program] ...
- python 之 包的使用
6.8 包的使用 包就是一个包含有init.py文件的文件夹,所以其实我们创建包的目的就是为了用文件夹将文件/模块组织起来 强调: 在python3中,即使包下没有__init__.py文件,impo ...
- 集合之ArrayList,HashSet,HashMap
结合框架的体系结构: 一.List(列表) 1. List的特点 (1)List是元素有序并且可以重复的集合,称为序列 (2)List可以精确的控制每个元素的插入位置,或删除某个位置的元素 (3)Li ...
- Django-Rest-Framework的解析器和渲染器
Django-Rest-Framework的解析器和渲染器 restful framework 解析器 解析器的作用就是服务端接收客户端传来的数据,把数据解析成自己想要的数据类型的过程 本质就是对请 ...
- byte取高4位,低4位,byte转int
byte abyte =-1; System.out.println(abyte); System.out.println(Integer.toBinaryString(abyte)); //取高四位 ...
- redis 拒绝远程访问解决
启动时报的警告: 1.Warning: no config file specified, using the default config. In order to specify a config ...