CF 1891A 学习笔记

原题

A. Sorting with Twos

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array of integers 1,2,…,�1,�2,…,��. In one operation, you do the following:

  • Choose a non-negative integer �, such that 2≤2�≤�.
  • Subtract 11 from �� for all integers �, such that 1≤≤21≤�≤2�.

Can you sort the array in non-decreasing order by performing some number (possibly zero) of operations?

An array is considered non-decreasing if ≤+1��≤��+1 for all integers � such that 1≤≤−11≤�≤�−1.

Input

The first line contains a single integer � (1≤≤1041≤�≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer � (1≤≤201≤�≤20) — the length of array �.

The second line of each test case contains � integers 1,2,…,�1,�2,…,�� — the integers in array � (0≤≤10000≤��≤1000).

Output

For each test case, output "YES" if the array can be sorted, and "NO" otherwise.

Example

input

Copy

 
  

8

5

1 2 3 4 5

5

6 5 3 4 4

9

6 5 5 7 5 6 6 8 7

4

4 3 2 1

6

2 2 4 5 3 2

8

1 3 17 19 27 57 179 13

5

3 17 57 179 92

10

1 2 3 4 0 6 7 8 9 10

output

Copy

YES
YES
YES
NO
NO
NO
YES
YES

Note

In the first test case, the array is already sorted in non-decreasing order, so we don't have to perform any operations.

In the second test case, we can choose =1�=1 twice to get the array [4,3,3,4,4][4,3,3,4,4]. Then, we can choose =0�=0 once and get the sorted in non-decreasing order array [3,3,3,4,4][3,3,3,4,4].

In the third test case, we can choose =0�=0 once and get the array [5,5,5,7,5,6,6,8,7][5,5,5,7,5,6,6,8,7]. Then, we can choose =2�=2 twice and get the array [3,3,3,5,5,6,6,8,7][3,3,3,5,5,6,6,8,7]. After that, we can choose =3�=3 once and get the sorted in non-decreasing order array [2,2,2,4,4,5,5,7,7][2,2,2,4,4,5,5,7,7].

For the fourth and fifth test case, it can be shown that the array could not be sorted using these operations.

原题链接

传送门 

代码

#include
using namespace std;

int a[30];

int main()
{
	int t;
	scanf("%d",&t);
	
	while(t--)
	{
		int n;
		scanf("%d",&n);
		
		bool flag=true;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		
		for(int i=1;i<=n;i++)
		{
			if(a[i-1]>a[i]&&i!=2&&i!=3&&i!=5&&i!=9&&i!=17)
			{
				flag=false;
				break;
			}
		}
		
		if(flag==true)	puts("Yes");
		else	puts("No");
		
		memset(a,0,sizeof a);
	}
	
	return 0;
}

总结

1.题目的意思是说,输入一个数列,询问我们能否经过一定的操作把该数列修改成一个非严格的升序数列

2.操作的具体步骤是,首先选择一个非负的整数m,使得2^m<=n,n表示的是数列的长度,然后对数列里面1~2^m个元素,每一个元素进行减一的操作

3.如果可以得到非严格的上升序列,就输出yes,否则输出no

4.n的最大值是20,满足条件的2的整数次幂是,1,2,4,8,16,可以发现如果超过可以修改的范围的降序部分,就一定不能经过修改达到要求,此时输出no

5.事实上,经过观察可以发现,只要不是2的整数次幂数字作为下标对应的元素大于下一个元素,就无法实现修改,比如说给定一个数列,1 2 3 2 5,第三个数字3对应的下标是3(假设下标从1开始使用),下标3不是2的整数次幂,元素3大于下一个元素2,我们不能割裂的对元素3进行修改,假设我们要修改元素3,就一定会同时修改元素2,元素3和元素2之间的相对大小关系没有发生改变(这里说的元素2指的是下标为4的元素2) 

6.根据上面的描述,使用循环进行条件判断即可,注意a数组是全局变量,没有使用的空间的数值都是0,a数组每使用一次都需要初始化一次,防止上一次输入的数据对当前产生影响

 

你可能感兴趣的:(Codeforces,学习,笔记)