CF1265B Beautiful Numbers
题意
给一个长度为\(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的更多相关文章
- CF1265B Beautiful Numbers 题解
Content 给定一个 \(1\sim n\) 的排列,请求出对于 \(1\leqslant m\leqslant n\),是否存在一个区间满足这个区间是一个 \(1\sim m\) 的排列. 数据 ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 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 Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- Codeforces Beta Round #51 D. Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Beautiful Numbers(牛客网)
链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...
随机推荐
- Java 基础 - 继承
子类继承父类的private字段么? Oracle的Java Documentation对Inheritance的定义: 很直白,定义里面就告诉你了这不叫继承.继承的意思是你可以对其进行直接的调用和修 ...
- onethink后台代码简单分析(1)
onethink后台的入口页面同样是Index/index控制器 首先,Index继承了AdminController,AdminController有一个_initialize函数,这是所有后台运行 ...
- AJAX(包括跨域)post请求封装
function ajaxPost(dataUrl, parameter, callback, bef_callback, com_callback, err_callback) { $.ajax({ ...
- (转)Ubuntu下用eclipse cdt编写多线程程序的简单设置
在Ubuntu下用eclipse cdt编写了一个多线程程序,但是总是出现pthread_create函数未定义! 查找了下原因,原来是要对eclipse进行一些简单的设置: 右键单击项目->P ...
- error LNK2019: 无法解析的外部符号 __imp__GetStockObject@4该符号在函数_WinMain@16 中被引用
编译链接报错 error LNK2019: 无法解析的外部符号 __imp__GetStockObject@4该符号在函数_WinMain@16 中被引用 解决方案: 在代码中添加链接库gdi32.l ...
- python 一些特殊用法和坑
https://github.com/leisurelicht/wtfpython-cn
- C# 中如何输出双引号(转义字符的使用)
实现效果: 输出这样的一个含有双引号的字符串 "hello" 方式一: 不用 @ 时转义 System.Console.WriteLine("\"he ...
- POJ 1329 Circle Through Three Points(三角形外接圆)
题目链接:http://poj.org/problem?id=1329 #include<cstdio> #include<cmath> #include<algorit ...
- shell 检查文件夹是否包含文件,或者只是空文件
empty_dir_check(){ check_dir=$ if [ -d $check_dir ];then file_list=` -maxdepth -type f` if [ $file_l ...
- vue-lic工具搭建vue-webpack项目
1.安装node环境(安装node时候会自动安装npm) 参考官网:https://nodejs.org/en/download/ 2.安装vue的脚手架工具vue-cli // 全局安装 npm i ...