【CSP】2024–09-3 防疫大数据 100分 STL大模拟 使用map优化索引 有坑得注意

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

d

]

D \in [d_1,d]

D∈[d1​,d],地区 r 在 D 日处于风险状态。

所以就是要遍历d日的前6天的到访数据,然后判断该地是不是风险地区在那个区间上。

遇到的问题(学到的东西)

这题随便一写什么都不用考虑,只要模拟出来了基本都是40分,然后我一开始就是40分,然后怎么也找不到bug,然后逐渐找到了几个bug但是还是40分,最终参考别人的代码还是找到了bug,考场上要是找不到只能认栽了。

  1. 风险地区的风险区间合并问题

这个问题我一开始,没有注意到,就是要把风险区间有交集的进行合并,但是这个合并的逻辑有了一点点问题,下面会提到

  1. 风险地区的风险区间合并问题的实现

实现的过程中是后一个插入前一个的,但是会导致最后一个没有插入

修改之前

    // 合并一下区间
    for (auto item : place_time)
    {
        struct places temp;
        temp.length = -1; // 标记为还没初始化
        for (auto p : item.second)
        {
            if (temp.length != -1 && p.day >= temp.day && p.day <= temp.day + temp.length)
            {
                int end1 = p.day + p.length - 1;
                int end2 = temp.day + temp.length - 1;
                temp.length += end1 - end2;
            }
            else
            {
                place_time2[item.first].push_back(temp);
                temp = p;
            }
        }
    }

并且会报错,因为没有初始化的temp也会被插入

修改后的

    // 合并一下区间
    for (auto item : place_time)
    {
        struct places temp;
        temp.length = -1; // 标记为还没初始化
        for (auto p : item.second)
        {
            if (temp.length != -1 && p.day >= temp.day && p.day <= temp.day + temp.length)
            {
                int end1 = p.day + p.length - 1;
                int end2 = temp.day + temp.length - 1;
                temp.length += end1 - end2;
            }
            else
            {
                if (temp.length != -1)
                    place_time2[item.first].push_back(temp);
                temp = p;
            }
        }
        if (temp.length != -1)
            place_time2[item.first].push_back(temp);
    }

  1. 判断是不是风险用户的问题

这点一开始是自己找的规律然后按着自己想到去判断的,没想到的少了一些条件,然后补上了,但是仍然是40分,之后百思不得其解

  1. 区间合并的逻辑问题

原来这个区间合并,不只是有交集的区间合并,就算是没有交集但是中间没有间隔的区间也要合并的,这点我是看了别人的代码才想到的,考试要是碰见这个直接就g了

修改之前

    // 合并一下区间
    for (auto item : place_time)
    {
        struct places temp;
        temp.length = -1; // 标记为还没初始化
        for (auto p : item.second)
        {
            if (temp.length != -1 && p.day >= temp.day && p.day <= temp.day + temp.length-1)
            {
                int end1 = p.day + p.length - 1;
                int end2 = temp.day + temp.length - 1;
                temp.length += end1 - end2;
            }
            else
            {
                if (temp.length != -1)
                    place_time2[item.first].push_back(temp);
                temp = p;
            }
        }
        if (temp.length != -1)
            place_time2[item.first].push_back(temp);
    }

修改之后

    // 合并一下区间
    for (auto item : place_time)
    {
        struct places temp;
        temp.length = -1; // 标记为还没初始化
        for (auto p : item.second)
        {
            if (temp.length != -1 && p.day >= temp.day && p.day <= temp.day + temp.length)
            {
                int end1 = p.day + p.length - 1;
                int end2 = temp.day + temp.length - 1;
                temp.length += end1 - end2;
            }
            else
            {
                if (temp.length != -1)
                    place_time2[item.first].push_back(temp);
                temp = p;
            }
        }
        if (temp.length != -1)
            place_time2[item.first].push_back(temp);
    }

就是这个

            if (temp.length != -1 && p.day >= temp.day && p.day <= temp.day + temp.length)

改变了

之后就可以拿到了100分,

在这里插入图片描述
5. 还学到了使用freopen()

如果你的csp考场上 无法往终端里复制数据,可以直接使用

freopen("1.txt", "r", stdin);

注意1.txt是相对路径,可以替换成你的输入的路径,然后其他什么都不用改变,就可以了。

感悟

这题竟然是没有一次做出来,而且还是看了别人的思路才找出来问题的。中间其实改了很多次,就是没有改到点上,以后联系的时候,如果不是真错了,就不改,或者要保留原版的,不然没办法真正的找到错误。

如果考试中有这中情况,得把每个实现的点都列出来,然后逐个分析,不能一带而过,想一想真的是这样的吗?尤其是写if的时候的。而且不能只盯着一个点不看别的,浪费时间又找不到错误。

完整代码

#include <bits/stdc++.h>
using namespace std;
int n;
struct dayget
{
    int u;
    int day; // 到达的一天
};
struct places
{
    int id;     // 地方的id
    int day;    // 风险的开始日期
    int length; // 风险的长度
};
unordered_map<int, unordered_map<int, vector<dayget>>> arrive; // arrive[i][j]第i天收到j地到的用户的集合
unordered_map<int, vector<places>> place_time;                 // place_time[i] id为i的地方的风险时间片段的集合
unordered_map<int, vector<places>> place_time2;                // place_time[i] id为i的地方的风险时间片段的集合
unordered_set<int> risks[1010];                                // 存储每天的风险用户
bool searchIsDanger(int d1, int d, vector<places> dage)
{
    for (int i = 0; i < dage.size(); i++)
    {
        if (dage[i].day <= d1 && dage[i].day + dage[i].length - 1 >= d)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    // freopen("1.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int r, m;
        cin >> r >> m;
        for (int j = 0; j < r; j++)
        {
            int p;
            cin >> p;
            struct places t = {p, i, 7};
            place_time[p].push_back(t);
        }
        for (int j = 0; j < m; j++)
        {
            int d, u, r1;
            cin >> d >> u >> r1;
            struct dayget t = {u, d};
            if (d < i - 6) // 如果收到收到消息的是7天之前的就不要了
                continue;
            arrive[i][r1].push_back(t);
        }
    }
    // 合并一下区间
    for (auto item : place_time)
    {
        struct places temp;
        temp.length = -1; // 标记为还没初始化
        for (auto p : item.second)
        {
            if (temp.length != -1 && p.day >= temp.day && p.day <= temp.day + temp.length)
            {
                int end1 = p.day + p.length - 1;
                int end2 = temp.day + temp.length - 1;
                temp.length += end1 - end2;
            }
            else
            {
                if (temp.length != -1)
                    place_time2[item.first].push_back(temp);
                temp = p;
            }
        }
        if (temp.length != -1)
            place_time2[item.first].push_back(temp);
    }

    for (int d = 0; d < n; d++)
    {
        for (int i = max(0, d - 6); i <= d; i++)
        {
            for (auto c : arrive[i]) // 分析第i天到访的所有地方
            {
                int r = c.first;
                for (auto user : c.second) // 到访r地的所有的人 然后判断是不是该设定为风险人员
                {
                    int d1 = user.day;
                    if (d1 >= d - 6 && searchIsDanger(d1, d, place_time2[r]))
                    {
                        risks[d].insert(user.u);
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i << ' ';
        vector<int> risk1(risks[i].begin(), risks[i].end());
        sort(risk1.begin(), risk1.end());
        for (auto item : risk1)
        {
            cout << item << ' ';
        }
        cout << endl;
    }
}

第二遍写(精简版代码)

#include <bits/stdc++.h>
using namespace std;
struct people
{
    int d,u,r;
};
struct place
{
    map<int , int> range;
};
int n;
map<int, place> p; // i号地区结束风险的时间
map<int, vector<struct people>> all;
set<int> res[1001];
int main()
{
//    freopen("1.txt", "r", stdin);
    cin >> n;

    for(int i = 0; i < n; i++)
    {
        int r, m;
        cin >> r >> m;
        for(int j = 0; j < r; j++)
        {
            int temp;
            cin >> temp;
            for(int k = i; k <= i + 6 ; k++)
            {
                p[temp].range[k] = true;
            }
        }

        for(int j = 0; j < m; j++)
        {
            struct people t;
            cin >> t.d >> t.u >> t.r;
            all[i].push_back(t);


![img](https://img-blog.csdnimg.cn/img_convert/92d3ba02aacad2a663f839f20b4ebd68.png)
![img](https://img-blog.csdnimg.cn/img_convert/7bd29406beced69a0dcf4a92049cdf0b.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

 t;
            cin >> t.d >> t.u >> t.r;
            all[i].push_back(t);


[外链图片转存中...(img-8Qi36l8h-1715695380656)]
[外链图片转存中...(img-E0bnqT8M-1715695380656)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值