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題都想了那么久。。
思路:這個(gè)題目的難點(diǎn)是沒(méi)有辦法處理那個(gè)拐點(diǎn),如果按照原來(lái)的數(shù)組來(lái)的話(huà),可能會(huì)出現(xiàn)很多拐點(diǎn)。但是我們仔細(xì)想一下,我們最后可以把數(shù)組處理成0 1 2 3…3 2 1 0的樣子,這應(yīng)該是處理的極致了。那么我們就遍歷數(shù)組,如果a[i]>=i的話(huà)就將它處理成i,到a[i]<i的那一點(diǎn)這就是拐點(diǎn)了,那么我們就看這一點(diǎn)以及后面的數(shù)字,不斷遍歷,并將a[i]更新為min(a[i-1]-1,a[i]),如果出現(xiàn)的負(fù)數(shù)就不行了。
但是還有一種情況,就是遞減序列,即拐點(diǎn)出現(xiàn)在第一位,根據(jù)以上做法是不可行的。我們轉(zhuǎn)換一下,變成遞增序列就可以了。
代碼如下:
努力加油a啊,(o)/~
總結(jié)
以上是生活随笔為你收集整理的Array Sharpening CodeForces - 1291B(思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Mind Control CodeFor
- 下一篇: Erasing Zeroes CodeF