狐进行了一次黑客马拉松大赛,全公司一共分为了N个组,每组一个房间排成一排开始比赛,比赛结束后没有公布成绩,但是每个组能够看到自己相邻的两个组里比自己成绩低的组的成绩,比赛结束之后要发奖金,以1w为单位,每个组都至少会发1w的奖金,另外,如果一个组发现自己的奖金没有高于比自己成绩低的组发的奖金,就会不满意,作为比赛的组织方,根据成绩计算出至少需要发多少奖金才能让所有的组满意。
输入描述:
每组数据先输入N,然后N行输入N个正整数,每个数表示每个组的比赛成绩。
输出描述:
输出至少需要多少w的奖金
输入例子:
10
20
32
12
32
45
11
21
31
41
33
输出例子:
20
(不知道是不是我理解错了题目,我觉得这里该为23)
java代码实现:
package com.example.demo;
import java.util.*;
/**
* Created by Administrator on 2019/4/4.
*/
public class Test3 {
public static void main(String[] args) {
try {
ArrayList<Integer> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String text;
while ((text = sc.nextLine()) != null) {
//控制台输入 enter 键结束录入
if ("".equals(text)) {
break;
}
//获取输入成绩
int grade = Integer.parseInt(text);
list.add(grade);
}
System.out.println(getTotalMinMoney(list));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static int getTotalMinMoney(ArrayList<Integer> list) {
if (list == null || list.size() == 0) {
return 0;
}
//获取最小的成绩数
Integer minGrade = Collections.min(list);
int minGradeIndex = 0;
for (int j = 0; j < list.size(); j++) {
if (minGrade == list.get(j)) {
minGradeIndex = j;
break;
}
}
//如果最小值的下标在第一个元素或者最后一个元素,那么用原数组
if (minGradeIndex == 0) {
return getTotalMoney(list);
} else if (minGradeIndex == list.size() - 1) {
//倒序 让最小的元素放在index=0的位置
Collections.reverse(list);
return getTotalMoney(list);
} else {
//把原list以最小下标为分界截取为两个list
ArrayList<Integer> aList = (ArrayList<Integer>) list.clone();
ArrayList<Integer> bList = (ArrayList<Integer>) list.clone();
List<Integer> list1 = subList(aList, 0, minGradeIndex + 1);
List<Integer> list2 = subList(bList, minGradeIndex, list.size());
Collections.reverse(list1);
return getTotalMoney(list1) + getTotalMoney(list2) - 1;
}
}
private static int getTotalMoney(List<Integer> list) {
int totalMoney = 0;
int prevMoney = 0;
for (int n = 0; n < list.size(); n++) {
int init;
if (n == 0) {
init = 1;
prevMoney = 1;
} else {
if (list.get(n) > list.get(n - 1)) {
init = prevMoney + 1;
} else if (list.get(n) == list.get(n - 1)) {
init = prevMoney;
} else {
//如果n角标的值小于n-1角标,那么默认奖金1W
init = 1;
}
prevMoney = init;
}
totalMoney = totalMoney + init;
}
return totalMoney;
}
/**
* 截取list
* 左闭右开
*
* @param list
* @param beginIndex
* @param endIndex
* @return
*/
public static List<Integer> subList(List<Integer> list, int beginIndex, int endIndex) {
return list.subList(beginIndex, endIndex);
}
}