codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2060D.md (1640B)


      1 Source: https://codeforces.com/problemset/problem/2060/D
      2 
      3 D. Subtract Min Sort
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a sequence $a$ consisting of $n$ positive integers.
     22 
     23 You can perform the following operation any number of times.
     24 
     25 * Select an index $i$ ($1 \le i \lt n$), and subtract $\min(a_i,a_{i+1})$ from both $a_i$ and $a_{i+1}$.
     26 
     27 Determine if it is possible to make the sequence non-decreasing by using the operation any number of times.
     28 
     29 Input
     30 
     31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     32 
     33 The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$).
     34 
     35 The second line of each test case contains $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$).
     36 
     37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     38 
     39 Output
     40 
     41 If it is possible to make the sequence non-decreasing, print "YES" on a new line. Otherwise, print "NO" on a new line.
     42 
     43 You can output the answer in any case. For example, the strings "yEs", "yes", and "Yes" will also be recognized as positive responses.
     44 
     45 Example
     46 
     47 Input
     48 
     49 ```
     50 5
     51 
     52 5
     53 
     54 1 2 3 4 5
     55 
     56 4
     57 
     58 4 3 2 1
     59 
     60 4
     61 
     62 4 5 2 3
     63 
     64 8
     65 
     66 4 5 4 5 4 5 4 5
     67 
     68 9
     69 
     70 9 9 8 2 4 4 3 5 3
     71 ```
     72 
     73 Output
     74 
     75 ```
     76 YES
     77 NO
     78 YES
     79 YES
     80 NO
     81 ```
     82 
     83 Note
     84 
     85 In the first test case, the array is already sorted.
     86 
     87 In the second test case, we can show that it is impossible.
     88 
     89 In the third test case, after performing an operation on $i=1$, the array becomes $[0,1,2,3]$, which is now in nondecreasing order.