连号区间数 刷题笔记

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;

}

相关推荐
52Hz11814 小时前
力扣230.二叉搜索树中第k小的元素、199.二叉树的右视图、114.二叉树展开为链表
python·算法·leetcode
苦藤新鸡14 小时前
56.组合总数
数据结构·算法·leetcode
LiLiYuan.15 小时前
【Cursor 中找不到LeetCode 插件解决办法】
算法·leetcode·职场和发展
Charlie_lll15 小时前
力扣解题-[3379]转换数组
数据结构·后端·算法·leetcode
captain37615 小时前
Java队列(Queue)
算法·链表
TracyCoder12315 小时前
LeetCode Hot100(23/100)——142. 环形链表 II
算法·leetcode·链表
jigsaw_zyx15 小时前
提示词工程
人工智能·算法
A尘埃15 小时前
银行个人贷款违约风险预测(逻辑回归)
算法·机器学习·逻辑回归
Volunteer Technology15 小时前
Sentinel的限流算法
java·python·算法