连号区间数 刷题笔记

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;

}

相关推荐
科研小白_1 天前
基于遗传算法优化BP神经网络(GA-BP)的数据时序预测
人工智能·算法·回归
Rousson1 天前
硬件学习笔记--82 连接器的选用原则与流程
笔记·单片机·学习
Terry Cao 漕河泾1 天前
基于dtw算法的动作、动态识别
算法
Larry_Yanan1 天前
QML学习笔记(四十)QML的ApplicationWindow和StackView
c++·笔记·qt·学习·ui
Miraitowa_cheems1 天前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号1 天前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode
Swift社区1 天前
LeetCode 400 - 第 N 位数字
算法·leetcode·职场和发展
fengfuyao9851 天前
BCH码编译码仿真与误码率性能分析
算法
小白不想白a1 天前
每日手撕算法--哈希映射/链表存储数求和
数据结构·算法
剪一朵云爱着1 天前
力扣2080. 区间内查询数字的频率
算法·leetcode