当前位置:网站首页>Sorting of poor cattle (winter vacation daily question 40)

Sorting of poor cattle (winter vacation daily question 40)

2022-06-25 03:33:00 51CTO

Farmer John Trying to put his  Trapped cattle sorting ( Winter vacation every day 40)_ structure A cow , For convenience, the number is  Trapped cattle sorting ( Winter vacation every day 40)_ios_02, Put them in order before they go to the pasture for breakfast .

At present , These cows feed on  Trapped cattle sorting ( Winter vacation every day 40)_ Thinking questions _03 Line up in the order of ,Farmer John Standing on the cow  Trapped cattle sorting ( Winter vacation every day 40)_#include_04 front .

He wants to rearrange these cows , Make their order  Trapped cattle sorting ( Winter vacation every day 40)_ Thinking questions _05, cow  Trapped cattle sorting ( Winter vacation every day 40)_ Thinking questions _06 stay Farmer John side .

The cows are a little sleepy today , So at any time, only direct face Farmer John Your cow will listen Farmer John Instructions .

He can move back along the line every time  Trapped cattle sorting ( Winter vacation every day 40)_#include_07 Step , Trapped cattle sorting ( Winter vacation every day 40)_#include_07 It can be a range  Trapped cattle sorting ( Winter vacation every day 40)_ Thinking questions _09 Any number in .

She passed by  Trapped cattle sorting ( Winter vacation every day 40)_#include_07 A cow will move forward , Make room for her to insert behind these cows in the team .

for example , hypothesis  Trapped cattle sorting ( Winter vacation every day 40)_#include_11, The cows started in this order :

FJ: ​​4, 3, 2, 1​

The only thing to notice FJ The cow instructed is the cow  Trapped cattle sorting ( Winter vacation every day 40)_ios_12.

When he ordered her to move behind the line  Trapped cattle sorting ( Winter vacation every day 40)_ Insertion sort _13 After step , The order of the team will become :

FJ: ​​3, 2, 4, 1​

Now the only thing to notice FJ The cow instructed is the cow  Trapped cattle sorting ( Winter vacation every day 40)_ structure _14, So the second time he can give the cow  Trapped cattle sorting ( Winter vacation every day 40)_ structure _14 Give orders , Do this until the cows are in order .

Farmer John Eager to finish sorting , So he can go back to his farmhouse and enjoy his own breakfast .

Please help him find out the minimum number of operations required to put the cows in order .

Input format
The first line of input contains  Trapped cattle sorting ( Winter vacation every day 40)_ structure .

The second line contains  Trapped cattle sorting ( Winter vacation every day 40)_ structure Space separated integers , Trapped cattle sorting ( Winter vacation every day 40)_ Thinking questions _03, Indicates the starting order of cows .

Output format
Output an integer , by Farmer John Using the best strategy can make this  Trapped cattle sorting ( Winter vacation every day 40)_ structure The number of operations required for a good order of head milk steak .

Data range
 Trapped cattle sorting ( Winter vacation every day 40)_#include_20

sample input :

4
1 2 4 3

sample output :

3


      
      
#include<iostream>
#include<string>

using namespace std;

const int N = 110;

int n;
int a[ N];

int main(){

scanf( "%d", & n);

for( int i = 1; i <= n; i ++) scanf( "%d", & a[ i]);

a[ 0] = n + 1;
for( int i = n; i >= 1; i --)
if( a[ i] < a[ i - 1]){
printf( "%d", i - 1);
break;
}

return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242133161936.html