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 ...
随机推荐
- 1.1-1.4 hadoop调度框架和oozie概述
一.hadoop调度框架 Linux Crontab Azkaban https://azkaban.github.io/ Oozie http://oozie.apache.org/ Zeus(阿里 ...
- PYTHON实现DFS算法
class Vertice: def __init__(self,index): self.no = index self.color = 0 # 0:white 1 gray 2 black sel ...
- WPF后台通知前台事件(ViewModelBase记录)
版本1: 定义:public class ModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler P ...
- HDU2844【背包问题(二进制优化)】
题意: n,m 给出A1,A2,A3...AN;C1,C2,C3...CN; 给出硬币的价值和个数,问在1-M中间能构造出多少个组合 思路: n种物品的价值,n种物品的个数: 一种物品能组成多种物品的 ...
- LightOJ1197【数学】
引自:WONTER 题意: 给你两个数,a,b,让你求区间[a,b]里面有多少个素数: 思路: 首先要知道,我们要想筛 [1, b] 中所有的素数,只需要用到 [1, sqrt(b)] 中的所有素数来 ...
- Integrated Metabolomics and Lipidomics Analyses Reveal Metabolic Reprogramming in Human Glioma with IDH1 Mutation (文献分享一组-黄旭蕾)
题目:Integrated Metabolomics and Lipidomics Analyses Reveal Metabolic Reprogramming in Human Glioma wi ...
- react native 获取地图需要的SHA1
1.从电脑的根目录进入.android文件 2.进入.android文件后输入 keytool -v -list -keystore debug.keystore 3.回车输入密码,(可以直接回车不用 ...
- 黑马tomact学习二 tomcat的启动
- PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)
嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...
- .NET 基础 一步步 一幕幕 [.NET基础知识点]
.NET基础知识点 l .Net平台 .Net FrameWork框架 l .Net FrameWork框架提供了一个稳定的运行环境,:来保障我们.Net平台正常的运转 l 两种交 ...