• Question No. 2nd
• For illustrative example: 5 2 3 1 7 4 6
Ideas:
Maintain a minimum value of T, initially the last number, here is 6;
We consider from the back forward, first look at the end of 6, its front is 4, do not need to move, change T to a smaller 4;
Again forward, for 7, at this time 7>t, then for 7, it is definitely need to move, make num++;
Again forward, 1<t, make t=1;
Forward, because T is 1, all values will be >t, for 3, 2, 5, all need to num++.
Num was added 4 times and the result is 4.
Explain:
Because the exchange of numbers in the topic has a feature, is to choose a number x, from the front to the back, until the x< the number behind it;
So for each number, as long as there is a smaller number behind it, it will be a shift!
(The meaning of this sentence is to find the minimum value from all the numbers after the current number, and compare it to it, and we can do it from the back to the minimum, which greatly reduces the complexity of the time),
AC Code:
1#include <iostream>2#include <stdio.h>3#include <algorithm>4#include <cstring>5#include <string.h>6#include <math.h>7#include <queue>8#include <stack>9#include <stdlib.h>Ten#include <map> One using namespacestd; A - #defineLL Long Long - #defineSF (a) scanf ("%d",& (a)); the #defineINF 2e9 - #defineINF 2147483647 - #defineN 25 - #definePI 3.141592653 + #defineEPS 1e-8 - + intF[n]; A intMain () { at intT,n;intk=1; -scanf"%d",&T); - while(t--){ -scanf"%d",&n); - for(intI=0; i<n;i++){ -scanf"%d",&f[i]); in } - intnum=0;intt = f[n-1]; to for(inti=n-2; i>=0; i--) + if(f[i]>t) num++; - Elset =F[i]; theprintf"Case #%d:", k++); *printf"%d\n", num); $ }Panax Notoginseng return 0; -}
1011 K.bro Sorting