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 <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n, q;
int question[50005];
int embz_island[50005];//表示各个位置的岛的淹没情况。embz_island[i]=1,表示位置为i的岛已经被淹没了。 struct qu
{
int va;
int pos;
}query[50005]; struct is
{
int height;
int pos;
}island[50005]; bool cmp1(is no1, is no2)
{
return no1.height < no2.height;
} bool cmp2(qu no1, qu no2)
{
return no1.va < no2.va;
} void work()
{
int i, j, ans, pos;
memset(embz_island, 0, sizeof(embz_island)); j = 0;//从高度为0的岛开始搜索
ans = 1;//一开始有1个岛站立
for (i = 0; i < q; i++)
{
for (; j < n; j++)
{
if (query[i].va >= island[j].height)
{
pos = island[j].pos;
embz_island[pos] = 1; if (pos == 1)//第一个岛屿与第n个岛屿在边上,需要特殊判断
{
if (embz_island[pos + 1] == 1)
ans--;
continue;
}
if (pos == n)
{
if (embz_island[pos - 1] == 1)
ans--;
continue;
}
if (embz_island[pos - 1] == 0 && embz_island[pos + 1] == 0)//左右两边都没有被淹,ans加一
{
ans++;
}
else if (embz_island[pos - 1] == 1 && embz_island[pos + 1] == 1)//左右两边都被淹了,ans减一
{
ans--;
}
}
else
{
break;
}
}
pos = query[i].pos;
question[pos] = ans;
}
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++)
{
scanf("%d", &island[i].height);
island[i].pos = i + 1;
}
sort(island, island + n, cmp1); for (i = 0; i < q; i++)
{
scanf("%d", &query[i].va);
query[i].pos = i;
}
sort(query, query + q, cmp2); work(); for (i = 0; i < q; i++)
{
printf("%d\n", question[i]);
} //system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
51nod 1276:岛屿的数量 很好玩的题目的更多相关文章
- 51nod 1276 岛屿的数量
题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没 ...
- 51nod 1276 1276 岛屿的数量 (很好玩的题目
题意: 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没.原本的大岛屿则会分为多个小岛屿,如果海平面一直上升,则所有岛都会被淹没在水下. 给出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 ...
- 51nod 1378:夹克老爷的愤怒 很好玩的一道树状dp
1378 夹克老爷的愤怒 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 取消关注 夹克老爷逢三抽一之后,由于采用了新师爷的策略,乡民们叫苦不堪,开始组织 ...
- [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 ...
随机推荐
- treap(堆树)
# 2018-09-27 17:35:58 我实现的这个treap不能算是堆.有问题 最近对堆这种结构有点感兴趣,然后想用指针的方式实现一个堆而不是利用数组这种结构,于是自己想到了一个用二叉树结构实现 ...
- vue中 el [$el] 的理解
<template> <div class="a"> <div class="basic" ref="ba"& ...
- 单表千亿电信大数据场景,使用Spark+CarbonData替换Impala案例
[背景介绍] 国内某移动局点使用Impala组件处理电信业务详单,每天处理约100TB左右详单,详单表记录每天大于百亿级别,在使用impala过程中存在以下问题: 详单采用Parquet格式存储,数据 ...
- LibreOJ #6008. 「网络流 24 题」餐巾计划
这道题其实我在刚学 OI 的时候就在一本通上看见过,还记得上面写着"新餐巾一次性买完"之类的话.当时还很稚嫩(现在也是),想了好久,根本想不出来. 学了网络流之后发现这道题的图也是 ...
- postman提交文件
说明 1.Headers中添加 Content-Type multipart/form-data 2.Body 中选择form-data 并添加 需要传的参数名和值 最后新的一行选择file ...
- 八 SpringMVC文件上传,必须设置表单提交为post
1 修改Tomcat配置,本地目录映射 那么在server.xml中体现为: 测试一下是否设置成功: 2 引入jia包 3 配置多媒体解析器 3 jsp开启图片上传 4 Controller层设置 ...
- SqlCommand的ExecuteReader方法----转载
SqlCommand的ExecuteReader方法 原创 小道 2018-08-28 17:32:01 阅读 1353 次 评论 0 条 摘要: 用于执行查询语句,并返回一个DataReader ...
- 多个span标签在同一行显示
属性设置为display:inline或display:inline-block
- vue.js使用更简单的方法制作带删除功能的tooolist
今天复习了下vue.js,先做了个基本版的todolist,做完后自己想加个删除本项的按钮.一开始做没啥头绪了,试了试无果,查了一把后发现网上的那些方法真的是麻烦,自己动手丰衣足食,自己最后换了思路试 ...
- Android加载手机磁盘上的资源---decodeFile方法的使用
一般在写Android程序时,通常会将图片资源放在/res/drawable/文件夹下,读取时,通过R.drawable.imageId即可读取图片内容,但用户在使用时,一般会想要读取存放在存储卡上的 ...