连号区间数 刷题笔记

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

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

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

假设取 a3-a5

则取出 5 4 6

重新排序后为 4 5 6

连续了 则ans++;

假设 取ai-aj这一段元素

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

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

(j-i)=max-min;

证明

暴力枚举

符合条件则ans++;

代码

#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

const int N=1e5+10;

int aN,bN;

int n;

int ans=0;

int main(){

cin>>n;

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

cin>>ai;

}

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

int minn=1e8,maxx=0;

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

minn=min(minn,aj);

maxx=max(maxx,aj);

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

{

ans++;

}

}

}

cout <<ans;

return 0;

}

相关推荐
疯狂打码的少年14 分钟前
【程序语言与编译】正规式与有限自动机的等价转换
笔记
是上好佳佳佳呀21 分钟前
【LangChain|Day03】LangChain 链式调用 Chains 笔记
笔记·langchain
洛水水31 分钟前
【力扣100题】81.寻找两个正序数组的中位数
数据结构·算法·leetcode
happymaker06261 小时前
LeetCodeHot100——155.最小栈
算法
洛水水1 小时前
【力扣100题】85.每日温度
算法·leetcode·职场和发展
Coder-magician1 小时前
《代码随想录》刷题打卡day15:二叉树part05
数据结构·c++·算法
Kurisu_红莉栖1 小时前
力扣56合并区间
算法·leetcode
Irissgwe1 小时前
算法的时间复杂度和空间复杂度
数据结构·c++·算法·c·时间复杂度·空间复杂度
随意起个昵称1 小时前
区间dp-基础题目3(永别)
c++·算法
周末也要写八哥1 小时前
有向图Hierholzer算法的另一种实现
算法