题目链接: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. 初识 D3.js :打造专属可视化

    一.前言 随着现在自定义可视化的需求日益增长,Highcharts.echarts等高度封装的可视化框架已经无法满足用户各种强定制性的可视化需求了,这个时候D3的无限定制的能力就脱颖而出. 如果想要通 ...

  2. kubernetes环境部署单节点redis

    kubernetes部署redis数据库(单节点) redis简介 Redis 是我们常用的非关系型数据库,在项目开发.测试.部署到生成环境时,经常需要部署一套 Redis 来对数据进行缓存.这里介绍 ...

  3. linux之curl工具

    curl是一个利用URL语法在命令行下工作的文件传输工具,作用是发出网络请求,然后获取数据:它支持文件的上传和下载:支持多种通信协议. 一.查看网页源码 直接在 curl 命令后加上网址,默认会发送 ...

  4. 技术实践丨React Native 项目 Web 端同构

    摘要:尽管 React Native 已经进入开源的第 6 个年头,距离发布 1.0 版本依旧是遥遥无期."Learn once, write anywhere",完全不影响 Re ...

  5. kubernets之configMap和secret

    一  如何有效且更好的将配置写到pod的容器中 考虑一个问题,就是在传统的应用中,程序里面需要的配置一般以配置文件的形式或者shell脚本里面的参数是在执行的时候在命令行里面进行添加,但是在kuber ...

  6. 代码审计 - BugkuCTF

    extract变量覆盖: 相关函数: extract()函数:从数组中将变量导入到当前的符号表.把数组键名作为变量名,数组的键值作为变量值.但是当变量中有同名的元素时会默认覆盖掉之前的变量值. tri ...

  7. SDUST数据结构 - chap4 串

    函数题: 6-1 查找子串: 裁判测试程序样例: #include <stdio.h> #define MAXS 30 char *search(char *s, char *t); vo ...

  8. SAP中用户口令状态的一点说明

    数据元素:XUPWDSTATE 数值      内涵 -2(通常)不能更改口令.-1(每天只允许一次)今天不能更改口令.0可以更改口令,但没有必要更改.1口令为初始值必须更改口令.2口令过期必须更改口 ...

  9. SAP 技术设置(technical setting)

    在创建数据库表的时候,需要设置它的技术参数:这样才能使用. 在技术设置里,有个数据类(data class),如APPL0,等等. 有好多值可以供我们选择.这些值保存在表DDART中,表的描述:DD: ...

  10. Docker 如何动态修改容器端口映射

    Docker端口映射往往是Docker Run命令时通过-p将容器内部端口映射到宿主机的指定端口上,一般来说容器的端口所对应的端口是提前确定需要映射的.但是有些情况下不得不需要临时映射端口,例如Doc ...