1. Question link: CCF 202009-2 Risk population screening
Question No.: | 202009-2 |
Question name: | Risk population screening |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | topic backgroundAfter the outbreak of an epidemic in a certain place, we would like to notify all residents who have recently passed through the high-risk area to participate in nucleic acid testing out of the principle of "all tests should be done". Problem DescriptionAnalyzing location records is a simple and effective way to find out residents who have passed through high-risk areas. Specifically, a resident's location record contains t plane coordinates (x1, y1), (x2, y2), ..., (xt, yt), where (xi, yi) represents the location of the resident at moment i. Consider the location record of a resident, if one of the coordinates is within the rectangle (including the boundary), it means that the resident has passed through the high-risk area; further, if k or more consecutive coordinates are located within the rectangle (including the boundary), It is considered that the resident has stayed in the high-risk area . It should be noted that we only care about the t coordinates in the location record when judging passing and staying , without considering where the resident is between time i and i+1. Given the range of high-risk areas and the location records of n residents at past t moments, try to count the number of people who have passed through high-risk areas and the number of people who have stayed in high-risk areas. input formatInput a total of n+1 lines. The first line contains the seven integers n, k, t, xl, yd, xr, and yu separated by spaces, with the meanings described above. The next n lines, each containing 2t integers separated by spaces, represent the location records (x1, y1), (x2, y2), ⋯, (xt, yt) of a resident in the past t moments in sequence. output formatThe output consists of two lines, and each line is an integer, respectively representing the number of people who have passed through the high-risk area and the number of people who have stayed in the high-risk area. Sample input 1Sample output 1Example 1 descriptionAs shown by the red mark in the figure below, the first three location records have passed through high-risk areas; Sample input 2Sample output 2Example 2 DescriptionThe location record has passed through a high-risk area, but it can only be in it for two consecutive moments at most, and the stay condition is not met. Evaluation use case scale and agreementAll test points satisfy 1≤n≤20, 1≤k≤t≤10 3 , all coordinates are integers and the absolute value does not exceed 10 6 . |
2. Problem analysis:
After reading the question analysis, the difficulty of this question lies in judging the continuous \color{red}\large{\bold{continuous}}Consecutive k or more coordinates are located within the rectangle (including the boundary), that is,whether there is a stay in a high-risk area. What I use here is to directly count the number of coordinates passing through the high-risk area through input stream processing, then iterate the maximum value, and finally judge whether a resident is staying. It should be noted that prev is initialized to time t to ensure that statistics will not be performed for the first time, and it will always be assigned the latest coordinate time passing through the high-risk area. The cnt variable indicates the number of coordinates that have continuously passed through the high-risk area.
3. C++ language program implementation:
#include <bits/stdc++.h>
using namespace std;
bool in_danger_area(int &xl,int &yd,int &xr,int &yu,int x,int y)
{
if (x>=xl&&x<=xr&&y>=yd&&y<=yu)
{
return true;
}
else
{
return false;
}
}
int main()
{
int n,k,t,xl,yd,xr,yu,xt,yt,passed=0,leaved=0,cnt,prev,adjacent_max;
scanf("%d%d%d%d%d%d%d",&n,&k,&t,&xl,&yd,&xr,&yu);
for (int i=0; i<n ; ++i )
{
bool is_in=false;
prev=t;
cnt=1,adjacent_max=0;
for (int j=0; j<t ; ++j )
{
scanf("%d%d",&xt,&yt);
if (in_danger_area(xl,yd,xr,yu,xt,yt))
{
if (j-prev==1)
{
adjacent_max=max(++cnt,adjacent_max);
}
else
{
cnt=1;
}
prev=j;
is_in=true;
}
}
if (is_in)
{
++passed;
}
if (adjacent_max>=k)
{
++leaved;
}
}
printf("%d\n%d",passed,leaved);
return 0;
}