当前位置:网站首页>连号区间数
连号区间数
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;
}

边栏推荐
- 小心你的字典和样板代码
- Detailed explanation of the delete problem of ClickHouse delete data
- 类和对象——上
- 1064 Complete Binary Search Tree
- Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
- mysql创建表
- MYSQL JDBC图书管理系统
- 解决centos8 MySQL密码问题ERROR 1820 (HY000) You must reset your password using ALTER USER
- 折叠旧版应用程序
- Advanced c language: pointers (5)
猜你喜欢

JUC原子类详解

ClickHouse 创建数据库建表视图字典 SQL

【Nacos】解决Nacos下载速度缓慢的问题

MySQL 灵魂 16 问,你能撑到第几问?

LeetCode·Daily Question·952. Calculate Maximum Component Size by Common Factor·Union Check

Advanced c language: pointers (5)

LeetCode·每日一题·952.按公因数计算最大组件大小·并查集

Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题

鳄梨价格数据集(Avocado Prices)

MySQL Soul 16 Questions, How Many Questions Can You Last?
随机推荐
Chrome 配置samesite=none方式
MySQL Soul 16 Questions, How Many Questions Can You Last?
Navicat cannot connect to mysql super detailed processing method
(7/29) Basic board minimum spanning tree prim+kruskal
系统结构考点之多级混洗交换网络
LeetCode·23.合并K个升序链表·递归·迭代
The Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
手把手教你搭建一台永久运行的个人服务器
Navicat new database
大家都在用的plm项目管理软件有哪些
y82.第四章 Prometheus大厂监控体系及实战 -- 监控扩展和prometheus 联邦(十三)
3 minutes to take you to understand WeChat applet development
牛客小白月赛53 A-E
3分钟带你了解微信小程序开发
活动推荐 | 2022年深圳最值得参加的边缘计算活动
解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead
The most powerful and most commonly used SQL statements in history
1064 Complete Binary Search Tree
MYSQL JDBC图书管理系统