Erasing an array
Practice
3.1 (34 votes)
Implementation
Basic programming
Basics of greedy algorithms
Problem
89% Success 7499 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a binary array $$A$$ of $$N$$ elements. The array consists of 0's and 1's. You can perform the following operations as many times as possible:

  • Select a subarray starting from the first index that is inversion-free and delete it.

Determine the minimum number of operations to delete the entire array.

  • Inversion free: There are no two indices $$i$$ and $$j$$ in array $$A$$ such that $$(i<j)$$ and (\(A_i>A_j\)). 
  • Subarray: A subarray is an array obtained after deleting some elements from the beginning (prefix) possibly 0 and deleting some elements from the end (suffix) possibly 0.

Input format

  • The first line contains an integer $$T$$ denoting the number of test cases. 
  • The first line of each test case contains an integer $$N$$ denoting the number of elements in array $$A$$.
  • The second line contains $$N$$ space-separated integers of array $$A$$.

Output Format

Print $$T$$ lines and for each test case:

  • Print the minimum number of operations to delete the entire array.

Constraints

\(1 \leq T \leq 20000\)

\(1 \leq N \leq 200000\)

\(0 \leq A_i \leq 1\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
67 votes
Tags:
Basic ProgrammingBasics of ImplementationEasyImplementation
Points:20
32 votes
Tags:
EasyMaps
Points:20
19 votes
Tags:
Basic ProgrammingEasy