Java实现洛谷 P1873 砍树(StreamTokenizer+IO+二分)
作者:互联网
P1873 砍树
输入输出样例
输入
5 20
4 42 40 26 46
输出
36
PS:
get新知识,以前只知道STringTokenizer并没有了解过StreamTokenizer,这次才知道
原来io输入的jdk提供了方法接收这个,大概感觉貌似差不多,好久没用io了,一边百度一边用
现学现卖哈哈
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class 砍树 {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int n = (int)in.nval;
in.nextToken();
int m = (int)in.nval;
int[] s = new int[n];
int l = 0,r = 0;
for(int i = 0;i < n;i++) {
in.nextToken();
s[i] = (int)in.nval;
r = Math.max(r, s[i]);
}
while(l <= r) {
int mid = (l + r) / 2;
if(judge(mid,s,m)) {
l = mid + 1;
}else {
r = mid - 1;
}
}
out.print(l - 1);
out.close();
}
public static boolean judge(int mid,int[] s,int m) {
int sum = 0;
for(int i = 0;i < s.length;i++) {
if(s[i] > mid) {
sum += (s[i] - mid);
if(sum >= m) {
return true;
}
}
}
return false;
}
}
标签:StreamTokenizer,P1873,java,int,import,io,new,洛谷 来源: https://blog.csdn.net/a1439775520/article/details/105295682