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只要从最高的岛屿开始找起,判断条件:如果他旁边都是没有被找过的(也就是默认是海),那么数量+ ...
随机推荐
- Codeforces 489A SwapSort (水题)
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Javascript中的"\r\n"
我们知道 \r 代表的是 回车符(ACSII: 13 或0x0d), 也就是"硬回车" \n 代表的是 换行符(ACSII: 10 或 0x0a), 也就是 "软回车&q ...
- UVaLive 4731 Cellular Network (期望DP)
题意:手机在蜂窝网络中的定位是一个基本问题,假设蜂窝网络已经得知手机处于c1,c2,,,cn这些区域中的一个,最简单的方法是同时在这些区域中寻找手机, 但这样做很浪费带宽,由于蜂窝网络中可以得知手机在 ...
- jmeter压力测试报告
XXX压力测试报告 时间:2015-08-04 测试人员:xxx 目录 XXX压力测试报告... 1 一 测试 ...
- spring boot 启动报:Composite-id class must implement Serializable: xxx错误
问题 在对spring boot项目进行修改后,重新启动时报如下错误: Composite-id class must implement Serializable: xxx xxx为具体的类 原因 ...
- 利用外部协议让chrome启动外部应用程序
http://bbs.kafan.cn/thread-1254526-1-1.html 原理:很简单,标题写的很明确了,不懂的google去. 步骤:举个例子,我要启动D:\Programe file ...
- 洛谷P1447 [NOI2010]能量采集(容斥)
传送门 很明显题目要求的东西可以写成$\sum_{i=1}^{n}\sum_{j=1}^m gcd(i,j)*2-1$(一点都不明显) 如果直接枚举肯定爆炸 那么我们设$f[i]$表示存在公因数$i$ ...
- Codeforces 183C(有向图上的环长度)
因为公用一个系统所以大家求gcd:衡量各点之间的拓扑位置,如果到达同一点有不同的长度则取gcd. #include <cstdio> #include <cstring> #i ...
- (转)nginx应用总结(2)--突破高并发的性能优化
原文:http://www.cnblogs.com/kevingrace/p/6094007.html 在日常的运维工作中,经常会用到nginx服务,也时常会碰到nginx因高并发导致的性能瓶颈问题. ...
- opencv作业
作业下载地址: 链接:http://pan.baidu.com/s/1qYQnbkw 密码:v7y9