欢迎访问我的CCF认证解题目录
题目描述
思路过程
新建一个长度为1000005的boolean
数组用来标记时间段区间。
首先将小H的时间段对应的下标都标记为true,接着读入小W的时间段,如果时间段对应的下标为true,则表明可以聊天,cnt++
例:
- 1 3表示区间2和3
- 2 4表示区间3和4
- 同样有区间3,cnt++
JAVA 11行代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), cnt = 0;
boolean[] flags = new boolean[1000005];//用于标记区间
for ( int i = 0; i < n; i++ )
for ( int st = in.nextInt(), ed = in.nextInt(); st < ed; st++ )
flags[st+1] = true;
for ( int i = 0; i < n; i++ )
for ( int st = in.nextInt(), ed = in.nextInt(); st < ed; st++ )
if ( flags[st+1] )
cnt++;
System.out.println(cnt);
}
}