题目链接:https://codeforces.com/contest/1265/problem/B

题意

给出大小为 $n$ 的一个排列,问对于每个 $i(1 \le i \le n)$,原排列中是否有一个大小为 $i$ 的连续子排列。

题解

从小到大构造排列,记录当前排列中数的最小下标和最大下标,若最小下标和最大下标的间距刚好为排列的长度,则说明大小为 $i$ 的排列是连续的。

代码一

#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
int pos[n + 1] = {};
for (int i = 0; i < n; i++) {
int x; cin >> x;
pos[x] = i;
}
int mi = n, mx = -1;
for (int i = 1; i <= n; i++) {
mi = min(mi, pos[i]);
mx = max(mx, pos[i]);
cout << (mx - mi == i - 1);
}
cout << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

代码二

虽然时间复杂度和空间复杂度都不如代码一,但是看上去更加简洁。

#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
map<int, int> pos;
for (int i = 0; i < n; i++) {
int x; cin >> x;
pos[x] = i;
}
set<int> st;
for (int i = 1; i <= n; i++) {
st.insert(pos[i]);
cout << (*st.rbegin() - *st.begin() == i - 1);
}
cout << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

Codeforces Round #604 (Div. 2) B. Beautiful Numbers(双指针)的更多相关文章

  1. Codeforces Round #604 (Div. 2) B. Beautiful Numbers

    链接: https://codeforces.com/contest/1265/problem/B 题意: You are given a permutation p=[p1,p2,-,pn] of ...

  2. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

  3. Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

    链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...

  4. Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest

    链接: https://codeforces.com/contest/1265/problem/C 题意: So the Beautiful Regional Contest (BeRC) has c ...

  5. Codeforces Round #604 (Div. 2) E. Beautiful Mirrors

    链接: https://codeforces.com/contest/1265/problem/E 题意: Creatnx has n mirrors, numbered from 1 to n. E ...

  6. Codeforces Round #604 (Div. 2) A. Beautiful String

    链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...

  7. Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学

    题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...

  8. Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)

    题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...

  9. Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest(贪心)

    题目链接:https://codeforces.com/contest/1265/problem/C 题意 从大到小给出 $n$ 只队伍的过题数,要颁发 $g$ 枚金牌,$s$ 枚银牌,$b$ 枚铜牌 ...

随机推荐

  1. 算法设计与分析 - 主定理Master theorem (分治法递推时间复杂度)

    英文原版不上了 直接中文 定义 假设有递推关系式T(n)=aT(n/b)+f(n) 其中n为问题规模 a为递推的子问题数量 n/b为每个子问题的规模(假设每个子问题的规模基本一样) f(n)为递推以外 ...

  2. 一文彻底理解IO多路复用

    在讲解IO多路复用之前,我们需要预习一下文件以及文件描述符. 什么是文件 程序员使用I/O最终都逃不过文件. 因为这篇同属于高性能.高并发系列,讲到高性能.高并发就离不开Linux/Unix,因此这里 ...

  3. Shiro配置Session检测时Quartz版本冲突

    项目背景: shiro 1.3 + quartz 2.x 2018-9-11 22:20:35补充: 经过测试,本人发现 ,通过实现 org.apache.shiro.session.mgt.Exec ...

  4. MySQL where 条件字句查询

    where 条件字句 搜索条件可由一个或多个逻辑表达式组成 , 结果一般为布尔值 逻辑运算符 运算符 语法 描述 and && a and b a && b 逻辑与 两 ...

  5. 爬虫学习(二)requests模块的使用

    一.requests的概述 requests模块是用于发送网络请求,返回响应数据.底层实现是urllib,而且简单易用,在python2.python3中通用,能够自动帮助我们解压(gzip压缩的等) ...

  6. Lakehouse: 统一数据仓库和高级分析的新一代开放平台

    1. 摘要 数仓架构在未来一段时间内会逐渐消亡,会被一种新的Lakehouse架构取代,该架构主要有如下特性 基于开放的数据格式,如Parquet: 机器学习和数据科学将被作为头等公民支持: 提供卓越 ...

  7. Databricks 第9篇:Spark SQL 基础(数据类型、NULL语义)

    Spark SQL 支持多种数据类型,并兼容Python.Scala等语言的数据类型. 一,Spark SQL支持的数据类型 整数系列: BYTE, TINYINT:表示1B的有符号整数 SHORT, ...

  8. java虚拟机入门(二)-探索内存世界

    上节简单介绍了一下jvm的内存布局以及简单概念,那么对于虚拟机来说,它是怎么一步步的让我们能执行方法的呢: 1.首先,jvm启动时,跟个小领导一样会根据配置参数(没有配置的话jvm会有默认值)向大领导 ...

  9. 路由协议-RIP协议

    一.路由协议的发展历程和分类 距离矢量路由协议--听信"谣言",使用跳数作为度量值,最大16(0-15)跳:RIP 链路状态路由协议--"地图"路由协议:OSP ...

  10. jmeter---线程组执行顺序记录

    一.默认未勾选测试计划中独立运行每个线程组时,线程组并行执行,如下,设置三个请求,每个线程组设置5秒启动5个线程. 未勾选独立运行 运行结果如下,三个线程并行执行 勾选后,一个线程组执行完后才执行下一 ...