Farmer John is attempting to sort his?NN?cows (1≤N≤1001≤N≤100), conveniently numbered?1…N1…N, before they head out to the pastures for breakfast.
Currently, the cows are standing in a line in the order?p1,p2,p3,…,pNp1,p2,p3,…,pN, and Farmer John is standing in front of cow?p1p1. He wants to reorder the cows so that they are in the order?1,2,3,…,N1,2,3,…,N, with cow?11?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?kk?paces down the line, for any?kk?in the range?1…N?11…N?1. The?kk?cows whom she passes will amble forward, making room for her to insert herself in the line after them.
For example, suppose that?N=4N=4?and the cows start off in the following order:
FJ: 4, 3, 2, 1
The only cow paying attention to FJ is cow?44. If he instructs her to move?22?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?33, so in the second time step he may give cow?33?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?NN.The second line contains?NN?space-separated integers,?p1,p2,p3,…,pNp1,p2,p3,…,pN, indicating the starting order of the cows.
A single integer: the number of time steps before the?NN?cows are in sorted order, if Farmer John acts optimally.
4 1 2 4 3
3
Problem credits: Dhruv Rohatgi
以上就是關于【USACO 2019 January Contest Bronze Problem 2 Sleepy Cow Sorting】的解答,如需了解學校/賽事/課程動態,可至翰林教育官網獲取更多信息。
往期文章閱讀推薦:
NOAI人工智能奧賽 2026-2027 活動章程出爐:新規則必看!
NOAI、UKOAI、USAAIO三大AI奧賽新賽季全面啟動:留學申請的“核武器”來了!

? 2026. All Rights Reserved. 滬ICP備2023009024號-1