oj

阅读: 评论:0

oj

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 = 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小时内删除。

标签:oj
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23