CSU-1401: insert sort

Source: Internet
Author: User
Description

Select the leftmost Number of the sequence, and insert it to any position in the sequence. The number of times that the above operation must be repeated at least to change the sequence to an incremental sequence.

The first line of input contains an integer. T( T> 0), indicating a total TGroup test data. For each group of test data, the first row contains an integer. N(1 ≤ N≤ 105), indicating a total NInteger. The second line contains NDifferent integers (all of which are in the range of [1,109]) are described in sequence.

Output

For each group of test data, an integer is output, indicating at least how many times the above operations need to be repeated before this sequence can be changed to an incremental sequence.

Sample Input
331 2 331 3 251 5 4 3 2
Sample output
024
Hint

Source

The eighth session of the Program Design Competition for Central South University Students

// Set m to the length of the continuous ascending sequence ending with the last number. The answer is n-M.

For example, M: If it is 1 5 2 3 4, it is 3. 3, 2, and 1 are 1.

Because you can only move the number on the leftmost side, the optimal strategy is to keep the rising column on the rightmost side of what I am talking about and just move the remaining number.

#include<cstdio>inta[100005];intmain(){    intt,n,i,j,ans;    scanf("%d",&t);    while(t--)    {        ans=1;        scanf("%d",&n);        for(i=0;i<n;i++)            scanf("%d",&a[i]);        for(j=n-2;j>=0;j--)        {            if(a[j]<a[j+1])                ans++;            else break;        }        printf("%d\n",n-ans);    }    return0;}


 

 

CSU-1401: insert sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.