Dilpreet wants to paint his dog- Buzo’s home that has n boards with different lengths[A1, A2,…, An]. He hired k painters for this work and each painter takes 1 unit time to paint 1 unit of the board.The problem is to find the minimum time to get this job done under the constraints that any painter will only paint continuous sections of boards, say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}.
The first line consists of a single integer T, the number of test cases. For each test case, the first line contains an integer k denoting the number of painters and integer n denoting the number of boards. Next line contains n- space separated integers denoting the size of boards.Constraints:1<=T<=1001<=k<=301<=n<=501<=A[i]<=500
For each test case, the output is an integer displaying the minimum time for painting that house.
2
2 4
10 10 10 10
2 4
10 20 30 40
20
60
package org.alphacat.first;import java.util.Scanner;public class Painter {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int t = Int();for (int i = 0; i < t; i++) {int k = Int();int n = Int();int[] nums = new int[n];for (int j = 0; j < n; j++) {nums[j] = Int();}int size = problem(nums, k);System.out.println(size);}}public static int problem(int[] nums, int k) {int l = nums[0];int h = l;for (int i = 1; i < nums.length; i++) {l = Math.max(l, nums[i]);h += nums[i];}while (l < h) {int crrSize = l + ((h - l) >> 1);if (getPainterCount(nums, crrSize) > k) {l = crrSize + 1;} else {h = crrSize;}}return l;}private static int getPainterCount(int[] nums, int size) {int crrSize = 0;int cnt = 1;for (int i = 0; i < nums.length; i++) {crrSize += nums[i];if (crrSize > size) {crrSize = nums[i];++cnt;}}return cnt;}
}
本文发布于:2024-02-01 19:37:13,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170678743238972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |