题目1502:最大值最小化
时间限制:1 秒
内存限制:128 兆
特殊判题:否
提交:583
解决:222
-
题目描述:
-
在印刷术发明之前,复制一本书是一个很困难的工作,工作量很大,而且需要大家的积极配合来抄写一本书,团队合作能力很重要。
当时都是通过招募抄写员来进行书本的录入和复制工作的, 假设现在要抄写m本书,编号为1,2,3...m, 每本书有1<=x<=100000页, 把这些书分配给k个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的。每个抄写员的速度是相同的,你的任务就是找到一个最佳的分配方案,使得所有书被抄完所用的时间最少。
-
输入:
-
输入可能包含多个测试样例。
第一行仅包含正整数 n,表示测试案例的个数。
对于每个测试案例,每个案例由两行组成,在第一行中,有两个整数m和 k, 1<=k<=m<=500。 在第二行中,有m个整数用空格分隔。 所有这些值都为正且小于100000。
-
输出:
-
对应每个测试案例,
输出一行数字,代表最佳的分配方案全部抄写完毕所需要的时间。
-
样例输入:
-
2 9 3 100 200 300 400 500 600 700 800 900 5 4 100 100 100 100 100
-
样例输出:
-
1700
200
-
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { new Task().solve() ; } } class Task{ InputReader in = new InputReader(System.in) ; PrintWriter out = new PrintWriter(System.out) ; int n , k , sum ; int[] value ; void solve(){ int t = in.nextInt() ; while(t-- > 0){ n = in.nextInt() ; k = in.nextInt() ; value = new int[n+1] ; sum = 0 ; for(int i = 1 ; i <= n ; i++){ value[i] = in.nextInt() ; sum += value[i] ; } out.println(binarySearch()); } out.flush(); } boolean judge(int targe){ int nowsum = 0 ; int cnt = 1 ; for(int i = 1 ; i <= n ; i++){ if(value[i] > targe) return false ; if(nowsum + value[i] <= targe){ nowsum += value[i] ; } else{ nowsum = value[i] ; cnt++ ; } } return cnt <= k ; } int binarySearch(){ int left = 0 , right = sum , res = -1 ; while(left <= right){ int mid = (left + right) >> 1 ; if(judge(mid)){ res = mid ; right = mid - 1 ; } else left = mid + 1 ; } return res ; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = new StringTokenizer(""); } private void eat(String s) { tokenizer = new StringTokenizer(s); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } }
-