202209-3 CCF Epidemic Prevention Big Data Full Score Problem Solution (Super Detailed Explanation + Note Code) + Problem Solving Ideas (STL Simulation)

Problem Description

insert image description here

problem solving ideas

First of all, the meaning of the question is to give the roaming information of n days and the list of risk areas of n days
to find the risk population of n days

According to the meaning of the question, the roaming information must be stored. It is more appropriate to use a structure array.
When judging whether the user is a risk group, it is necessary to judge whether the location r in the interval [d1, d] is a risk area, so the risk of the location r needs to be To store the start and end time, you can use map combined with pair
and open an array to store the answers of daily risk groups, and finally sort and deduplicate the output. The following
is my data structure, which can be referred to. There are other definition forms
insert image description here
and then it is read into each day The roaming information and risk areas
of the day can be obtained on the day of the risk population.
First, after obtaining the risk area, first update the risk time period of each area.
Confirm that the location r is a risk area on day d, then the current risk termination time of r will be updated. Into d + 6 (in the next 7 days)
if r was not a risk area before or d - 1 day is no longer a risk area, then the risk start time of r needs to be updated to d, otherwise it will not be updated
insert image description here

After knowing the risk time period of the region, you can judge whether the user is a risk group according to the conditions given in the title. The
insert image description here
corresponding code
insert image description here
finally outputs the answers in turn every day, and sorts the numbers before outputting to remove duplicates. The reason is that there is duplicate data and multi-site access data in roaming data
insert image description here


Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <queue>

using namespace std;

struct node //存储漫游信息
{
    
    
    int day;
    int user;
    int address;
};

vector <node> alls[1010]; //存储每一天的有用的漫游信息
vector <int> res[1010]; //存储每一天的风险人群
unordered_map <int, pair <int, int>> A; //存储地点r的风险时间段
int n, r, m;

int main()
{
    
    
    scanf("%d", &n);

    for (int d = 0; d < n; d ++)
    {
    
    
        scanf("%d%d", &r, &m);
        for (int i = 0; i < r; i ++)
        {
    
    
            int x;
            scanf("%d", &x); //地点x在d天被确认为风险地区
            //如果x之前不是风险地区或者d-1天时x已经不是风险地区了
            if (A.count(x) == 0 || d - A[x].second > 1) A[x].first = d; //更新风险时间段的起点
            
            //更新风险时间的终点为 第 d + 6天
            A[x].second = d + 6;
        }

        for (int i = 0; i < m; i ++)
        {
    
    
            int d1, u, a;
            scanf("%d%d%d", &d1, &u, &a);
            //如果这天漫游信息中的地点不是风险地区或者当天已经不是风险地区或者是七天前的无效信息,则不需要保存
            if (A.count(a) == 0 || A[a].second < d || d1 < d - 6) continue;
            node t = {
    
    d1, u, a};
            alls[d].push_back(t); //存入当天的漫游信息
        }
        
        //处理[d-6, d]天中收到的有效漫游信息,得到风险人群
        for (int i = max(0, d - 6); i <= d; i ++)
        {
    
    
            for (int c = 0; c < alls[i].size(); c ++) //遍历第i天收到的漫游信息
            {
    
    
                int d1 = alls[i][c].day, u = alls[i][c].user, a = alls[i][c].address;
                //当前这个地区是风险地区
                //并且漫游信息是七天之内收到的漫游信息
                //并且[d1, d]这个时间段这个地区都是风险地区
                //则将这个人列入危险人群
                if (A.count(a) && d1 >= d - 6 && d1 >= A[a].first && d <= A[a].second) res[d].push_back(u);
            }
        }
    }

    for (int i = 0; i < n; i ++)
    {
    
    
        sort(res[i].begin(), res[i].end()); //排序
        //处理重复数据和多地点数据,可能一个人被列入多次
        res[i].erase(unique(res[i].begin(), res[i].end()), res[i].end());  //去重
        
        printf("%d", i);
        for (int j = 0; j < res[i].size(); j ++) printf(" %d", res[i][j]); //输出
        printf("\n");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_51800570/article/details/129640133