package com.sf.ccmas.video.config.odd.od4;
import com.sf.cemp.framework.common.util.object.SfObjectUtil;
import org.aspectj.org.eclipse.jdt.core.IField;
import java.util.Arrays;
import java.util.Scanner;
/**
* 题目描述
*
* 【最短木板长度】
*
*
*
* 小明有 n 块木板,第 i ( 1 ≤ i ≤ n ) 块木板长度为 ai。
* 小明买了一块长度为 m 的木料,这块木料可以切割成任意块,
* 拼接到已有的木板上,用来加长木板。
* 小明想让最短的木板尽量长。
* 请问小明加长木板后,最短木板的长度可以为多少?
*
* 输入描述
* 输入的第一行包含两个正整数,n(1≤n≤10^3),m(1≤m≤10^6)
* n表示木板数,m表示木板长度。输入的第二行包含n个正整数,a1,a2,...an(1≤ai≤10^6)。
*
* 输出描述
* 输出的唯一一行包含一个正整数,表示加长木板后,最短木板的长度最大可以为多少?
*
* 示例一:
* 输入:
* 5 3
* 4 5 3 5 5
* 输出:
* 5
*
* 说明:
* 给第1块木板长度增加1,给第3块木板长度增加2后,
* 这5块木板长度变为[5,5,5,5,5],最短的木板的长度最大为5。
*
* 示例二:
* 输入:
* 5 2
* 4 5 3 5 5
* 输出:
* 4
*
* 输入:
* 5 8
* 2 3 7 7 7
*
* 给第3块木板长度增加1后,
* 这5块木板长度变为[4,5,4,5,5],剩余的木料长度为1。此时剩余木料无论给哪块木板加长,最短木料的长度都为4。
*
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String strUp = scanner.nextLine();
String strDown = scanner.nextLine();
String[] splitUp = strUp.split(" ");
int n =Integer.parseInt( splitUp[0]);
int m =Integer.parseInt( splitUp[1]);
String[] splitDown = strDown.split(" ");
Arrays.sort(splitDown);
int sum = Integer.parseInt(splitDown[0]);
int left = 0;
int total = m;
for (int i = 1; i < n; i++) {
int curNum = Integer.parseInt(splitDown[i]);
int preVal = Integer.parseInt(splitDown[left]);
System.out.println("i=" + i + " curNum=" + curNum + " preVal=" + preVal);
//差值
int diff = curNum - preVal;
System.out.println(" m=" + m + " diff=" + diff);
if (m > 0 && diff >= 0) {
if (m > diff*i) {
m -= diff*i;
//如果在循环中就可以用完木料,则直接输出
// if (m == 0) {
// //取出 preVal + m/i
// System.out.println(preVal + diff/i);
// break;
// }
}else {
System.out.println(" i=" + i + " sum ="+ sum + " total=" + total);
System.out.println((sum + total) / i);
//将m置0
break;
}
}
sum += curNum;
//左指针右移
left++;
}
//如果指针跑完整个数组,木料都还有多的,趣最大值 + m/n, 比如 最大值是5, m还剩余7
if (total > sum) {
System.out.println(" sum=" + sum);
System.out.println((sum + total) / n);
}
}
}
华为OD题目:最短木板长度
最新推荐文章于 2023-12-11 22:45:50 发布