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) {
        }
    }
}
目录
相关文章
|
10月前
|
人工智能 容器
Leetcode 11. Container With Most Water
题目可以这么理解,在i位置有条高为ai的竖线,让你选出两台竖线构成一个容器,使得该容器装的水最多,注意容器不能倾斜。
41 3
UVa837 - Light and Transparencies(排序)
UVa837 - Light and Transparencies(排序)
48 0
LeetCode 48. Rotate Image
给定一个n x n的二维矩阵,以顺时针方向旋转90度
71 0
LeetCode 48. Rotate Image
|
机器学习/深度学习 存储 人工智能
LeetCode 832. Flipping an Image
LeetCode 832. Flipping an Image
97 0
AtCoder Beginner Contest 222 E - Red and Blue Tree(dfs dp)
AtCoder Beginner Contest 222 E - Red and Blue Tree(dfs dp)
108 0
|
人工智能 算法 容器
leetcode 11 Container with Most Water
1.题目描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).
1310 0
|
数据挖掘
[LeetCode]--48. Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 本地使得二维矩阵,旋转90角度。 通过实际数据分析,通过两个步骤的元素交换可实现目标:
1226 0
|
容器 人工智能
[LeetCode]--11. Container With Most Water
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
1110 0