当前位置:网站首页>连号区间数
连号区间数
2022-07-30 21:46:00 【Ding Jiaxiong】
题目
小明这些天一直在思考这样一个奇怪而有趣的问题:
在 1∼N 的某个排列中有多少个连号区间呢?
这里所说的连号区间的定义是:
如果区间 [L,R] 里的所有元素(即此排列的第 L 个到第 R 个元素)递增排序后能得到一个长度为 R−L+1 的“连续”数列,则称这个区间连号区间。
当 N 很小的时候,小明可以很快地算出答案,但是当 N 变大的时候,问题就不是那么简单了,现在小明需要你的帮助。
输入格式
第一行是一个正整数 N,表示排列的规模。
第二行是 N 个不同的数字 Pi,表示这 N 个数字的某一排列。
输出格式
输出一个整数,表示不同连号区间的数目。
数据范围
1≤N≤10000,
1≤Pi≤N
输入样例1:
4
3 2 4 1
输出样例1:
7
输入样例2:
5
3 4 2 5 1
输出样例2:
9
样例解释
第一个用例中,有 7 个连号区间分别是:[1,1],[1,2],[1,3],[1,4],[2,2],[3,3],[4,4]
第二个用例中,有 9 个连号区间分别是:[1,1],[1,2],[1,3],[1,4],[1,5],[2,2],[3,3],[4,4],[5,5]
思路分析
题解
#include<iostream>
#include<cstring>
using namespace std;
const int N = 10010,INF = 100000000;
int n ;
int a[N];
int main(){
cin >> n;
//读入数据
for(int i = 0 ; i < n; i++){
cin >> a[i];
}
int res = 0;
for(int i = 0; i < n; i++){
//区间左端点
int minv = INF , maxv = -INF;
//枚举右端点
for(int j = i;j < n; j ++){
minv = min(minv , a[j]);
maxv = max(maxv , a[j]);
if(maxv - minv == j - i){
res ++;
}
}
}
cout << res << endl;
return 0;
}
边栏推荐
- 类似 MS Project 的项目管理工具有哪些
- proxy反向代理
- go慢速入门——函数
- MySQL压缩包方式安装,傻瓜式教学
- Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
- navicat无法连接mysql超详细处理方法
- Union, the difference between union and structure, the knowledge of enumeration of C language corners
- 【Nacos】解决Nacos下载速度缓慢的问题
- Markdown的使用
- 史上最全的Redis基础+进阶项目实战总结笔记
猜你喜欢
随机推荐
史上超强最常用SQL语句大全
Union, the difference between union and structure, the knowledge of enumeration of C language corners
基于ABP实现DDD--领域服务、应用服务和DTO实践
The structure of knowledge in the corners of the C language
openim支持十万超级大群
Niu Ke Xiaobaiyue Race 53 A-E
LeetCode·每日一题·952.按公因数计算最大组件大小·并查集
MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
关于SFML Rect.inl文件报错的问题
【网络安全专栏目录】--企鹅专栏导航
ArrayList扩容机制分析
Detailed explanation of the delete problem of ClickHouse delete data
Day 16 of HCIP
The most complete Redis basic + advanced project combat summary notes in history
系统结构考点之PM2I单级网络
The reason for not using bs4 is that the name is too long?Crawl lottery lottery information
Solve npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead
MYSQL JDBC Book Management System
类和对象——上
Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos