All submissions for this problem are available.
Sheldon is addicted to Facebook , and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations of where each thread consists of multiple messages.
Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Sheldon has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Sheldon find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
thread x is not updated (it has no new messages);
the list order 1, 2, ..., n changes to a1, a2, ..., an.
The first line of input contains an integer T, no. of test cases
Each test case contains n, the number of threads. The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output a single integer — the number of threads that surely contain a new message.
- 1 ≤ n ≤ 105
- 1 ≤ ai ≤ n
Input: 3 5 5 2 1 3 4 3 1 2 3 4 4 3 2 1 Output: 2 0 3
case 1.Threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
case 2.There may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
|Time Limit:||0.1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, CLOJ, FS|
Fetching successful submissions