UVa11413 - Fill the Containers(最大值最小化问题)
2014-04-19 20:49
357 查看
Time Limit: 1 sec
A conveyor belt has a number of vessels of different capacities each filled to brim with milk. The milk from conveyor belt is to be filled into 'm' containers. The constraints are:
Whenever milk from a vessel is poured into a container, the milk in the vessel must be completely poured into that container only. That is milk from same vessel can not be poured into different containers.
The milk from the vessel must be poured into the container in order which they appear in the conveyor belt. That is, you cannot randomly pick up a vessel from the conveyor belt and fill the container.
The ith container must be filled with milk only from those vessels that appear earlier to those that fill jth container, for all i < j
Given the number of containers 'm', you have to fill the containers with milk from all the vessels, without leaving any milk in the vessel. The containers need not necessarily have same capacity. You are given
the liberty to assign any possible capacities to them. Your job is to find out the minimal possible capacity of the container which has maximal capacity. (If this sounds confusing, read down for more explanations.)
Input Format
A single test case consist of 2 lines. The first line specifies 1<=n<=1000 the number of vessels in the conveyor belt and then 'm' which specifies the number of containers to which, you have to transfer the milk.(1 <= m <= 1000000) .The next line contains, the capacity 1<=c<=1000000 of each vessel in order which they appear in the conveyor belt. Note that, milk is filled to the brim of any vessel. So the capacity of the vessel is equal to the amount of milk in it.
There are several test cases terminated by EOF.
Output Format
For each test case, print the minimal possible capacity of the container with maximal capacity. That is there exists a maximal capacity of the containers, below which you can not fill the containers without increasingthe number of containers. You have to find such capacity and print it on a single line.
Sample Input
5 31 2 3 4 5
3 2
4 78 9
Sample Output
682
Explanation of the output:
Here you are given 3 vessels at your disposal, for which you are free to assign the capacity. You can transfer, {1 2 3} to the first container, {4} to the second, {5} to third. Here the maximal capacity of thecontainer is the first one which has a capacity of 6. Note that this is optimal too. That is, you can not have the maximal container, have a capacity, less than 6 and still use 3 containers only and fill the containers with all milk.
For the second one, the optimal way is, {4 78} into the first container, and {9} to the second container. So the minimal value of the maximal capacity is 82. Note that {4} to first container and {78 9} to the
second is not optimal as, there exists a way to have an assignement of maximal capacity to 82, as opposed to 87 in this case.
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
public class Main
{
public static boolean DEBUG = false;
public BufferedReader cin;
public PrintWriter cout;
public StreamTokenizer tokenizer;
public int n, m, sum;
public int[] a;
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
} catch (IOException e) {
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StreamTokenizer(cin);
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tokenizer.ttype == StreamTokenizer.TT_WORD)
return tokenizer.sval;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER)
return String.valueOf((int)tokenizer.nval);
} catch (IOException e) {
}
return null;
}
public boolean input()
{
try {
n = Integer.parseInt(next());
a = new int
;
m = Integer.parseInt(next());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(next());
sum += a[i];
}
return true;
} catch (Exception e) {
return false;
}
}
private boolean check(int num)
{
int cnt = 0;
int s = 0;
for (int i = 0; i < n; i++) {
if (a[i] > num) return false;
if (s + a[i] > num) {
cnt++;
s = 0;
}
s += a[i];
}
if (cnt <= m - 1) return true;
return false;
}
public void solve()
{
int low = 0, high = sum;
while (low < high) {
int mid = (low + high) >> 1;
if (check(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
cout.println(low);
cout.flush();
}
public static void main(String[] args)
{
try {
Main solver = new Main();
solver.init();
while (solver.input()) {
solver.solve();
}
} catch (Exception e) {
}
}
}
相关文章推荐
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
- UVa 11413 - Fill the Containers
- Uva 714Copying Books,(最大值最小化问题)
- uva--714+二分(最大值最小化问题)
- 【UVa】11413 – Fill the Containers
- UVa 714 - Copying Books,最大值最小化问题
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
- UVa 714 Copying Books 二分 + 贪心 (最大值最小化问题)
- 【算法竞赛入门经典】树的最大独立集、树的唯一性问题 例题9-13 UVa1220
- uva 10603 Fill(倒水问题 BFS)
- 例题7-8 倒水问题(Fill, UVa 10603)
- UVAlive 7037 - The Problem Needs 3D Arrays(网络流‘最大密度子图)
- UVa 11520 - Fill the Square
- Fill the Square UVA - 11520
- 最大值最小化--uva714
- UVA 11520 Fill the Square
- (顺序表应用5.1.2)UVA 113 The Dole Queue(双向约瑟夫环问题:给出总人数n,顺时针数k个,逆时针数m个)
- UVA 133 The Dole Queue 约瑟夫问题的变种,手写链表,模拟
- 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)
- 最大值最小化问题——分治