Problem Descriptionmatt ' s friend K.bro is an acmer.
Yesterday, K.bro learnt an algorithm:bubble sort. Bubble sort would compare each pair of adjacent items and swaps them if they is in the wrong order. The process repeats until no swap is needed.
Today, K.bro comes up with a new algorithm and names it k.bro sorting.
There is many rounds in K.bro sorting. For each round, K.bro chooses a number, and keeps swapping it with its next number and the next number are less than it. For example, if the sequence is "1 4 3 2 5", and K.bro chooses "4", he'll get "1 3 2 4 5" after this round. K.bro sorting is similar to Bubble sort, but it's a randomized algorithm because K.bro would choose a random number at the Beginning of each round. K.bro wants to know which, for a given sequence, how many rounds is needed to sort this sequence in the best situation. In other words, you should answer the minimal number of rounds needed to sort the sequence into ascending order. To simplify the problem, K.bro promises that the sequence is a permutation of 1, 2, ..., N.
Inputthe first line contains only one integer T (t≤200), which indicates the number of test cases. For each test case, the first line contains an integer N (1≤n≤106).
The second line contains N integers ai (1≤ai≤n), denoting the sequence K.bro gives.
The sum of N in all test cases would not exceed 3x106.
Outputfor each test case, output a single line "Case #x: Y", where x was the case number (starting from 1), and y is the minima L Number of rounds needed to sort the sequence.
Sample Input255 4 3 2 155 1 2 3 4
Sample outputcase #1:4Case #2:1
Test instructions: Give a sequence, from small to large, sort by the following rules. Select a number from the sequence to move right until the number on the right is larger than it will stop. Ask at least how many steps.
Idea: For a number if it has a smaller number on the right, then it must move. If you move the largest number at a time, then a number can be moved at most once, so the question is simplified into the number of numbers in the series that are smaller than the right.
So just start the loop from the right, never record the minimum, and with the minimum and the next number, if the next value is larger than the minimum value, the result is 1.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 using namespacestd;6 inta[1000006];7 intMain ()8 {9 intmn,i,t,n,ans,k=1;Tenscanf"%d",&t); One while(t--) A { -ans=0; -scanf"%d",&n); the for(i=1; i<=n;i++) scanf ("%d",&a[i]); -mn=A[n]; - for(i=n-1; i>=1; i--) - { + if(Mn>a[i]) mn=A[i]; - Elseans++; + } Aprintf"Case #%d:%d\n", K,ans); atk++; - } -}
Hdu 5122 K.bro Sorting (water problem)