import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
arr.add(in.nextInt());
}
// 翻转
int count = 0;
while (arr.size() > 1) {
int max = Collections.max(arr);
// 最大值在顶部
if (arr.get(0) == max) {
Collections.reverse(arr);
count++;
}
// 最大值在中间
if (arr.get(0) != max && arr.get(arr.size() - 1) != max) {
// 找到最大值位置
int x = 0;
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == max) {
x = i;
break;
}
}
// 翻转前半部分
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int i = 0; i <= x; i++) {
temp.add(arr.get(i));
}
for (int i = 0; i <= x; i++) {
arr.set(x - i, temp.get(i));
}
count++;
}
// 最大值在底部
if (arr.get(arr.size() - 1) == max) {
arr.remove(arr.size() - 1);
}
}
System.out.println(count);
}
}
SWUST OJ 254: 翻煎饼
于 2023-05-19 15:35:13 首次发布