原题目链接:
P2058 [NOIP2016 普及组] 海港 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
原题目截图:
思路分析:
这个题有一个难点,就是读题很难,这句话是重点:
计算出以每一艘船到达时间为止的 24小时(2424 小时 =86400=86400 秒)内所有乘船到达的乘客来自多少个不同的国家。
我这里画个图来阐述:
我一开始的思路也很简单:
思路一:
先用数组按顺序存储好每条船的信息。
对于每一条船,寻找它的前24h内(即前86400秒)的所有满足条件的船只,然后C++的STL集合(unordered_set)添加国籍,因为集合会自动去重,所以最后返回集合的长度,就是这一条船到达时间为止的 24小时(2424 小时 =86400=86400 秒)内所有乘船到达的乘客来自多少个不同的国家的数目。
当然这里也不需要真正寻找每条船的前24h内(即前86400秒)的所有满足条件的船,只要用一个first标记24小时内的首条船的序号即可。
不过很可惜,部分案例超时。
解决代码:
//超时,只能过70%案例
#include<iostream>
#include<vector>
#include<unordered_set>
#include<algorithm>
using namespace std;
struct boat {
int arrive_time; // 船只到达时间
vector<int> nations; // 船只携带的国家列表
};
vector<boat> boat_info; // 存储所有船只信息
// 计算指定范围内船只携带的国家数量
int func(int first, int end, const vector<boat>& boat_info, unordered_set<int>& nation_set) {
for (int i = first; i <= end; i++) {
for (int nation : boat_info[i].nations) {
nation_set.insert(nation); // 将国家插入集合中
}
}
return (int)nation_set.size(); // 返回集合中元素的数量,即不同国家的数量
}
int main() {
int n;
cin >> n; // 读取船只数量
for (int i = 0; i < n; i++) {
int ti, ki;
cin >> ti >> ki; // 读取船只到达时间和携带的国家数量
vector<int> vec;
for (int j = 0; j < ki; j++) {
int x;
cin >> x; // 读取每个国家
vec.push_back(x);
}
boat_info.push_back({ ti, vec }); // 将船只信息添加到列表中
}
int first = 0; // 初始化首个船只序号
for (int i = 0; i < boat_info.size(); i++) {
// 搜寻bo_i前24小时的首个船只序号
while (first < boat_info.size() && boat_info[first].arrive_time <= boat_info[i].arrive_time - 86400) {
first++;
}
unordered_set<int> nation_set; // 使用集合存储国家,自动去重
cout << func(first, i, boat_info, nation_set) << endl; // 计算并输出不同国家的数量
}
return 0;
}
思路二:
改进,用二分查找法来搜寻first。这样可以降低时间复杂度,不过还是很可惜,或许是因为这道题的数据过大,依旧部分案例过不了。
解决代码:
//使用二分法来查找首个在24h内的船只
//很可惜依然超时,只能过70%案例
#include<iostream>
#include<vector>
#include<unordered_set>
#include<algorithm>
using namespace std;
struct boat {
int arrive_time; // 船只到达时间
vector<int> nations; // 船只携带的国家列表
};
vector<boat> boat_info; // 存储所有船只信息
// 计算指定范围内船只携带的国家数量
int func(int first, int end, const vector<boat>& boat_info, unordered_set<int>& nation_set) {
for (int i = first; i <= end; i++) {
for (int nation : boat_info[i].nations) {
nation_set.insert(nation); // 将国家插入集合中
}
}
return (int)nation_set.size(); // 返回集合中元素的数量,即不同国家的数量
}
int binsearch(vector<boat>& boat_info, int start,int end) {
int left = start, right = end;
while (left <= right) {
int mid = left + (right - left) / 2;
// 检查 mid 船只到达时间是否在 end 船只到达时间的前 24 小时内
if (boat_info[mid].arrive_time <= boat_info[end].arrive_time - 86400) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return left;
}
int main() {
int n;
cin >> n; // 读取船只数量
for (int i = 0; i < n; i++) {
int ti, ki;
cin >> ti >> ki; // 读取船只到达时间和携带的国家数量
vector<int> vec;
for (int j = 0; j < ki; j++) {
int x;
cin >> x; // 读取每个国家
vec.push_back(x);
}
boat_info.push_back({ ti, vec }); // 将船只信息添加到列表中
}
int first = 0; // 初始化首个船只序号
for (int i = 0; i < boat_info.size(); i++) {
// 搜寻 boat_i 前 24 小时的首个船只序号
first = binsearch(boat_info, first,i);
unordered_set<int> nation_set; // 使用集合存储国家,自动去重
cout << func(first, i, boat_info, nation_set) << endl; // 计算并输出不同国家的数量
}
return 0;
}
思路三:最终解决法
优先级队列+哈希表+模拟过程
最终方法的思路和前面两种方法实际上有挺大区别。
前两种方法的关注点都在于:
找到符合条件的船,然后再算国籍。
最终方法则相反:
模拟过程,找到符合条件的人,然后算国籍。
具体是什么意思呢?
对于每一个人,我们知道他携带两个信息:到达时间和国籍。
很好,现在假如你是城管大队长。
每一次船到来时,假设之前到的人都站岸边等着你来(这群人里面有满足条件的,即在24h内的人;也有不满足条件的,即不在24h内的人)。
然后我们现在就要统计国籍了,首先把不满足条件的人赶走,然后记录满足条件的人的国籍。
这个国籍怎么记录呢?你的手下告诉你:当然是拿纸笔记录啦!
假如某条船到来的24h内有以下情况:
你是个聪明的城管大队长,你突然发现一个问题:
你不可能每一次来船都再做一个表,因为那样太浪费了!
那么在统计国籍时候,把不满足条件的人赶走时,你要把他们在表上的信息删除掉!
然后记录余下满足条件的人的国籍。
那么现在来看一看代码吧!
解决代码:
#include <iostream> // 包含标准输入输出流库
#include <vector> // 包含向量库
#include <queue> // 包含队列库
#include <unordered_map> // 包含哈希表库
#include<unordered_set>
using namespace std;
// 定义船只结构体
struct boat {
int arrive_time; // 船只到达时间
vector<int> peoples; // 船只携带的人
};
vector<boat> boat_info; // 存储所有船只信息
int main() {
int n;
cin >> n; // 读取船只数量
for (int i = 0; i < n; i++) {
int ti, ki;
cin >> ti >> ki; // 读取船只到达时间和携带的国家数量
vector<int> vec;
for (int j = 0; j < ki; j++) {
int x;
cin >> x; // 读取每个国家
vec.push_back(x);
}
boat_info.push_back({ ti, vec }); // 将船只信息添加到列表中
}
// 使用优先队列(最小堆)维护人
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> people_queue;
unordered_map<int, int> nation_count; // 使用哈希表存储国家数量
unordered_set<int>nation_set;
int ans = 0; // 初始化答案
for (int i = 0; i < n; i++) {
// 检查并更新队列中的人
while (!people_queue.empty() && people_queue.top().first <= boat_info[i].arrive_time - 86400) {//不在24H内到达的人
int nation = people_queue.top().second;
people_queue.pop(); //优先级队列每次弹出到达时间最小的人
nation_count[nation]--;
if (nation_count[nation] == 0) ans--;
}
// 更新当前国籍数量
for (int nation : boat_info[i].peoples) {
if (nation_count[nation] == 0) ans++;//还没记录过的新国籍
nation_count[nation]++;//标记记录了
nation_set.insert(nation);
people_queue.push({ boat_info[i].arrive_time, nation });//加入优先级队列
}
cout << ans<< endl; // 输出当前答案
}
return 0;
}
尾声:
今天这道题做得还挺久的,一开始没有想到模拟的思路,卡在几个数据案例始终过不去。不过好在皇天不负有心人,终于做出来了。
凡事坚持,终有所获。
今天的博客就写到这里了,愿诸君共勉之!