题目:N个歌手,M个评委,(N>=M)评论时间T为M个评委评论中评论时间最长的
歌手顺序不能变,评委只能评价连续唱歌的歌手,如给出N、M、及各歌手唱歌时间,求T
public static void main(String[] args) throws IOException {
method();
}
private static String[] getInput(int linesCounts) {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String line[] = new String[linesCounts];
try {
for (int i = 0; i < linesCounts; i++) {
line[i] = stdin.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
private static void method() {
String[] lines = getInput(2);
int singerCounts = Integer.parseInt(lines[0].split(" ")[0]);
int judgeCounts = Integer.parseInt(lines[0].split(" ")[1]);
String[] singers = lines[1].split(" ");
int[] times = new int[singerCounts];
for (int i = 0; i < singerCounts; i++) {
times[i] = Integer.parseInt(singers[i]);
}
System.out.println(minLeng(times, judgeCounts));
}
}
//递归求最小时间算法,多维递归
private static int minLeng(int[] times, int part) {
int length = Integer.MAX_VALUE;
if (part > 1) {//如果裁判数量大于1,就考虑第一个裁判分别判第一个歌手、前两个歌手...然后找出这些情况中时间最小的
int size = times.length;
int sum = 0;
for (int i = 0; i <= size - part; i++) {
sum += times[i];
int tmpLength = minLeng(Arrays.copyOfRange(times, i + 1, size), part - 1);
tmpLength = tmpLength < sum ? sum : tmpLength;
length = length > tmpLength ? tmpLength : length;
}
} else {//如果裁判数量为1,最小长度即为所有歌手唱歌时间之和
length = 0;
for (int i = 0; i < times.length; i++) {
length += times[i];
}
}
return length;
}
觉得这些题目挺好的,很考查算法技巧及逻辑能力