abc344E - Insert or Erase以及关于std::list的使用

本文介绍了如何使用C++STL中的std::list容器进行序列的插入和删除操作,强调了其常数时间复杂度的优势,并讨论了std::list与std::vector的区别,以及splice方法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Insert or Erase

题目要求不断对一个序列进行增、删操作,容易想到学习数据结构时关于这个问题的讨论,显然使用链表是一个非常不错的选择。

作为modern cpp选手,我非常倾向于使用STL中的容器解决这个问题,于是顺路学习了下常用相关操作。

std::list is a container that supports constant time insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is usually implemented as a doubly-linked list.

—— cppreference

上面这段话引用自cppreference,大体就是说std::list 是一个容器,支持从容器中的任意位置定时插入和移除元素。不支持快速随机存取。它通常以双链表的形式实现。

这里我们并不讨论其实现细节,读者感兴趣可自行查看源码。

  • .insert().emplace():当你写下 .emplace(x,y),其中 x 是指向同一列表中某个元素的迭代器时,它的意思是在 x 之前创建一个与 y 相等的元素。请注意,迭代器 x 是必须的。时间复杂度严格为 O ( 1 ) O(1) O(1)
  • .erase():当你擦除 .erase(x),其中 x 是指向同一列表中元素的迭代器时,字面意思是删除它。时间复杂度严格为 O ( 1 ) O(1) O(1)
  • .splice():它连接两个列表,当你使用.splice(iterator, list)时,这意味着list列表将插入当前调用函数的列表的iterator位置。(这个功能很强大,但在本问题中没有用到)。

在本题中,由于在增删时,我们总是需要对迭代器进行操作,因此我们就需要对序列中的每个整数所对应位置的迭代器进行存储。

特别注意,std::list与std::vector不同的一点是,除非它的迭代器被删除,否则是不会破坏掉的(不会像std::vector那样因为序列的改变使得序列中元素位置和迭代器发生改变),这就是为什么我们能够保存下来不断使用。

因此本题时间复杂度: O ( N l o g N ) O(NlogN) O(NlogN) 注: O ( l o g N ) O(logN) O(logN)为使用std::map的复杂度。

本题代码:

#include <bits/stdc++.h>

using i64 = long long;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int N;
	std::cin >> N;
	std::vector<int> A(N);
	for (int i = 0; i < N; i++) {
		std::cin >> A[i];
	}

	std::map<int, std::list<int>::iterator> f;
	int Q;
	std::cin >> Q;

	std::list<int> ans;
	for (int i = 0; i < N; i++) {
		ans.emplace_back(A[i]);
		f[A[i]] = std::prev(ans.end());
	}

	for (int i = 0; i < Q; i++) {
		int o;
		std::cin >> o;

		if (o == 1) {
			int x, y;
			std::cin >> x >> y;

			auto it = std::next(f[x]);
			ans.emplace(it, y);

			f[y] = std::prev(it);
		} else {
			int x;
			std::cin >> x;

			ans.erase(f[x]);
		}
	}

	for (auto x : ans) {
		std::cout << x << " ";
	}

	return 0;
}
在C++中,`std::list` 是一个双向链表容器,`std::pair` 是一个模板类,可以存储一对值。为了实现一个顺序键值对容器,我们可以使用 `std::list<std::pair<Key, Value>>`。以下是实现 `insert`, `find`, `erase` 方法的示例代码: ```cpp #include <iostream> #include <list> #include <utility> template <typename Key, typename Value> class OrderedMap { private: std::list<std::pair<Key, Value>> container; public: // 插入键值对 void insert(const Key& key, const Value& value) { // 检查键是否已经存在 for (auto& pair : container) { if (pair.first == key) { pair.second = value; // 更新值 return; } } // 如果键不存在,则插入新的键值对 container.push_back(std::make_pair(key, value)); } // 查找键对应的值 Value* find(const Key& key) { for (auto& pair : container) { if (pair.first == key) { return &(pair.second); } } return nullptr; // 如果未找到,则返回nullptr } // 根据键删除键值对 void erase(const Key& key) { for (auto it = container.begin(); it != container.end(); ++it) { if (it->first == key) { container.erase(it); return; } } } // 打印所有键值对 void print() const { for (const auto& pair : container) { std::cout << pair.first << ": " << pair.second << std::endl; } } }; int main() { OrderedMap<int, std::string> map; map.insert(1, "One"); map.insert(2, "Two"); map.insert(3, "Three"); map.print(); map.insert(2, "Dos"); // 更新键为2的值 map.print(); auto value = map.find(2); if (value) { std::cout << "Found: " << *value << std::endl; } else { std::cout << "Not found" << std::endl; } map.erase(2); map.print(); return 0; } ``` ### 代码说明 1. **`insert` 方法**:检查键是否已经存在,如果存在则更新值;否则插入新的键值对。 2. **`find` 方法**:查找键对应的值,如果找到则返回指向值的指针,否则返回 `nullptr`。 3. **`erase` 方法**:根据键删除键值对。 4. **`print` 方法**:打印所有键值对。 ### 运行结果 ``` 1: One 2: Two 3: Three 1: One 2: Dos 3: Three Found: Dos 1: One 3: Three ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值