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 ...
随机推荐
- git rebase 与git merge 小结
git merge是用来合并两个分支的. $ git merge b 将b分支合并到当前分支 同样 $ git rebase b ,也是把 b分支合并到当前分支 ---------------- ...
- JSON转换的实现
String转成JSON这个依赖很重要,我们将围绕fastjson中的JSONObject这个类来谈转换 <dependency> <groupId>com.alibaba&l ...
- 代码审计变成CTF
0x01 代码审计中的信息收集 一个cms代码量确实不少,通读代码耗时长,效果也不一定好.而一个功能点如果之前出过漏洞,特别是多次出现漏洞的地方,证明开发者对这个漏洞的理解不充分,很容易再次绕过补丁. ...
- php实现简单链式操作mysql数据库类
<?php $dbConfig = require_once(dirname(__FILE__).'/config.php'); class Db{ public $conn; ...
- 「BJOI2018」求和
「BJOI2018」求和 传送门 观察到 \(k\) 很小而且模数不会变,所以我们直接预处理 \(k\) 取所有值时树上前缀答案,查询的时候差分一下即可. 参考代码: #include <alg ...
- SQLite、MySQL和PostgreSQL 三种关系数据库哪个好?
关系型数据库的使用已经有相当长的时间了.它们变得流行起来托了管理系统的福,关系模型被实现得相当的好,并且被证明是操作数据的好方法(特别是事务性强的应用). 在这篇DigitalOcean文章中,我们将 ...
- iOS开发的调试技巧
关于本文: 1.模拟器的快捷键 2.覆盖安装注意事项 3.给模拟器相册增加照片 4.模拟器中程序的数据 5.安装旧版本的模拟器 6.模拟慢网速 7.异常断点与符号断点 1.模拟器的快捷键 常用的模拟器 ...
- [多校联考]SLON!!!
题目描述 $SLON$是一个调皮的学生,为了让他静下心来,老师给他出了一道数学题:给定表达式$A$,$A$中含有变量$x$和$+,-,*,(,)$这些符号,括号成对出现,一个算术运算符均对应两个操作数 ...
- nginx 安装部署前篇
官网:https://nginx.org/ 特性:既可以作为HTTP服务器,也可以作为反向代理服务器或者邮件服务器或者邮件服务器:能够快递响应静态页面的请求:支持 Fast CGI.SSL.Virtu ...
- Java的clone方法效率问题
在Java中,经常会需要新建一个对象,很多情况下,需要这个新建的对象和现有的某个对象保持属性一致. 那么,就有两种方式来实现这个对象的构造: ①通过新建一个对象,为这个对象的属性根据原有对象的属性来进 ...