Codeforces——Array Sharpening
You’re given an array a1,…,an of n non-negative integers.
Let’s call it sharpened if and only if there exists an integer 1≤k≤n such that a1<a2<…ak+1>…>an. In particular, any strictly increasing or strictly decreasing array is sharpened. For example:
The arrays [4], [0,1], [12,10,8] and [3,11,15,9,7,4] are sharpened;
The arrays [2,8,2,8,6,5], [0,1,1,0] and [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 i (1≤i≤n) such that ai>0 and assign ai:=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 t (1≤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 n (1≤n≤3⋅105).
The second line of each test case contains a sequence of n non-negative integers a1,…,an (0≤ai≤109).
It is guaranteed that the sum of n over all test cases does not exceed 3⋅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] (decrease the first element 97 times and decrease the last element 4 times). It is sharpened because 3<11<15 and 15>9>7>4.
In the fourth test case of the first test, it’s impossible to make the given array sharpened.
题目大意:
有t组数,每组数有n个数字,若数字不为0,可以对其进行减1操作,可进行多次,最后判断是否存在某个位置k(1<=k<=n),使得a1<a2<…ak+1>…>an。
分析:
对于第i位上的数字,它的最小值不能同时小于i-1和n-i,如果它小于这两个数的话,那么它前面的数或者后面的数要比它小,这样一直小下去的话它的前面或者肯定至少会出现两个0,就不对了。然后,还有两个特殊的点,就是如果当n为偶数的时候,就可能会存在n/2的点和n/2+1的点对应的数字都等于n/2-1的情况,此时,两点对应的都是它们所在位置的最小值,但按题意,两者又不能相等,所以也不可以。举个例子,对于[0,1,1,0],第二个点的最小值是2-1=1,与它对应的值相等,第三个点的最小值是4-3=1,与它对应的值也相等,而第二个点等于第三个点,不符合题意,所以我们可以试着把第三个点减1,可以变成[0,1,0,0],显然,并不存在某个位置k,使得a1<a2<…ak+1>…>an。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long int ll;
const int maxn=300050;
ll f[maxn];
int main()
{
int t,flag,n;
scanf("%d",&t);
while(t--){
flag=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&f[i]);
}
for(int i=1;i<=n;i++){
if(n-i>f[i]&&i-1>f[i]){
flag=1;
}
}
if(flag==1||(n%2==0&&f[n/2]==f[n/2+1]&&f[n/2]==n/2-1)){
printf("No\n");
}
else{
printf("Yes\n");
}
}
return 0;
}
来源:CSDN
作者:linjiayina
链接:https://blog.csdn.net/linjiayina/article/details/104157533