#PU892. USACO 2019 January Contest, Bronze — Problem 2. Sleepy Cow Sorting
USACO 2019 January Contest, Bronze — Problem 2. Sleepy Cow Sorting
USACO 2019 January Contest, Bronze — Problem 2. Sleepy Cow Sorting
题目描述
Farmer John is attempting to sort his cows (), conveniently numbered , before they head out to the pastures for breakfast. Currently, the cows are standing in a line in the order , and Farmer John is standing in front of cow . He wants to reorder the cows so that they are in the order , with cow next to Farmer John. The cows are a bit sleepy today, so at any point in time the only cow who is paying attention to Farmer John's instructions is the cow directly facing Farmer John. In one time step, he can instruct this cow to move paces down the line, for any in the range . The cows whom she passes will amble forward, making room for her to insert herself in the line after them. For example, suppose that and the cows start off in the following order: FJ: 4, 3, 2, 1 The only cow paying attention to FJ is cow . If he instructs her to move paces down the line, the order will subsequently look like this: FJ: 3, 2, 4, 1 Now the only cow paying attention to FJ is cow , so in the second time step he may give cow an instruction, and so forth until the cows are sorted. Farmer John is eager to complete the sorting, so he can go back to the farmhouse for his own breakfast. Help him find the minimum number of time steps required to sort the cows.
输入格式
The first line of input contains . The second line contains space-separated integers, , indicating the starting order of the cows.
输出格式
A single integer: the number of time steps before the cows are in sorted order, if Farmer John acts optimally.
数据范围
(见题面原文)
样例
样例 1
4
1 2 4 3
3
Problem credits: Dhruv Rohatgi