51nod 1276 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
这个题目用了一些小技巧
可以看作1-n周围的已经被淹没 cnt = 2;
那么 cnt -1 就是答案
当一个岛被淹没 如果他的左右有一块被淹没的区域 那么cnt不变
当周围没被淹 那么cnt++
如果周围都被淹没 那么淹没的区域为cnt--
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<climits>
#include<vector>
using namespace std;
const int N = 5e4+;
int min_h = INT_MAX,max_h = INT_MIN;
struct data
{
int h,id; /* data */
}Q[N];
bool vis[N];
vector<int>v[N],num;
int H[N];
int ans[N];
int getid(int x)
{
x++;
return lower_bound(num.begin(),num.end(),x)-num.begin()-;
}
bool cmp(data a,data b)
{
return a.h<b.h;
}
int cnt = ;
void check(int pos)
{
vis[pos] = true;
if(vis[pos+]&&vis[pos-])
{
cnt--;
}
else{
if(vis[pos+]||vis[pos-])
{
;
}
else{
cnt++;
}
}
}
int main()
{
int n,q,t;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&t);
num.push_back(t);
H[i] = t;
max_h = max(max_h,t);
min_h = min(min_h,t);
}
sort(num.begin(),num.end());
num.erase(unique(num.begin(),num.end()),num.end() );
for(int i=;i<=n;i++)
{
//cout<<getid(H[i])<<endl;
v[getid(H[i])].push_back(i);
}
vis[] = vis[n+] = true; for(int i=;i<q;i++)
{
scanf("%d",&Q[i].h);
Q[i].id = i;
}
sort(Q,Q+q,cmp);
int pre = -;
for(int i=;i<q;i++)
{
if(Q[i].h>=max_h)
{
ans[Q[i].id] = ;
continue;
}
if(Q[i].h<min_h)
{
ans[Q[i].id] = ;
continue;
}
int now = getid(Q[i].h);
//cout<<endl<<"H: "<<Q[i].h<<" id: "<<now<<endl;
for(int j = pre+;j<=now;j++)
{
int len = v[j].size();
for(int k=;k<len;k++)
{
check(v[j][k]);
//cout<<v[j][k]<<" ";
}
}
pre = now;
ans[Q[i].id] = cnt - ;
}
for(int i=;i<q;i++)
{
printf("%d\n",ans[i]);
}
return ;
}
AC代码
51nod 1276 1276 岛屿的数量 (很好玩的题目的更多相关文章
- 51nod 1276:岛屿的数量 很好玩的题目
1276 岛屿的数量 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 有N个岛连在一起形成了一个大的岛屿,如果海平 ...
- [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 1276 岛屿的数量
题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没 ...
- 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 ...
- 51nod 1042数字0-9的数量
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19 ...
随机推荐
- jsoup解析xml某片段的问题
参考: http://blog.csdn.net/qy20115549/article/details/53556928 <tr> <td class='center'> &l ...
- ElasticSearch基础+文档CRUD操作
本篇博客是上一篇的延续,主要用来将年前学习ES的知识点做一个回顾,方便日后进行复习和汇总!因为近期项目中使用ES出现了点小问题,因此在这里做一个详细的汇总! [01]全文检索和Lucene (1)全文 ...
- 生产环境下Flask项目目录构建
接触Flask已经有大半年了,本篇博客主要来探讨如何规范化生产环境下Flask的项目目录结构.虽然目录结构见仁见智,个人有个人的看法和习惯,但总的来说,经过很多人的实践和总结,还是有很多共同的意见和想 ...
- tomcat的bin文件夹下的.bat和.sh文件
tomcat的bin文件夹中存在一份.bat文件和相对应的.sh文件,一个是为了在window系统上执行的文件,另一个是linux下的批处理文件.例如:startup.bat和startup.sh. ...
- HDU - 2036 改革春风吹满地 叉乘法求多边形面积
改革春风吹满地 “ 改革春风吹满地, 不会AC没关系; 实在不行回老家, 还有一亩三分地. 谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟 ...
- Matplotlib 在绘画bar时, 鼠标响应点击 bar 的消息
官方教程: http://urania.udea.edu.co/sitios/astronomia-2.0/pages/descargas.rs/files/descargasdt5vi/Cursos ...
- [SDOI2019] 热闹又尴尬的聚会
热闹度\(p\)子图中最小的度数,尴尬度\(q\)独立集大小,之间的约束 \[ \begin{aligned} \lfloor n/(p+1)\rfloor\le q &\rightarrow ...
- Alcatraz -- 一个神奇的管理插件的Xcode插件
Install Paste this into your terminal: curl -fsSL https://raw.githubusercontent.com/supermarin/Alcat ...
- PJzhang:互联网是有国界
猫宁!!! 参考链接:https://mp.weixin.qq.com/s/NFgps_5HBpl3ZjDoR5w8XA 上世纪90年代,互联网开始崛起,美国凭借超级大国的地位,先进技术优势,以美国公 ...
- iOS 7:漫谈#define 宏定义(转)
iOS :漫谈#define 宏定义 #define宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步 ...