UVa11413 - Fill the Containers(最大值最小化问题)

简介: UVa11413 - Fill the Containers(最大值最小化问题)
importjava.io.IOException;
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.io.BufferedReader;
importjava.io.PrintWriter;
importjava.io.OutputStreamWriter;
importjava.io.StreamTokenizer;
publicclassMain{
publicstaticbooleanDEBUG=false;
publicBufferedReadercin;
publicPrintWritercout;
publicStreamTokenizertokenizer;
publicintn, m, sum;
publicint[] a;
publicvoidinit()
    {
try {
if (DEBUG) {
cin=newBufferedReader(newInputStreamReader(
newFileInputStream("d:\\OJ\\uva_in.txt")));
            } else {
cin=newBufferedReader(newInputStreamReader(System.in));
            }
        } catch (IOExceptione) {
        }
cout=newPrintWriter(newOutputStreamWriter(System.out));
tokenizer=newStreamTokenizer(cin);
    }
publicStringnext()
    {
try {
tokenizer.nextToken();
if (tokenizer.ttype==StreamTokenizer.TT_EOF)
returnnull;
elseif (tokenizer.ttype==StreamTokenizer.TT_WORD)
returntokenizer.sval;
elseif (tokenizer.ttype==StreamTokenizer.TT_NUMBER) 
returnString.valueOf((int)tokenizer.nval);
        } catch (IOExceptione) {
        }
returnnull;
    }
publicbooleaninput()
    {
try {
n=Integer.parseInt(next());
a=newint[n];
m=Integer.parseInt(next());
for (inti=0; i<n; i++) {
a[i] =Integer.parseInt(next());
sum+=a[i];
            }
returntrue;
        } catch (Exceptione) {
returnfalse;
        }
    }
privatebooleancheck(intnum)
    {
intcnt=0;
ints=0;
for (inti=0; i<n; i++) {
if (a[i] >num) returnfalse;
if (s+a[i] >num) {
cnt++;
s=0;
            }
s+=a[i];
        }
if (cnt<=m-1) returntrue;
returnfalse;
    }
publicvoidsolve()
    {
intlow=0, high=sum;
while (low<high) {
intmid= (low+high) >>1;
if (check(mid)) {
high=mid;
            } else {
low=mid+1;
            }
        }
cout.println(low);
cout.flush();
    }
publicstaticvoidmain(String[] args)
    {
try {
Mainsolver=newMain();
solver.init();
while (solver.input()) {
solver.solve();
            }
        } catch (Exceptione) {
        }
    }
}
目录
相关文章
|
6月前
|
人工智能 容器
Leetcode 11. Container With Most Water
题目可以这么理解,在i位置有条高为ai的竖线,让你选出两台竖线构成一个容器,使得该容器装的水最多,注意容器不能倾斜。
25 3
|
9月前
UVa837 - Light and Transparencies(排序)
UVa837 - Light and Transparencies(排序)
38 0
LeetCode 48. Rotate Image
给定一个n x n的二维矩阵,以顺时针方向旋转90度
57 0
LeetCode 48. Rotate Image
|
机器学习/深度学习 存储 人工智能
LeetCode 832. Flipping an Image
LeetCode 832. Flipping an Image
81 0
|
机器学习/深度学习
Leetcode-Easy 887. Projection Area of 3D Shapes
Leetcode-Easy 887. Projection Area of 3D Shapes
108 0
Leetcode-Easy 887. Projection Area of 3D Shapes
|
人工智能 算法 容器
leetcode 11 Container with Most Water
1.题目描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).
1296 0
LeetCode - 48. Rotate Image
48. Rotate Image  Problem's Link  ---------------------------------------------------------------------------- Mean:  顺时针旋转矩阵.
807 0