CCF CSP Brush Question Record 26-201809-2 Shopping (Java)

Question number: 201809-2
Question name: Grocery shopping
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Little H and Little W came to a street, and they bought groceries separately. The process of buying groceries can be described as going to the store to buy some groceries and then going to a square next to load the groceries on the cart. Both of them buy n kinds of vegetables. Food, so it must be loaded n times. Specifically, for small H, there are n disjoint time periods [a1,b1],[a2,b2]...[an,bn] are loading, and for small W, there are n disjoint periods The time period [c1,d1],[c2,d2]...[cn,dn] is loading. Among them, a time period [s, t] represents the time from time s to time t, and the duration is ts.
  Since they are good friends, they all chat while loading the car in the square, and they want to know how long they can chat.

Input format

  The first line of input contains a positive integer n, which represents the number of time periods.
  In the next n lines, each line has two numbers ai, bi, describing the time period of each loading of small H.
  In the next n lines, each line has two numbers ci, di, describing the time period of each loading of small W.

Output format

  Output one line, a positive integer, indicating how long the two can talk.

Sample input

4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14

Sample output

3

Data size and convention

  For all evaluation use cases, 1 ≤ n ≤ 2000, ai <bi <ai+1, ci <di <ci+1, for all i (1 ≤ i ≤ n), 1 ≤ ai, bi, ci, di ≤ 1000000.

 

import java.util.Scanner;
public class 买菜 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int[] a=new int[1000001];
		int[] b=new int[1000001];
		for(int i=1;i<=n;i++){
			int s=sc.nextInt();
			int t=sc.nextInt();
			for(int j=s;j<t;j++){
			a[j]=1;
			}
		}
		for(int i=1;i<=n;i++){
			int s=sc.nextInt();
			int t=sc.nextInt();
			for(int j=s;j<t;j++){
			b[j]=1;
			}
		}
		int res=0;
		for(int i=1;i<=1000000;i++){
			if(a[i]==b[i]&&a[i]!=0&&b[i]!=0){
				res++;
			}
		}
		System.out.println(res);
	}
}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108365480
Recommended