队列--洛谷P1540

这篇文章描述了一个C++程序,通过哈希表和队列的数据结构,实现了对单词查询的内存优化,当内存达到上限时会移除最早添加的单词,以减少外存访问次数。
#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int m,n=0;
	//接收m,n的输入
	cin>>m>>n;
	
	//用哈希表表示单词是否在内存中,1为存在,0为不存在
	int hash[1000]={0};
	
	//用队列表示内存
	queue<int> q;
	//查询外存次数
	int cnt=0;
	while(n>0)
	{
		//单词输入
		int word=0;
		cin>>word;
		if(hash[word]!=1)
		{
			q.push(word);
			hash[word]=1;
			cnt++;
			
			//内存超界处理
			if(q.size()>m)
			{
				hash[q.front()]=0;
				q.pop();
			}
		}
		n--;
	}
	cout<<cnt;
	
}

关于洛谷平台上的题目 P3363 队列操作题解,虽然没有直接提供该题的具体代码或解法描述,但可以基于队列操作的典型应用场景以及洛谷平台上其他类似题目的解法,提供一些通用的指导思路和实现方法。 ### 队列操作的常见场景 队列是一种先进先出(FIFO)的数据结构,常用于处理需要按顺序操作数据的问题。常见的操作包括入队(push)、出队(pop)、获取队首元素(front)等。在编程竞赛中,队列常用于模拟、广度优先搜索(BFS)、滑动窗口等问题。 ### 解题思路 对于 P3363 这类题目,通常需要根据题目要求实现一个队列,并处理一系列操作。例如,题目可能要求支持多种操作类型,如入队、出队、查询队列中某个特定条件的元素等。 #### 示例:使用 STL 实现队列操作 以下是一个简单的 C++ 示例,展示如何使用标准库中的 `queue` 实现基本的队列操作: ```cpp #include <iostream> #include <queue> using namespace std; int main() { queue<int> q; int n; cin >> n; for (int i = 0; i < n; i++) { string op; cin >> op; if (op == "push") { int x; cin >> x; q.push(x); } else if (op == "pop") { if (!q.empty()) { q.pop(); } } else if (op == "front") { if (!q.empty()) { cout << q.front() << endl; } else { cout << "empty" << endl; } } else if (op == "size") { cout << q.size() << endl; } } return 0; } ``` ### 手动实现队列 如果题目要求手动实现队列而不是使用 STL,可以通过数组或链表来模拟队列的行为。以下是一个使用数组实现的简单队列示例: ```cpp #include <iostream> #define MAX_SIZE 100005 using namespace std; int queue[MAX_SIZE]; int front = 0, rear = 0; void push(int x) { queue[rear++] = x; } void pop() { if (front != rear) { front++; } } int getFront() { if (front != rear) { return queue[front]; } else { return -1; // 表示队列为空 } } int getSize() { return rear - front; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string op; cin >> op; if (op == "push") { int x; cin >> x; push(x); } else if (op == "pop") { pop(); } else if (op == "front") { cout << getFront() << endl; } else if (op == "size") { cout << getSize() << endl; } } return 0; } ``` ### 单调队列的应用 在某些特定问题中,如滑动窗口最大值问题,单调队列是一个非常高效的工具。它可以在 O(n) 的时间复杂度内处理整个数组,从而快速找到每个窗口内的最大值。以下是一个使用 `deque` 实现的单调队列示例: ```cpp #include <iostream> #include <deque> #include <vector> using namespace std; void maxSlidingWindow(vector<int>& nums, int k) { deque<int> dq; for (int i = 0; i < nums.size(); i++) { // 移除不在当前窗口内的元素 while (!dq.empty() && dq.front() < i - k + 1) { dq.pop_front(); } // 移除比当前元素小的元素 while (!dq.empty() && nums[dq.back()] <= nums[i]) { dq.pop_back(); } dq.push_back(i); // 输出窗口最大值 if (i >= k - 1) { cout << nums[dq.front()] << " "; } } cout << endl; } int main() { vector<int> nums = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3; maxSlidingWindow(nums, k); return 0; } ``` ### 总结 通过上述示例,可以看出队列操作在编程竞赛中的重要性。无论是直接使用 STL 提供的 `queue` 或 `deque`,还是手动实现队列,都需要根据题目要求灵活选择合适的数据结构和算法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值