题目链接: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. HBase 底层原理详解(深度好文,建议收藏)

    HBase简介 HBase 是一个分布式的.面向列的开源数据库.建立在 HDFS 之上.Hbase的名字的来源是 Hadoop database,即 Hadoop 数据库.HBase 的计算和存储能力 ...

  2. jxl解析多个excel工作表-java代码

    @Override public ResultBean txImportDqKpi(String filePath) { ResultBean rb = new ResultBean(); int s ...

  3. C#中的异步和多线程

    许多开发人员对异步代码和多线程以及它们的工作原理和使用方法都有错误的认识.在这里,你将了解这两个概念之间的区别,并使用c#实现它们. 我:"服务员,这是我第一次来这家餐厅.通常需要4个小时才 ...

  4. springboot 和 mongdb连接问题 Exception in thread "main" com.mongodb.MongoSecurityException:

    1 Exception in thread "main" com.mongodb.MongoSecurityException: Exception authenticating ...

  5. 【Linux】如何查找命令及历史记录history

    如何查找命令及历史记录 文章目录 如何查找命令及历史记录 1.如何找到一个命令 2.命令的历史记录 3.一些实用的快捷键 4.小结 5.参考资料 如何找到一个命令.命令的历史记录.一些实用的快捷键.总 ...

  6. 【ORA】ORA-32004: 问题分析和解决

    今天做一个特殊的实验,需要重启数据库 数据库关闭没有问题 SQL> shutdown immediate; Database closed. Database dismounted. ORACL ...

  7. 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code

    问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...

  8. Linux删除文件后磁盘目录不释放

    今天测试oracle数据库的时候,把表空间连带内容和数据文件一并删除了,但是删除之后,查看数据文件不存在了,但是目录的带下没有释放 SQL> drop tablespace users incl ...

  9. D2Admin 登录用户重新初始话右侧菜单

    背景 最近用到D2Admin开发项目,用户登录菜单要根据用户角色权限获取,但是又不想用官网的方案(vue基于d2-admin的RBAC权限管理解决方案),所以自己加了个只修改 menuAside的方案 ...

  10. 【Azure Developer】已发布好的.NET Core项目文件如何打包为Docker镜像文件

    问题描述 在博文([Azure App Service For Container]创建ASP.NET Core Blazor项目并打包为Linux镜像发布到Azure应用服务)中我们通过VS 201 ...