admin管理员组文章数量:1794759
oj
Description
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}.
Input
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
Output
For each test case, the output is an integer displaying the minimum time for painting that house.
Sample Input 1
2
2 4
10 10 10 10
2 4
10 20 30 40
Sample Output 1
20
60
Code
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 = scanner.nextInt();for (int i = 0; i < t; i++) {int k = scanner.nextInt();int n = scanner.nextInt();int[] nums = new int[n];for (int j = 0; j < n; j++) {nums[j] = scanner.nextInt();}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;}
}
本文标签: OJ
版权声明:本文标题:oj 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1730963299a946396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论