C. Prefix Min and Suffix Max

time limit per test

2 seconds

memory limit per test

256 megabytes

You are given an array a of distinct integers.

In one operation, you may either:

  • choose a nonempty prefix∗ of a and replace it with its minimum value, or
  • choose a nonempty suffix† of a and replace it with its maximum value.

Note that you may choose the entire array a.

For each element ai, determine if there exists some sequence of operations to transform a into ai; that is, make the array a consist of only one element, which is ai. Output your answer as a binary string of length n, where the i-th character is 1 if there exists a sequence to transform a into ai, and 0 otherwise.

∗A prefix of an array is a subarray consisting of the first k elements of the array, for some integer k.

†A suffix of an array is a subarray consisting of the last k elements of the array, for some integer k.

Input

The first line contains an integer t (1≤t≤104) --- the number of test cases.

The first line of each test case contains one integer n (2≤n≤2⋅105) --- the size of the array a.

The second line of each test case contains n integers, a1,a2,...,an (1≤ai≤106). It is guaranteed that all ai are distinct.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case, output a binary string of length n --- the i-th character should be 1 if there exists a sequence of operations as described above, and 0 otherwise.

Example

Input

Copy

复制代码

3

6

1 3 5 4 7 2

4

13 10 12 20

7

1 2 3 4 5 6 7

Output

Copy

复制代码
100011
1101
1000001

Note

In the first sample, you can first choose the prefix of size 3. Then the array is transformed into

|---|---|---|---|
| 1 | 4 | 7 | 2 |

Next, you can choose the suffix of size 2. Then the array is transformed into

|---|---|---|
| 1 | 4 | 7 |

Finally, you can choose the prefix of size 3. Then the array is transformed into

|---|
| 1 |

So we see that it is possible to transform a into 1.

It can be shown that it is impossible to transform a into 3.

解题说明:此题是一道字符串题,给定一个字符串,需要找到一个子字符串,使得该子字符串的前缀中的最小字符尽可能大,而后缀中的最大字符尽可能小。可以通过遍历找出前缀的最小值,以及后缀的最大值,选择使得前缀最小值尽可能大且后缀最大值尽可能小的子字符串。

cpp 复制代码
#include <stdio.h>

int arr[200006];
int ans[200006];

int main()
{
	int t;
	scanf("%d ", &t);
	while (t--)
	{
		int n;
		scanf("%d ", &n);
		for (int i = 0; i < n; i++) 
		{
			scanf("%d ", &arr[i]);
			ans[i] = 0;
		}
		ans[0] = 1;
		ans[n - 1] = 1;
		int min = arr[0];
		for (int i = 1; i < n - 1; i++) 
		{
			if (arr[i] < min)
			{
				ans[i] = 1;
				min = arr[i];

			}
		}
		int max = arr[n - 1];
		for (int i = n - 2; i > 0; i--)
		{
			if (arr[i] > max)
			{
				ans[i] = 1;
				max = arr[i];

			}
		}
		for (int i = 0; i < n; i++) 
		{
			printf("%d", ans[i]);
		}
		printf("\n");
	}
	return 0;
}
相关推荐
find1star7 小时前
LeetCode 141:环形链表
java·算法·leetcode·链表
迅翼SwiftWing7 小时前
编队系列(总)|固定翼编队如何实现稳定飞行?通信架构、协同算法与工程约束深度解析
算法·架构
LB21127 小时前
力扣160 21 86
算法·leetcode·职场和发展
小智老师PMP9 小时前
2026深度解析|PMP第八版与NPDP核心侧重点本质区别(管理类证书怎么选)
开发语言·分布式·算法·职场和发展·产品经理
HugoStudio_SWAN9 小时前
洛谷 B4450 / B3867 / B3923 智慧购物、储蓄与做题——从程序到生活
c++·学习·程序人生·算法·生活
a187927218319 小时前
【算法】动态规划第二篇:双序列 DP 三课——继承、计步与断链
算法·leetcode·动态规划·dp·回溯·暴力·算法讲解
江畔柳前堤10 小时前
前台·中台·后台:2026年AI原生时代的架构全景图
开发语言·人工智能·算法·机器学习·架构·scala·ai-native
文心快码BaiduComate10 小时前
2026 OPC x AI Coding 趋势案例交流会圆满落幕:一个人就一支研发团队
算法
ACM-moran10 小时前
条件显化(二分单峰函数+构造数列)
算法·推公式·整数三分
闻缺陷则喜何志丹11 小时前
【栈 运算系统】P3719 [AHOI2017初中组] rexp|普及+
c++·算法··洛谷·运算系统