题意

给一个长度为\(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. 【leetcode】939. Minimum Area Rectangle

    题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from t ...

  2. 【leetcode】951. Flip Equivalent Binary Trees

    题目如下: For a binary tree T, we can define a flip operation as follows: choose any node, and swap the ...

  3. leetcood学习笔记-7

    Python join()方法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的元素序列. 返回值 返回通过指定字符连接序列中元素后生成的新字符串. ...

  4. CentOS下MYSQL数据库的安装

    关于在Centos系统下安装MYSQL数据库,网络上资料有很多,在此主要感谢该文章的博主:http://www.cnblogs.com/zhoulf/archive/2013/01/25/zhoulf ...

  5. docker的备份和迁移

    备份与迁移 容器保存为镜像 我们可以通过以下命令将容器保存为镜像 docker commit pyg_nginx mynginx pyg_nginx是容器名称 mynginx是新的镜像名称 此镜像的内 ...

  6. NX二次开发-Ufun API Example

    UF公共类型 UF_begin_timer计时函数 https://www.cnblogs.com/nxopen2018/p/10957135.html UF_end_timer计时函数 https: ...

  7. jQuery Validate (摘自官网)

    jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证 ...

  8. Arduino与NodeMCU——联网

    我们现在要使用Arduino IDE来配置您的ESP8266芯片.这是使用该芯片的好方法,因为您可以使用着名的Arduino IDE对其进行编程,并重复使用几个现有的Arduino库.如果尚未完成,请 ...

  9. Linux网卡驱动(4)—DM9000网卡驱动程序完全分析

    1.硬件连接 mini2440开发板上DM9000的电气连接和mach-mini2440.c文件的关系 其中片选信号AEN使用了nGCS4,所以网卡的内存区域在BANK4,也就是从地址0x200000 ...

  10. WIN7下怎么安装iis教程

    点击开始→控制面板,然后再点击程序,勿点击卸载程序,否则到不了目标系统界面. 2 然后在程序和功能下面,点击打开和关闭windows功能. 3 进入Windows功能窗口,然后看到internet信息 ...