noi-2026年1月07号作业

**题目:**B3616 【模板】队列

网址: https://www.luogu.com.cn/problem/B3616

**思路:**定义一个queue,然后按照题目的意思模拟一遍就行。

**知识点:**queue

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
const int maxn=1e2+100;

int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int n;
	cin>>n;
	queue<int>q;
	while(n--)
	{
	   int op;
	   cin>>op;
	   if(op==1)
	   {
	      int x;
		  cin>>x;
		  q.push(x);	
	   }else if(op==2)
	   {
	      if(q.size()==0)
		  cout<<"ERR_CANNOT_POP"<<'\n';
		  else q.pop();	
	   }else if(op==3)
	   {
	      if(q.size()==0)
		  cout<<"ERR_CANNOT_QUERY"<<'\n';
		  else cout<<q.front()<<'\n';	
	   }else cout<<q.size()<<'\n';	
	} 
	
	return 0; 
}

**题目:**P1540 [NOIP 2010 提高组] 机器翻译

网址: https://www.luogu.com.cn/problem/P1540

**思路:**我们定义一个queue来模拟入内存、出内存的操作,同时使用vis来标记数是否在内存里面(也就是是否在队列里面)

**知识点:**queue

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
const int maxn=1e4+100;

int vis[maxn];
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	queue<int>q;
	int m,n;
	cin>>m>>n;
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		if(vis[x])
		continue;
		if(q.size()+1<=m)
		{
			q.push(x);
			vis[x]=1;
			ans++;
		}else{
			vis[q.front()]=0;
			q.pop();
			q.push(x);
			vis[x]=1;
			ans++;
		}
	}
	cout<<ans;
	
	return 0; 
}

**题目:**P2952 [USACO09OPEN] Cow Line S

网址: https://www.luogu.com.cn/problem/P2952

**思路:**定义一个双端队列,这个是双端队列模板题

**知识点:**deque

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
const int maxn=1e4+100;

int vis[maxn];
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int s;
	cin>>s;
	int now=1;
	deque<int>q;
	while(s--)
	{
		char op;
		cin>>op;
		if(op=='A')
		{
			char dir;
			cin>>dir;
			if(dir=='L')
			q.push_front(now);
			else q.push_back(now);
			now++; 
		}else if(op=='D')
		{
			char dir;
			cin>>dir;
			int cnt;
			cin>>cnt;
			if(dir=='L')
			{
				while(cnt--)
				q.pop_front();

			}else{
				while(cnt--)
				q.pop_back();
			}
		}
	}
	while(q.size())
	{
		cout<<q.front()<<'\n';
		q.pop_front();
	}
	return 0; 
}

**题目:**P5661 [CSP-J 2019] 公交换乘

网址: https://www.luogu.com.cn/problem/P5661

**思路:**我们定义一个deque来存储优惠劵,然后再按照题目的意思进行模拟,因为每个乘车的时间点都不一样,所以我们只需要在暴力枚举的时候最多找45个。

**知识点:**deque,简单思维

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
const int maxn=1e4+100;

int vis[maxn];
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int n;
	cin>>n;
	deque<pll>q;
	ll ans=0;
	while(n--)
	{
		int op,ti;
		ll price;
		cin>>op>>price>>ti;
		if(op==0)
		{
			q.push_back({ti,price});
			ans+=price;
		}else{
			while(q.size()&&q.front().fi+45<ti)
			q.pop_front();
			int sz=min(45,(int)q.size());
			deque<pll>res;
			bool ok=false;
			while(sz--)
			{
				if(q.front().se>=price)
				{
					ok=true;
					q.pop_front();
					break;
				}else{
					res.push_back(q.front());
					q.pop_front();
				}
			}
			while(res.size())
			{
				q.push_front(res.back());
				res.pop_back();
			}
			if(!ok)
			ans+=price; 
		}
	}
	cout<<ans;
	
	return 0; 
}

**题目:**P2058 [NOIP 2016 普及组] 海港

网址: https://www.luogu.com.cn/problem/P2058

**思路:**我们定义一个deque来存储每只船的id,同时定义t数组、k数组、e数组来分别存放每只船到达的时间、乘客数、乘客的国籍。我们再定义一个cnt数组表示每个国籍现有的乘客数。

**知识点:**deque,简单思维

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int maxn=2e5+100;

int n;
vector<int>e[maxn];
int cnt[maxn];
int ans;
int t[maxn],k[maxn];
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	cin>>n;
	deque<int>q;
	for(int i=1;i<=n;i++)
	{
		cin>>t[i]>>k[i];
		q.push_back(i);
		for(int j=1;j<=k[i];j++)
		{
			int x;
			cin>>x;
			e[i].eb(x);
			if(cnt[x]==0)
			ans++;
			cnt[x]++;
		}
		while(q.size()&&t[q.front()]<=t[i]-86400)
		{
			int id=q.front();
			for(auto it:e[id])
			{
				cnt[it]--;
				if(cnt[it]==0)
				ans--;
			}
			q.pop_front();
		}
		cout<<ans<<'\n';
	}
	
	return 0; 
}

**题目:**P11138 [APC001] C - Not APC

网址: https://www.luogu.com.cn/problem/P11138

**思路:**对于每一个字符,我们从后往前判断是否可以删除。

**知识点:**deque,思维题

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int maxn=3e6+100;

int n;
deque<int>q[3];
int vis[maxn];
struct node{
	int a[3];
};
void solve()
{
	string str;
	cin>>str;
	int len=str.length();
	for(int i=0;i<=2;i++)
	{
		while(q[i].size())
		q[i].pop_front();
	}
	for(int i=0;i<len;i++)
	{
		int id=0;
		vis[i]=0;
		if(str[i]=='P')
		id=1;
		else if(str[i]=='C')
		id=2;
		q[id].push_back(i);
	}
	vector<node>ans;
	
	while(true)
	{
		bool ok=false;
		for(int i=0;i<3;i++)
		{
			if(q[i].size()==0)
			{
				ok=true;
				break;
			}
		}
		if(ok)
		break;
		for(int i=1;i>=0;i--)
		{
			if(q[i].front()>q[i+1].back())
			{
				ok=true;
				break;
			}
			while(q[i].size()>=1&&(q[i].back()>q[i+1].back()))
			q[i].pop_back();
			if(q[i].size()==0)
			{
				ok=true;
				break;
			}
			
		}
		if(ok)
		break;
		node nd;
		for(int i=0;i<=2;i++)
		{
			vis[q[i].back()]=1;
			nd.a[i]=q[i].back();
			q[i].pop_back();
		}
		ans.eb(nd);
	}
	string res="";
	for(int i=0;i<len;i++)
	{
		if(!vis[i])
		res+=str[i];
	}
	if(res.length()==0)
	res="Perfect";
	cout<<res<<'\n';
	cout<<ans.size()<<'\n';
	for(int i=0;i<ans.size();i++)
	{
		cout<<ans[i].a[0]+1<<" "<<ans[i].a[1]+1<<" "<<ans[i].a[2]+1<<'\n';
	}
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T;
	cin>>T;
	while(T--)
	solve();
	return 0; 
}

**题目:**P9422 [蓝桥杯 2023 国 B] 合并数列

网址: https://www.luogu.com.cn/problem/P9422

**思路:**因为两个数组的和相等,所以一定可以使得两个数组相等,最差的情况就是两个数组都全不合并。因为合并的时候不影响数字的顺序,所以只能从头开始一个个合并。

知识点:deque,简单思维

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int maxn=2e5+100;


int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int n,m,ans=0;
	cin>>n>>m;
	deque<ll>q1,q2;
	for(int i=1;i<=n;i++)
	{
		ll x;
		cin>>x;
		q1.push_back(x);
	} 
	for(int i=1;i<=m;i++)
	{
		ll x;
		cin>>x;
		q2.push_back(x);
	}
	while(q1.size())
	{
		if(q1.front()==q2.front())
		{
			q1.pop_front();
			q2.pop_front();
		}else if(q1.front()>q2.front())
		{
			q2[1]+=q2[0];
			q2.pop_front();
			ans++;
		}else{
			q1[1]+=q1[0];
			q1.pop_front();
			ans++;
		}
	}
	cout<<ans;
	return 0; 
}
相关推荐
m0_635647481 天前
Qt使用第三方组件库新手教程(一)
开发语言·c++·qt
l1t1 天前
DeepSeek辅助编写的利用唯一可选数求解数独SQL
数据库·sql·算法·postgresql
星火开发设计1 天前
二叉树详解及C++实现
java·数据结构·c++·学习·二叉树·知识·期末考试
WJSKad12351 天前
传送带物体检测识别_基于YOLO11与RGCSPELAN改进算法_工业视觉检测系统
人工智能·算法·视觉检测
CSDN_RTKLIB1 天前
C++取余符号%
开发语言·c++
仍然.1 天前
JavaDataStructure---排序
数据结构·算法·排序算法
ZhuNian的学习乐园1 天前
LLM知识检索增强:RAG_系统化解析与工程实践
人工智能·算法
WBluuue1 天前
Codeforces Good Bye 2025 Div1+2(ABCDE)
c++·算法
骑自行车的码农1 天前
🕹️ 设计一个 React 重试
前端·算法·react.js