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 ...
随机推荐
- [CEOI 2004]锯木厂选址
Description 题库链接 从山顶上到山底下沿着一条直线种植了 \(n\) 棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂. 木材只能朝山下运.山脚下有一个 ...
- mysql innodb cluster 无感知集群
MySQL 8.0.12 innodb cluster 高可用集群部署运维管理手册 Innodb cluster 原理介绍 Innodb cluster 利用组复制的 pxos 协议,保障数据一致性 ...
- 118、Java中String类之取字符串长度
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- Lucene_solr
1.总结 https://pan.baidu.com/s/1pMAWk0z 密码:ekhx 2.代码 https://pan.baidu.com/s/1nxmTWy1 密码:65ec 3.资料 ...
- 窥探QQ基础数据库架构演变史
作为腾讯最核心最基础的后台服务之一,QQ基础数据库是存储QQ用户帐户信息和关系链信息的海量集群,它承载了百万级每秒的访问量.十亿级的账户数.百亿级关系链.如此大规模的集群,它是如何从300万的数量级一 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:表示一个警告的操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- mybatis注解基础使用
一.创建Maven项目 代码:pom.xml <?xml version="1.0" encoding="UTF-8"?> <projec ...
- python-python基础7
一.静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类 ...
- scrapy 实现mysql 数据保存
开始用scrapy 爬取数据的时候 开始用同步操作始终会报1064 的错误 因为 mysql 语法和导入的字段不兼容 尝试了 n 次之后 开始用 异步爬取 虽然一路报错 但是还是能把数 ...
- centos6忘记root密码
Centos6 1.在开机时不要自动进入系统,按任意键进入GRUB引导菜单 2.按E键进入编辑模式 3.选中kernel选项继续按E键 4.在结尾处添加single关键字后按ENTER保存退出 5.之 ...