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 ...
随机推荐
- Educational Codeforces Round 72 (Rated for Div. 2)D(DFS,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n,m,k=1;int c[5007] ...
- JS事件委托或者事件代理原理以及实现
事件委托(事件代理)原理:简单的说就是将事件交由别人来执行,就是将子元素的事件通过冒泡的形式交由父元素来执行. 为什么要用时间委托? 在JavaScript中,添加到页面上的事件处理程序数量将直接关系 ...
- 「CF126B」Password
题目描述 给定一个字符串 \(S\),我们规定一个字符串 \(P\) 是可行解,\(P\) 需要满足: \(P\) 是 \(S\) 的前缀 \(P\) 是 \(S\) 的后缀 \(P\) 出现在 \( ...
- Spark 写 Hive table 非常慢【解决】
代码如下: dataFrame.createOrReplaceTempView("view_page_utm") val sql = s""" |in ...
- Java编程打开运行exe程序
String path = "notepad.exe"; //(C:\Program Files\Tencent\QQ\Bin\qq.exe) try { Runtime runt ...
- python面向对象之练习题2
练习题 需求: 士兵 可以 花钱买一个AK47 士兵 可以 用开开火 士兵 可以 买弹夹 士兵 可以 上子弹 士兵 可以 给 枪 添加子弹 枪 需要弹夹和有子弹的情况下,借助士兵扣动扳机 才能开火 枪 ...
- Visual Studio Code 格式化ESlint 的方法
既然要格式化ESlint,就不得不先介绍一下什么是ESlint.后面再介绍格式化的方法 1.ESlint ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具 ...
- PaperReading20200223
CanChen ggchen@mail.ustc.edu.cn AdaBatch Motivation: Current stochastic gradient descend methods u ...
- java并发初探ReentrantWriteReadLock
java并发初探ReentrantWriteReadLock ReenWriteReadLock类的优秀博客 ReentrantReadWriteLock读写锁详解 Java多线程系列--" ...
- [SUCTF 2019]CheckIn
进入靶场只有一个文件上传界面,上传php一句话木马提示非法后缀. 尝试利用php后缀的多样性绕过,发现都被过滤掉了,先把php改成.jpg的图片,提示说有问号在内容中. php后缀的多样性:php语言 ...