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 ...
随机推荐
- leetcood学习笔记-14*-最长公共前缀
笔记: python if not 判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不 ...
- c++ 兰姆达表达式
#include<iostream> using namespace std; int main() { int a = 1; int b = 2; aut ...
- go结构体上的函数
go结构体上的函数 我们可以将一个方法和一个结构体关联: type Saiyan struct { Name string Power int } func (s *Saiyan) Super() { ...
- API函数ShellExecute与ShellExecuteEx用法
ShellExecute: 1.函数功能:你可以给它任何文件的名字,它都能识别出来并打开它.2.函数原型: HINSTANCE ShellExecute( HWND hwnd, LPCTSTR lpO ...
- NX二次开发-自定义添加右键菜单RegisterConfigureContextMenuCallback
首先声明这个知识我以前不知道,是夏天的时候看到别人在唐工的QQ群里问的,唐工说西门子官方有这个例子.那个时候我因为在忙其他事情,也就没去研究那个右键菜单到底是怎么做的.关于自定义添加右键菜单Regis ...
- HTTPS 加密原理探究
由于之前项目中IOS系统建议将http协议换成https协议所以查看相关资料在此记录 HTTPS 通讯过程的基本原理 问:Https是什么? 答: HTTP 协议定义了一套规范,让客户端或浏览器可以和 ...
- 使用wordpress搭建的网站如何去掉域名中的wordpess
我们搭建好的网站当以文件夹的形式把wordpress程序放在空间的根目录时,访问的时候要加上文件夹名,访问地址就是:http://www.xxx.com/wordpress,直接用域名是无法访问,解决 ...
- ocaml学习
ocaml与haskell一样,是functional programming的代表. 对于有一定编程经验的人来说,入手一种新语言,最有效的方式就是开发一些实用的utility,因此top-level ...
- CodeForces-1249D2-Too Many Segments (hard version) -STL+贪心
The only difference between easy and hard versions is constraints. You are given nn segments on the ...
- layui中下拉框问题
<div class="layui-form-item"> <label class="layui-form-label">上级栏目:& ...