The problem defines a new sort algorithm, which is to put a number in a sequence of sequences, if its right number is smaller than it
You can move to the right of the past until the number on the right is larger than it.
Easy to get, if the simulation is O (n^2) efficiency, certainly not
Thinking about it, this problem can be translated into
For each element in this sequence, the right side of the string is less than its number and, if present, ++ans
I didn't think of it at first. = = should not be
1 //#pragma COMMENT (linker, "/stack:16777216")//For C + + Compiler2#include <stdio.h>3#include <iostream>4#include <cstring>5#include <cmath>6#include <stack>7#include <queue>8#include <vector>9#include <algorithm>Ten #definell Long Long One #defineMax (b) ((a) > (b))? (a): (b)) A #defineMin (b) ((a) < (a))? (a): (b)) - #defineAbs (x) (((x) > 0)? (x): (-(x))) - the using namespacestd; - - Const intINF =0x3f3f3f3f; - inta[1000001]; + - intMain () { + intI, J, K, T, N, m, numcase =0; Ascanf"%d",&t); at while(t--){ -scanf"%d",&n); - for(i =0; I < n; ++i) scanf ("%d",&a[i]); - intAns =0; - for(i = n-2; I >=0; --i) { - if(A[i] > a[i +1]){ inSwap (A[i], a[i +1]); -++ans; to } + } -printf"Case #%d:%d\n",++numcase, ans); the } * return 0; $}
HDU 5122 K.bro Sorting (2014 Beijing Regional Game K-problem simulation)