思路:就是模拟杀手杀人的过程,新开一个数组用于标记被杀死的位置。然后生成新的数组,直到平安夜来临。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
boolean flag = true;
int res = 0;
while (flag) {
int died =0;
int[] kill = new int[arr.length];
//标记已经被杀死的位置
for (int i = 0; i < arr.length-1; i++) {
if(arr[i]>arr[i+1]){
kill[i + 1] = 1;
died++;
}
}
//死亡人数是0->今夜是平安夜
if(died==0){
System.out.println(res);
break;
}
//清除尸体
for (int i = 0; i < arr.length; i++) {
if (kill[i] == 1) {
arr[i] = 0;
}
}
//生成新数组
res++;
int newlen = arr.length - died;
int[] lived = new int[newlen];
int index = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
lived[index++] = arr[i];
}
if(index==newlen) break;
}
arr = lived;
}
}
}
}
原文:https://www.cnblogs.com/keeya/p/9606847.html