题目1502:最大值最小化 二分
2017-03-20 17:59
190 查看
题目1502:最大值最小化
时间限制:1 秒
内存限制:128 兆
特殊判题:否
提交:583
解决:222
题目描述:
在印刷术发明之前,复制一本书是一个很困难的工作,工作量很大,而且需要大家的积极配合来抄写一本书,团队合作能力很重要。
当时都是通过招募抄写员来进行书本的录入和复制工作的, 假设现在要抄写m本书,编号为1,2,3...m, 每本书有1<=x<=100000页, 把这些书分配给k个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的。每个抄写员的速度是相同的,你的任务就是找到一个最佳的分配方案,使得所有书被抄完所用的时间最少。
输入:
输入可能包含多个测试样例。
第一行仅包含正整数 n,表示测试案例的个数。
对于每个测试案例,每个案例由两行组成,在第一行中,有两个整数m和 k, 1<=k<=m<=500。 在第二行中,有m个整数用空格分隔。 所有这些值都为正且小于100000。
输出:
对应每个测试案例,
输出一行数字,代表最佳的分配方案全部抄写完毕所需要的时间。
样例输入:
样例输出:
时间限制: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
样例输出:
1700200
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()); } }
相关文章推荐
- 题目1502:最大值最小化
- 题目1502:最大值最小化
- 九度OJ-题目1502:最大值最小化
- POJ - 3273 Monthly Expense(二分搜索:最小化最大值)
- 序列变换(二分,最小化最大值)
- POJ_3104_Drying_(二分,最小化最大值)
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
- poj 3273- Monthly Expense(最大值最小化)-二分
- 二分匹配 模版 及最大总结 及 题目集合 +解析
- 【Zzulioj 1919】多校训练三(二分->最大值最小化)
- uva 714 - Copying Books(贪心 最大值最小化 二分)
- UVa 714 Copying Books——二分最大值最小化
- POJ 3273 Monthly Expense (二分,最小化最大值)
- poj2456Aggressive cows(二分搜索最小化最大值)
- [省选前题目整理][BZOJ 1189][HNOI 2007]紧急疏散(BFS+二分+最大流)
- POJ 3273 Monthly Expense【二分】(最大值最小化)
- BZOJ-3130 费用流 (听题目胡扯丶裸最大流) 二分判定+最大流+实数精度乱搞
- 九度 1502:最大值最小化(二分法)
- 最大值最小化 二分查找
- 最大值最小化-较为精简(二分+贪心)