连号区间数 刷题笔记

1.单个元素算一个连续区间

2.题意为 单独截取数组中的一段元素

例如 a数组假设为 3 1 2 5 4 6 7

假设取 a[3]-a[5]

则取出 5 4 6

重新排序后为 4 5 6

连续了 则ans++;

假设 取a[i]-a[j]这一段元素

设该段元素的最大值为max,最小值为min

如果该段元素重新排序后 是连续的则有

(j-i)=max-min;

证明

暴力枚举

符合条件则ans++;

代码

#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

const int N=1e5+10;

int a[N],b[N];

int n;

int ans=0;

int main(){

cin>>n;

for(int i=1;i<=n;i++){

cin>>a[i];

}

for(int i=1;i<=n;i++){

int minn=1e8,maxx=0;

for(int j=i;j<=n;j++){

minn=min(minn,a[j]);

maxx=max(maxx,a[j]);

if((maxx-minn)==(j-i))

{

ans++;

}

}

}

cout <<ans;

return 0;

}

相关推荐
西岸行者6 小时前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
ZPC82106 小时前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC82106 小时前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David6 小时前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
颜酱6 小时前
栈的经典应用:从基础到进阶,解决LeetCode高频栈类问题
javascript·后端·算法
多恩Stone6 小时前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
生信大杂烩6 小时前
癌症中的“细胞邻域“:解码肿瘤微环境的空间密码 ——Nature Cancer 综述解读
人工智能·算法
starlaky6 小时前
Django入门笔记
笔记·django
勇气要爆发6 小时前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
蜡笔小马7 小时前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost