题目解析
求最少需要建设多少个山头可以满足任何时间点都不存在超过一只处于活跃时间段的大熊猫。
解题思路
差分模板题目
将给定的时间段里面活动的熊猫数量都增加1,然后找到某个时间上活动最多的熊猫数量即可
代码
import java.io.*;
import java.util.*;
public class Main
{
// 将给定时间转换为秒
static int getTime(String time)
{
int h = Integer.parseInt(time.substring(0, 2));
int m = Integer.parseInt(time.substring(3, 5));
int s = Integer.parseInt(time.substring(6, 8));
return h * 3600 + m * 60 + s;
}
public static void main(String[] args) throws IOException
{
int n = sc.nextInt();
int[] shu = new int[60 * 60 * 24 + 10];
for (int i = 1; i <= n; i++)
{
int start = getTime(sc.next());
int end = getTime(sc.next());
shu[start] += 1;
shu[end + 1] -= 1;
}
int max = 0;
for (int i = 1; i <= 60 * 60 * 24; i++)
{
shu[i] += shu[i - 1];
max = Math.max(max, shu[i]);
}
out.println(max);
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}