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

边栏推荐
猜你喜欢

手把手教你搭建一台永久运行的个人服务器

MySQL Soul 16 Questions, How Many Questions Can You Last?

Solve npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead

MySQL 游标

Uni-app 小程序 App 的广告变现之路:激励视频广告

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

解决centos8 MySQL密码问题ERROR 1820 (HY000) You must reset your password using ALTER USER

Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’

IDEA 连接 数据库

Markdown的使用
随机推荐
mysql去除重复数据
MySQL 5.7 detailed download, installation and configuration tutorial
Typescript 严格模式有多严格?
系统结构考点之流水线向量点积
It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
mysql remove duplicate data
【Network Security Column Directory】--Penguin Column Navigation
类似 MS Project 的项目管理工具有哪些
socket: Kernel initialization and detailed process of creating streams (files)
你需要知道的ES6—ES13开发技巧
The structure of knowledge in the corners of the C language
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
手把手教你搭建一台永久运行的个人服务器
cnpm installation steps
Be careful with your dictionaries and boilerplate code
ELF: Loading process
HCIP第十六天
LeetCode·23.合并K个升序链表·递归·迭代
Installation and use of cnpm
(7/29)基础板子最小生成树prim+kruskal