我等蒟蒻来写题解了,思路是和别人讨论得出的,如有雷同实属巧合,请大佬帮忙指点。
题目
outputstandard output
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
inputCopy
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
outputCopy
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.
题意分析:
这道题就是叫我们,确定一组数,是否可以从它中间找到一个数ak(这组数用数组a来存),使得这个数的前面全部单增,后面全部单减。n你可以对任意一个数一直减1,只要保证不为负数就行了。当然由于1<=k<=n,所以全部单增,全部单减也可以。(注意:我的代码下标从0开始,所以0<=k<n)**
如果可以就输出Yes,否则No
思路:
首先Yes的条件是,找到一个数ak,a1<a2<…<ak 并且 ak>ak+1>…>an。
我们要做的就是找到一个“最大值”,记录它的下标(这个最大值不一定是输入数据里面的最大值,而是经过处理后(减1若干次)的那个最大值),判断它后面的数是不是经过处理后(减1若干次后)满足能够单减的情况.找到“最大值”的方法是,在输入完成后,加个循环判断,如果找到一个数aj小于它的下标j,那么它前面的那一个数就是我们要找的“最大值”,直接break,这个时候“最大值”前面的数都是大于自己的下标的。如果所有的数都大于它的下标j,那么经过处理后一定能满足条件,直接输出Yes.比如 5 3 7 8 2 1(对应下标0 1 2 3 4 5),元素2小于下标4,所以它前面的数8经过处理后就是“最大值”。因为经过处理,这组数可以变成0 1 2 3 1 0.然后判断他后面的数是否能够满足单减的情况,判断条件是如果有一个元素小于n-j-1,(n为数据个数,j为下标),那么就输出No,否则为Yes.
但是注意一点,在循环遍历判断能否单减之前,一定要判断下最大值是不是等于n-f-2(f为最大值下标,经过前面的判断最大值不可能小于n-f-2),如果是直接输出No.比如,0 1 2 2 1 0,这组数,“最大值”为第一个2,后面的也满足单减,但是2=6-f-2,不满足,a1<a2<…<ak 并且 ak>ak+1>…>an,所以不行。
代码
#include<bits/stdc++.h>
using namespace std;
int t=0;
int a[1000000]={0};
int main()
{
cin>>t;
for(int i=1;i<=t;i++)
{
int max = 0;
int flag = 1;
int n=0;
int f = 0;
cin>>n;
for(int j=0;j<n;j++)
{
cin>>a[j];
}
for(int j=0;j<n;j++)//一旦找到最前面的最大值就break
{
if(a[j]<j)
{
f= j-1;//最大值下标
flag = 0;
// cout<<"f="<<f<<endl;
break;
}
}
if(flag) //如果全部都大于它的下标,那么都可以减成全部单增或者全部单减的,这样直接输出Yes,比如,5 6 7 9 10,(下标:0 1 2 3 4)
{
cout<<"Yes"<<endl;//如果全部都大于下标
continue;
}
else//只要进这里,flag就=0
{
if(a[f]==n-f-2)//这个很重要, 比如像0 1 2 2 1 0这种数据,后面的判断会输出yes但是是no
{
cout<<"No"<<endl;
continue;
}
for(int j=f+1;j<n;j++)//判断“最大值”后面的数是否有不满足的情况,一旦有个数不满足就输出
{
if(a[j]<n-j-1) //No的判断
{
flag = -1;
cout<<"No"<<endl;
break;
}
}
if(flag==0) cout<<"Yes"<<endl;//排除No的情况后就是Yes
}
}
return 0;
}**