题意

给一个长度为\(n\)的排列\(P\),求对于\(1\) 到 \(n\)中的每个数\(m\),是否能找到一段长度为\(m\)的区间使得区间内的数是一个\(1\)到\(m\)的排列。

输出一个\(01\)串,其中第\(i\)位表示是否能找到一段长度为\(i\)的区间使得区间内的数是一个\(1 - i\)的排列

\(n \leq 2e5\)

分析

对于某个数,如果能找到一段区间使它合法,那么这个区间一定是唯一且连续的

考虑从小到大对于每个数,查找它的位置,并维护当前所找到的位置的最小值和最大值,即维护一下当前区间的左端点和右端点

当这个区间连续时,即\(Max - Min + 1 = m\)时,当前\(m\)是合法的

于是现在变成了在一个无序的排列上查找一个数的位置,主席树+二分即可

代码

#include<bits/stdc++.h>
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
return cnt * f;
}
const int N = 200000 + 10;
int T, n, a[N], L, R, p;
int cur, rt[N], ls[N * 20], rs[N * 20], val[N * 20];
void modify(int &x, int l, int r, int lst, int pos) {
x = ++cur;
ls[x] = ls[lst], rs[x] = rs[lst], val[x] = val[lst] + 1;
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) modify(ls[x], l, mid, ls[lst], pos);
else modify(rs[x], mid + 1, r, rs[lst], pos);
}
int query(int x, int l, int r, int pos) {
if (!x) return 0; if (l == r) return val[x];
int mid = (l + r) >> 1;
if (pos <= mid) return query(ls[x], l, mid, pos);
else return query(rs[x], mid + 1, r, pos);
}
void clear() {
for (register int i = 1; i <= cur; ++i) ls[i] = rs[i] = val[i] = 0;
memset(rt, 0, sizeof rt);
cur = 0; L = n + 1, R = 0;
}
int binary(int d) {
int l = 1, r = n;
int mid = (l + r) >> 1;
while (l < r) {
if (!query(rt[mid], 1, n, d)) l = mid + 1;
else r = mid;
mid = (l + r) >> 1;
} return l;
}
int main() {
// freopen("1.in", "r", stdin);
T = read();
while (T--) {
n = read();
clear();
for (register int i = 1; i <= n; ++i) a[i] = read(), modify(rt[i], 1, n, rt[i - 1], a[i]);
for (register int i = 1; i <= n; ++i) {
p = binary(i);
if (p < L) L = p;
if (p > R) R = p;
if (R - L + 1 == i) printf("1"); else printf("0");
} puts("");
}
return 0;
}

CF1265B Beautiful Numbers的更多相关文章

  1. CF1265B Beautiful Numbers 题解

    Content 给定一个 \(1\sim n\) 的排列,请求出对于 \(1\leqslant m\leqslant n\),是否存在一个区间满足这个区间是一个 \(1\sim m\) 的排列. 数据 ...

  2. CodeForces 55D Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  3. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  4. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

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

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

  6. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  7. CF 55D - Beautiful numbers(数位DP)

    题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...

  8. Codeforces Beta Round #51 D. Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  9. Beautiful Numbers(牛客网)

    链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...

随机推荐

  1. this关键字的使用!

    class Student{ String name; int age; Student(String name,int age){ this.name=name; this.age=age; } S ...

  2. 30天轻松学习javaweb_通过javac编译java文件

    通过javac编译java文件1.先导入需要引用的包D:\Program Files (x86)\apache-tomcat-7.0.53\webapps\test\WEB-INF\classes&g ...

  3. 关于C语言命令行参数问题

    1 int main(int argc,char** argv) 参数: argc:命令行参数的个数 argv:保存命令行参数:argv[0]保存本程序自己的名称 现在自己只知道这些以后再有学习继续补 ...

  4. go gin

    1.安装 go get -u github.com/gin-gonic/gin 2. package main import "github.com/gin-gonic/gin" ...

  5. 记录解决java.io.IOException: Server returned HTTP response code: 500 for URL:xxxxxxxx

    踩坑经历 因为项目需要去对接别的接口,使用URLConnection POST请求https接口,发送json数组时遇到java.io.IOException: Server returned HTT ...

  6. jdk tomcat的项目版本一致操作

    操作jdk版本以及tomcat版本:右键项目--buildpath--configure buildpath...---project Facets---libraries---add/选中remov ...

  7. 比较器CompareTo的使用

    比较器CompareTo的使用 源码 package test; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  8. git分布式版本控制系统权威指南学习笔记(五):git checkout

    文章目录 分离头指针 通过cat可以查看当前的分支 通过branch查看当前分支 checkout commitId(真正的

  9. spring 中 isolation 和 propagation 详解

    可以在XML文件中进行配置,下面的代码是个示意代码 <tx:advice id="txAdvice" transaction-manager="txManager& ...

  10. MySQL 建库建表规范

    1.约束 1.PrimaryKey(PK)主键 特点:唯一 + 非空,一张表中只能有一个主键约束,一般是一个数字列,最好是无意义的. 2.NOT NULL 非空 特点:不能为空,建议在MySQL中,业 ...