−
7
,
d
]
d_1 \in (d-7,d]
d1∈(d−7,d],并且
- 对于任意的
D
∈
[
d
1
,
d
]
D \in [d_1,d]
D∈[d1,d],地区 r 在 D 日处于风险状态。
所以就是要遍历d日的前6天的到访数据,然后判断该地是不是风险地区在那个区间上。
遇到的问题(学到的东西)
这题随便一写什么都不用考虑,只要模拟出来了基本都是40分,然后我一开始就是40分,然后怎么也找不到bug,然后逐渐找到了几个bug但是还是40分,最终参考别人的代码还是找到了bug,考场上要是找不到只能认栽了。
- 风险地区的风险区间合并问题
这个问题我一开始,没有注意到,就是要把风险区间有交集的进行合并,但是这个合并的逻辑有了一点点问题,下面会提到
- 风险地区的风险区间合并问题的实现
实现的过程中是后一个插入前一个的,但是会导致最后一个没有插入
修改之前
// 合并一下区间 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); }
- 判断是不是风险用户的问题
这点一开始是自己找的规律然后按着自己想到去判断的,没想到的少了一些条件,然后补上了,但是仍然是40分,之后百思不得其解
- 区间合并的逻辑问题
原来这个区间合并,不只是有交集的区间合并,就算是没有交集但是中间没有间隔的区间也要合并的,这点我是看了别人的代码才想到的,考试要是碰见这个直接就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;
}
}
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**





**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)**

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
12995809195)]
[外链图片转存中...(img-gyqGiuVl-1712995809195)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)**
[外链图片转存中...(img-xqSr3z1z-1712995809195)]
**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
1666

被折叠的 条评论
为什么被折叠?



