[Java] Milking Cows
程序员文章站
2022-06-27 23:19:51
/* Use the slash-star style comments or the system won't see your identification information *//*ID: lincans1LANG: JAVATASK: milk2 */import java.io.*;import java.util.*;public class milk2 {private final int MAXLENGTH = 1000000;public milk2(...
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: lincans1
LANG: JAVA
TASK: milk2
*/
import java.io.*;
import java.util.*;
public class milk2 {
private final int MAXLENGTH = 1000000;
public milk2() throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
BufferedReader f = new BufferedReader(new FileReader("milk2.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("milk2.out")));
int N = Integer.parseInt(f.readLine());
boolean[] flag = new boolean[MAXLENGTH];
int start = Integer.MAX_VALUE, end = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(f.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
start = Math.min(start, s);
end = Math.max(end, e);
while (s < e) {
flag[s++] = true;
}
}
int maxContinuous = 0, maxIdle = 0;
while (start < end) {
int continuous = 0, idle = 0;
while (start < end && flag[start]) {
continuous++;
start++;
}
while (start < end && !flag[start]) {
idle++;
start++;
}
maxContinuous = Math.max(maxContinuous, continuous);
maxIdle = Math.max(maxIdle, idle);
// System.out.println(maxContinuous + " " + maxIdle);
}
out.println(maxContinuous + " " + maxIdle);
out.close();
}
public static void main (String [] args) throws IOException {
new milk2();
}
}
本文地址:https://blog.csdn.net/weixin_41714373/article/details/111888485
下一篇: 淘宝店铺装修要注意哪些问题?
推荐阅读
-
JAVA性能调优-避免对boolean 判断
-
JAVA-性能调优-多用条件操作符
-
JAVA-性能调优-有条件地使用final 关键字
-
JAVA-性能调优-静态方法代替实例方法
-
JAVA-性能调优-多用条件操作符
-
30_数据库_第30天java_jdbc_(DBUtils)_讲义
-
java中关于return返回值的用法详解
-
策略模式优化Java代码中分支过多的情况(switch或ifelse等)
-
Java项目记录
-
Jackson转换报错java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.exc.InvalidDefinitionExc