Array Sharpening CodeForces - 1291B(思维)

You’re given an array a1,…,ana1,…,an of nn non-negative integers.

Let’s call it sharpened if and only if there exists an integer 1≤k≤n1≤k≤n such that a1<a2<…<aka1<a2<…ak+1>…>anak>ak+1>…>an. In particular, any strictly increasing or strictly decreasing array is sharpened. For example:

The arrays [4][4], [0,1][0,1], [12,10,8][12,10,8] and [3,11,15,9,7,4][3,11,15,9,7,4] are sharpened;
The arrays [2,8,2,8,6,5][2,8,2,8,6,5], [0,1,1,0][0,1,1,0] and [2,5,6,9,8,8][2,5,6,9,8,8] are not sharpened.
You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any ii (1≤i≤n1≤i≤n) such that ai>0ai>0 and assign ai:=ai−1ai:=ai−1.

Tell if it’s possible to make the given array sharpened using some number (possibly zero) of these operations.

Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤15 0001≤t≤15 000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105).

The second line of each test case contains a sequence of nn non-negative integers a1,…,ana1,…,an (0≤ai≤1090≤ai≤109).

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105.

Output
For each test case, output a single line containing “Yes” (without quotes) if it’s possible to make the given array sharpened using the described operations, or “No” (without quotes) otherwise.

Example
Input
10
1
248618
3
12 10 8
6
100 11 15 9 7 8
4
0 1 1 0
2
0 0
2
0 1
2
1 0
2
1 1
3
0 1 0
3
1 0 1
Output
Yes
Yes
Yes
No
No
Yes
Yes
Yes
Yes
No
Note
In the first and the second test case of the first test, the given array is already sharpened.

In the third test case of the first test, we can transform the array into [3,11,15,9,7,4][3,11,15,9,7,4] (decrease the first element 9797 times and decrease the last element 44 times). It is sharpened because 3<11<153<11<15 and 15>9>7>415>9>7>4.

In the fourth test case of the first test, it’s impossible to make the given array sharpened.
太菜了,B题都想了那么久。。
思路:这个题目的难点是没有办法处理那个拐点,如果按照原来的数组来的话,可能会出现很多拐点。但是我们仔细想一下,我们最后可以把数组处理成0 1 2 3…3 2 1 0的样子,这应该是处理的极致了。那么我们就遍历数组,如果a[i]>=i的话就将它处理成i,到a[i]<i的那一点这就是拐点了,那么我们就看这一点以及后面的数字,不断遍历,并将a[i]更新为min(a[i-1]-1,a[i]),如果出现的负数就不行了。
但是还有一种情况,就是递减序列,即拐点出现在第一位,根据以上做法是不可行的。我们转换一下,变成递增序列就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=3e5+100;
int a[maxx];
int b[maxx];
int n;

inline int fcs(int c[])
{
	int pos=-1;
	for(int i=0;i<n;i++)
	{
		if(c[i]>=i) c[i]=i;
		else
		{
			pos=i;
			break;
		}
	}
	if(pos==-1) return true;
	for(int i=pos;i<n;i++)
	{
		c[i]=min(c[i-1]-1,c[i]);
		if(c[i]<0) return false;
	}
	return true;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=0;i<n;i++) b[i]=a[n-i-1];
		if(fcs(a)||fcs(b)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}
//0 1 4 3 5 4 3 2 1 0

努力加油a啊,(o)/~

发布了426 篇原创文章 · 获赞 24 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104293861