聊天

题目描述
A和B是好友,他们经常在空闲时间聊天,A的空闲时间为[a1 ,b1 ],[a2 ,b2 ]..[ap ,bp ]。B的空闲时间是[c1 +t,d1 +t]..[cq +t,dq +t],这里t为B的起床时间。这些时间包括了边界点。B的起床时间为[l,r]的一个时刻。若一个起床时间能使两人在任一时刻聊天,那么这个时间就是合适的,问有多少个合适的起床时间?
输入描述:
第一行数据四个整数:p,q,l,r(1≤p,q≤50,0≤l≤r≤1000)。接下来p行数据每一行有一对整数ai,bi(0≤aii+1>bi,ci+1>di
输出描述:
输出答案个数
示例1
输入
2 3 0 20
15 17
23 26
1 4
7 11
15 17
输出
20

A转换成Arraylist,用contains去比较B,如果B的c+t或d+t被包含在A中,就停止查询,该t符合条件,查询下一个。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int p = sc.nextInt();
			int q = sc.nextInt();
			int l = sc.nextInt();
			int r = sc.nextInt();
			int t;
			int[][] A = new int[p][2];
			int[][] B = new int[q][2];
			for (int i = 0; i < p; i++) {
				A[i][0]=sc.nextInt();
				A[i][1]=sc.nextInt();
			}
			for (int i = 0; i < q; i++) {
				B[i][0]=sc.nextInt();
				B[i][1]=sc.nextInt();
			}
			int property = getProperty(A, B, l, r, p, q);
			System.out.println(property);
		}
	}
	
	public static int getProperty(int[][]A,int[][] B,int l,int r,int p,int q) {
		int property = 0;
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		for (int i = 0; i < A.length; i++) {
			int a = A[i][0];
			int b = A[i][1];
			while(a<=b) {
				arrayList.add(a);
				a++;
			}
		}
		for (int i = l; i <= r; i++) {
			int t=i;
			boolean contain = false;
			for (int j = 0; j < B.length; j++) {
				int c=t+B[j][0];
				int d=t+B[j][1];
				if(arrayList.contains(c)||arrayList.contains(d)) {
					contain = true;
					break;
				}
			}
			if(contain)property++;
		}
		
		return property;
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值