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$ 枚铜牌 ...
随机推荐
- 如何优雅地开发HarmonyOS APP应用
目录: 一.挖掘项目需求或者做项目移植 二.创建项目工程 三.功能模块实现的流程思路 四.养成良好的编程规范习惯以及运用设计模式 研究HarmonyOS有一段时间了,今天主要结合自己多年的项目开发经验 ...
- Java微服务 vs Go微服务,究竟谁更强!?
前言 Java微服务能像Go微服务一样快吗? 这是我最近一直在思索地一个问题. 去年8月份的the Oracle Groundbreakers Tour 2020 LATAM大会上,Mark Nels ...
- 用 Flutter 搭建标签+导航框架
前言 在 Flutter 这个分类的第一篇文章总结了下最新的 Mac 搭建 Flutter 开发环境和对声明式UI这个理解的东西,前面也有提过,准备像在 SwiftUI 分类中那样花一些功夫来写一个 ...
- 跨站脚本漏洞(XSS)基础
什么是跨站脚本攻击XSS 跨站脚本(cross site script),为了避免与样式css混淆所以简称为XSS,是一种经常出现在web应用中的计算机安全漏洞,也是web中最主流的攻击方式. 什么是 ...
- 【Java】标识符
一.标识符 文章目录 一.标识符 1.标识符的命名规则 2.关键字.保留字.特殊值 3.code Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符.简单的说,凡是程序员自己命名的部分 ...
- LeetCode897. 递增顺序查找树
题目 法一.自己 1 class Solution { 2 public: 3 vector<int>res; 4 TreeNode* increasingBST(TreeNode* ro ...
- ftp交互和控制命令总结
一.FTP管理: 基于tcp,首先有客户端相服务端的知名端口21发起tcp连接建立ftp控制连接,控制连接在整个会话期间都保持打开,只用来发送连接/传送请求. 这里分为两种模式: 主动模式(PORT) ...
- 响应式编程库RxJava初探
引子 在读 Hystrix 源码时,发现一些奇特的写法.稍作搜索,知道使用了最新流行的响应式编程库RxJava.那么响应式编程究竟是怎样的呢? 本文对响应式编程及 RxJava 库作一个初步的探索. ...
- kotlin和python哪个好!程序员怎样优雅度过35岁中年危机?满满干货指导
导语 学历永远是横在我们进人大厂的一道门槛,好像无论怎么努力,总能被那些985,211 按在地上摩擦! 不仅要被"他们"看不起,在HR挑选简历,学历这块就直接被刷下去了,连证明自己 ...
- 【UltraISO】中文破解版
下载链接:https://cn.ultraiso.net/uiso9_cn.exe简体中文版专用: 注册名:Guanjiu 注册码:A06C-83A7-701D-6CFC多国语言版专用: ...