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 and ak>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
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
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.
思路:第一个如果是在从低往高的话,最小为0,第二个为1...,如果第n个从高往低的话,最小为0,第n - 1个为1,那么我们可以预处理这两种情况,如果a[i] < i (从低往高),a[i] < (n - 1) - i(从高往低),那么说明从这里开始就不可能变成从低到高或者是从高到低了,如果满足从低到高的最大下标left小于从高到低最小的right,那么0~left能从低到高,right~n-1能从高到低,而left~right这不能保障从低到高在从高到低(因为left开始就不能保障从低到高,而right往前也不可能保障从高到低),所以这样的不可能,其他的均有可能
AC代码:
#include <stdio.h> int a[300005]; int left, right; int main(void) { int t, n; scanf("%d", &t); for(int i = 0; i < t; i++) { left = -1, right = n;//注意一开始要把范围定好,不能与之后可能出现的值重复了,并且保证这两个值某一个未发生改变,输出都是No,最好的办法就在边界外,这样如果其中一个值不发生变化,那么最后不可能Yes scanf("%d", &n); for(int j = 0; j < n; j++) { scanf("%d", &a[j]); } for(int j = 0; j < n; j++) { if(j > a[j]) break; left = j; } for(int j = n - 1; j >= 0; j--) { if(n - j - 1 > a[j]) break; right = j; } if(left >= right) printf("Yes\n"); else printf("No\n"); } return 0; }