第 20 次 CSP认证-202009-Java-风险人群筛查(100分)

风险人群筛查

时间限制: 1.0 秒
空间限制: 512 MiB

题目背景
    某地疫情爆发后,出于“应检尽检”的原则,我们想要通知所有近期经过该高危区域的居民参与核酸检测。

题目描述
    想要找出经过高危区域的居民,分析位置记录是一种简单有效的方法。

    具体来说,一位居民的位置记录包含 t 个平面坐标 (x1,y1),(x2,y2),⋯,(xt,yt),其中 (xi,yi) 表示该居民 i 时刻所在位置。 高危区域则可以抽象为一个矩形区域(含边界),左下角和右上角的坐标分别为 (xl,yd) 和 (xr,yu),满足 xl<xr 且 yd<yu

    考虑某位居民的位置记录,如果其中某个坐标位于矩形内(含边界),则说明该居民经过高危区域; 进一步地,如果其中连续 k 个或更多坐标均位于矩形内(含边界),则认为该居民曾在高危区域逗留。 需要注意的是,判定经过和逗留时我们只关心位置记录中的 t 个坐标,而无需考虑该居民在 i 到 i+1 时刻之间位于何处。

    给定高危区域的范围和 n 位居民过去 t 个时刻的位置记录,试统计其中经过高危区域的人数和曾在高危区域逗留的人数。

输入格式
    从标准输入读入数据。

    输入共 n+1 行。

    第一行包含用空格分隔的七个整数 n、k、t、xl、yd、xr 和 yu,含义如上文所述。

    接下来 n 行,每行包含用空格分隔的 2t 个整数,按顺序表示一位居民过去 t 个时刻的位置记录 (x1,y1),(x2,y2),⋯,(xt,yt)。

输出格式
    输出到标准输出。

    输出共两行,每行一个整数,分别表示经过高危区域的人数和曾在高危区域逗留的人数。

样例1输入

5 2 6 20 40 100 80
100 80 100 80 100 80 100 80 100 80 100 80
60 50 60 46 60 42 60 38 60 34 60 30
10 60 14 62 18 66 22 74 26 86 30 100
90 31 94 35 98 39 102 43 106 47 110 51
0 20 4 20 8 20 12 20 16 20 20 20

样例1输出

3
2

样例1解释
    如下图红色标记所示,前三条位置记录经过了高危区域; 但第三条位置记录(图中左上曲线)只有一个时刻位于高危区域内,不满足逗留条件。

img
样例2输入

1 3 8 0 0 10 10
-1 -1 0 0 0 0 -1 -1 0 0 -1 -1 0 0 0 0

样例2输出

1
0

样例2解释
    该位置记录经过了高危区域,但最多只有连续两个时刻位于其中,不满足逗留条件。

子任务
    全部的测试点满足 1≤n≤20,1≤k≤t≤103,所有坐标均为整数且绝对值不超过 106

100分代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Reader {
    
    
	static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tokenizer = new StringTokenizer("");

	static String next() throws IOException {
    
    
		while (!tokenizer.hasMoreTokens()) {
    
    
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
    
    
		return reader.readLine();
	}

	static int nextInt() throws IOException {
    
    
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
    
    
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
    
    
		return Double.parseDouble(next());
	}
}

class Main {
    
    

	public static void main(String[] args) {
    
    
		try {
    
    
			int n = Reader.nextInt(), k = Reader.nextInt(), t = Reader.nextInt(), xl = Reader.nextInt(),
					yd = Reader.nextInt(), xr = Reader.nextInt(), yu = Reader.nextInt(), x, y, passNum = 0, stayNum = 0,
					count;
			boolean pass = false;
			boolean stay = false;
			boolean flag = false;
			for (int i = 0; i < n; i++) {
    
    
				count = 0;
				stay = false;
				pass = false;
				flag = false;
				for (int j = 0; j < t; j++) {
    
    
					x = Reader.nextInt();
					y = Reader.nextInt();
					if (xl <= x && x <= xr && yd <= y && y <= yu) {
    
    
						count++;
						pass = true;
						flag = true;
					}
					if (!flag) {
    
    
						count = 0;
					}
					flag = false;
					if (count == k) {
    
    
						stay = true;
					}
				}
				if (stay) {
    
    
					stayNum++;
				}
				if (pass) {
    
    
					passNum++;
				}
			}
			System.out.println(passNum);
			System.out.println(stayNum);
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/H_X_P_/article/details/108573897
今日推荐