Codeforces Round #604 (Div. 2) B. Beautiful Numbers(双指针)
题目链接: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(双指针)的更多相关文章
- 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 ...
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest
链接: https://codeforces.com/contest/1265/problem/C 题意: So the Beautiful Regional Contest (BeRC) has c ...
- 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 ...
- Codeforces Round #604 (Div. 2) A. Beautiful String
链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...
- Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学
题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...
- Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)
题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...
- Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest(贪心)
题目链接:https://codeforces.com/contest/1265/problem/C 题意 从大到小给出 $n$ 只队伍的过题数,要颁发 $g$ 枚金牌,$s$ 枚银牌,$b$ 枚铜牌 ...
随机推荐
- Java 中的 equals() 和 hashCode()
equals() 和 hashCode() 在 Object 类中以本地方法的形式存在,Java 中所有的类都继承了 Object 类,因此所有的类中都包含了这两个方法.这两个方法在 Java 开发中 ...
- 【SpringBoot1.x】 Docker
SpringBoot1.x Docker 核心概念 Docker 是一个开源的应用容器引擎,是一个轻量级容器技术.Docker 支持将软件编译成一个镜像,然后在镜像中各种软件做好配置,将镜像发布出去, ...
- Logrotate工具使用
Logrotate logrotate是一个被设计来简化系统管理日志文件的工具,在系统运行时,如果产生大量的日志文件,可以使用该工具进行管理,如/var/log/*文件夹是存储系统和应用日志的目录 ...
- Payment Spring Boot 1.0.4.RELEASE 发布,最易用的微信支付 V3 实现
Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 欢迎ISSUE,欢迎 ...
- 【Linux】查看系统僵尸进程
ps -ef|grep -v grep|grep defunct 如果这个有显示内容的话,可以手动将进程kill掉即可 ---------------------------------------- ...
- C语言字符串结束符“\0”
介绍 '\0'就是8位的00000000,因为字符类型中并没有对应的这个字符,所以这么写.'\0'就是 字符串结束标志. '\0'是转译字符,意思是告诉编译器,这不是字符0,而是空字符.空字符\0对应 ...
- SAP 技术设置(technical setting)
在创建数据库表的时候,需要设置它的技术参数:这样才能使用. 在技术设置里,有个数据类(data class),如APPL0,等等. 有好多值可以供我们选择.这些值保存在表DDART中,表的描述:DD: ...
- 前端基础功能,原生js实现轮播图实例教程
轮播图是前端最基本.最常见的功能,不论web端还是移动端,大平台还是小网站,大多在首页都会放一个轮播图效果.本教程讲解怎么实现一个简单的轮播图效果.学习本教程之前,读者需要具备html和css技能,同 ...
- Java多线程基础知识笔记(持续更新)
多线程基础知识笔记 一.线程 1.基本概念 程序(program):是为完成特定任务.用某种语言编写的一组指令的集合.即指一段静态的代码,静态对象. 进程(process):是程序的一次执行过程,或是 ...
- python(re正则)
import re #导入模块 info = 'qwewwer12332423kdsjfkl2342kdjfl213nkafal123123' 例1: res1 = re.compile('er( ...