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只要从最高的岛屿开始找起,判断条件:如果他旁边都是没有被找过的(也就是默认是海),那么数量+ ...
随机推荐
- 浏览器原生 form 表单POST 数据的两种方式
我们在提交表单的时候,form表单参数中会有一个enctype的参数.enctype指定了HTTP请求的Content-Type. 常用有两种:application/x-www-form-urlen ...
- c程序实现unicode字符转utf-8字符
下面是一个unicode字符转换为utf-8的c程序实现: /* * ================================================================= ...
- QDUOJ 河老师的新年礼物(尺取法)
河老师的新年礼物 发布时间: 2017年1月1日 15:11 最后更新: 2017年1月1日 15:13 时间限制: 1000ms 内存限制: 256M 描述 河老师的新年礼物是一个长度为 ...
- SPFA算法——最短路径
粗略讲讲SPFA算法的原理,SPFA算法是1994年西南交通大学段凡丁提出 是一种求单源最短路的算法 算法中需要用到的主要变量 int n; //表示n个点,从1到n标号 int s,t; //s ...
- httpd.conf------Apache主站点配置
Apache主站点配置 Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. Apache主站点基本配置:ServerRoot “/mnt/sof ...
- Keras实现CIFAR-10分类
仅仅为了学习Keras的使用,使用一个四层的全连接网络对MNIST数据集进行分类,网络模型各层结点数为:3072: : 1024 : 512:10: 使用50000张图片进行训练,10000张 ...
- PowerDesigner设计表时显示注释列Comment,Columns中没有Comment的解决办法
我使用的PowerDesigner版本为16.5,如下图: 在所要编辑的表上双击,打开Table Properties窗口,并将上面的选项卡切换到Columns,如下图: 我们点击Customize ...
- 洛谷P2289 [HNOI2004]邮递员(插头dp)
传送门 太神仙了……讲不来讲不来->这里 //minamoto #include<iostream> #include<cstdio> #include<cstri ...
- C 语言实例 - 求两数最小公倍数
C 语言实例 - 求两数最小公倍数 用户输入两个数,其这两个数的最小公倍数. 实例 - 使用 while 和 if #include <stdio.h> int main() { int ...
- java获取本机ip的方法
直接上代码: public class LocalIPUtil { public static String getLocalIp(HttpServletRequest request){ Strin ...