A16028 | Keep it Beautiful
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The array $[a_1, a_2, \dots, a_k]$ is called beautiful if it is possible to remove several (maybe zero) elements from the beginning of the array and insert all these elements to the back of the array in the same order in such a way that the resulting array is sorted in non-descending order.
In other words, the array $[a_1, a_2, \dots, a_k]$ is beautiful if there exists an integer $i \in [0, k-1]$ such that the array $[a_{i+1}, a_{i+2}, \dots, a_{k-1}, a_k, a_1, a_2, \dots, a_i]$ is sorted in non-descending order.
For example:
- $[3, 7, 7, 9, 2, 3]$ is beautiful: we can remove four first elements and insert them to the back in the same order, and we get the array $[2, 3, 3, 7, 7, 9]$ , which is sorted in non-descending order;
- $[1, 2, 3, 4, 5]$ is beautiful: we can remove zero first elements and insert them to the back, and we get the array $[1, 2, 3, 4, 5]$ , which is sorted in non-descending order;
- $[5, 2, 2, 1]$ is not beautiful.
Note that any array consisting of zero elements or one element is beautiful.
You are given an array $a$ , which is initially empty. You have to process $q$ queries to it. During the $i$ -th query, you will be given one integer $x_i$ , and you have to do the following:
- if you can append the integer $x_i$ to the back of the array $a$ so that the array $a$ stays beautiful, you have to append it;
- otherwise, do nothing.
After each query, report whether you appended the given integer $x_i$ , or not.
In other words, the array $[a_1, a_2, \dots, a_k]$ is beautiful if there exists an integer $i \in [0, k-1]$ such that the array $[a_{i+1}, a_{i+2}, \dots, a_{k-1}, a_k, a_1, a_2, \dots, a_i]$ is sorted in non-descending order.
For example:
- $[3, 7, 7, 9, 2, 3]$ is beautiful: we can remove four first elements and insert them to the back in the same order, and we get the array $[2, 3, 3, 7, 7, 9]$ , which is sorted in non-descending order;
- $[1, 2, 3, 4, 5]$ is beautiful: we can remove zero first elements and insert them to the back, and we get the array $[1, 2, 3, 4, 5]$ , which is sorted in non-descending order;
- $[5, 2, 2, 1]$ is not beautiful.
Note that any array consisting of zero elements or one element is beautiful.
You are given an array $a$ , which is initially empty. You have to process $q$ queries to it. During the $i$ -th query, you will be given one integer $x_i$ , and you have to do the following:
- if you can append the integer $x_i$ to the back of the array $a$ so that the array $a$ stays beautiful, you have to append it;
- otherwise, do nothing.
After each query, report whether you appended the given integer $x_i$ , or not.
输入格式
The first line contains one integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases.
Each test case consists of two lines. The first line contains one integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ) — the number of queries. The second line contains $q$ integers $x_1, x_2, \dots, x_q$ ( $0 \le x_i \le 10^9$ ).
Additional constraint on the input: the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$ ).
Each test case consists of two lines. The first line contains one integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ) — the number of queries. The second line contains $q$ integers $x_1, x_2, \dots, x_q$ ( $0 \le x_i \le 10^9$ ).
Additional constraint on the input: the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$ ).
输出格式
For each test case, print one string consisting of exactly $q$ characters. The $i$ -th character of the string should be 1 if you appended the integer during the $i$ -th query; otherwise, it should be 0.
输入输出样例
输入 #1
3 9 3 7 7 9 2 4 6 3 4 5 1 1 1 1 1 5 3 2 1 2 3
输出 #1
111110010 11111 11011
Consider the first test case of the example. Initially, the array is $[]$ .
- trying to append an integer $3$ . The array $[3]$ is beautiful, so we append $3$ ;
- trying to append an integer $7$ . The array $[3, 7]$ is beautiful, so we append $7$ ;
- trying to append an integer $7$ . The array $[3, 7, 7]$ is beautiful, so we append $7$ ;
- trying to append an integer $9$ . The array $[3, 7, 7, 9]$ is beautiful, so we append $9$ ;
- trying to append an integer $2$ . The array $[3, 7, 7, 9, 2]$ is beautiful, so we append $2$ ;
- trying to append an integer $4$ . The array $[3, 7, 7, 9, 2, 4]$ is not beautiful, so we don't append $4$ ;
- trying to append an integer $6$ . The array $[3, 7, 7, 9, 2, 6]$ is not beautiful, so we don't append $6$ ;
- trying to append an integer $3$ . The array $[3, 7, 7, 9, 2, 3]$ is beautiful, so we append $3$ ;
- trying to append an integer $4$ . The array $[3, 7, 7, 9, 2, 3, 4]$ is not beautiful, so we don't append $4$ .
- trying to append an integer $3$ . The array $[3]$ is beautiful, so we append $3$ ;
- trying to append an integer $7$ . The array $[3, 7]$ is beautiful, so we append $7$ ;
- trying to append an integer $7$ . The array $[3, 7, 7]$ is beautiful, so we append $7$ ;
- trying to append an integer $9$ . The array $[3, 7, 7, 9]$ is beautiful, so we append $9$ ;
- trying to append an integer $2$ . The array $[3, 7, 7, 9, 2]$ is beautiful, so we append $2$ ;
- trying to append an integer $4$ . The array $[3, 7, 7, 9, 2, 4]$ is not beautiful, so we don't append $4$ ;
- trying to append an integer $6$ . The array $[3, 7, 7, 9, 2, 6]$ is not beautiful, so we don't append $6$ ;
- trying to append an integer $3$ . The array $[3, 7, 7, 9, 2, 3]$ is beautiful, so we append $3$ ;
- trying to append an integer $4$ . The array $[3, 7, 7, 9, 2, 3, 4]$ is not beautiful, so we don't append $4$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted