题目
一、思路
将时间段分成时刻,计算小H和小W在每个时刻是否在装车(在装车则他们两个该时刻可以聊天),在装车则该时刻标记+1,若该时刻两人都在,则标记应该为2。最后计算标记为2的时刻数即为他们两个聊天的时长。
二、代码
代码如下(示例):
import java.util.Scanner;
public class one_1201 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []res = new int[1000001];
int a = 0,b = 0;
int len = 2*n;
int time = 0;
for (int i = 0; i < len; i++) {
a= sc.nextInt();
b =sc.nextInt();
for (int j = a; j < b; j++) {
res[j]++;
}
}
for (int i = 0; i < res.length; i++) {
if (res[i]==2) {
time++;
}
}
System.out.print(time);
}
}