跳到正文

个人记录

队列进阶

#SUSPENDED#约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年7月6日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 9 分钟

版本

  1. v1当前

课堂笔记


笔记

课堂题目

T627216 周末舞会

思路:用手写队列模拟,q存入1-n,l在存入1-m,将它们一一匹配,每输一个,将那个数存到后面,再将前面的删除,就完成了。

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int q[10005],qh=0,qt=-1,l[10005],lh=0,lt=-1;
int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		q[++qt]=i;
	}
	for(int i=1;i<=m;i++)
	{
		l[++lt]=i;
	}
	for(int i=1;i<=k;i++)
	{
		cout<<q[qh]<<" "<<l[lh]<<"\n";
		qt++;
		q[qt]=q[qh];
		qh++;
		lt++;
		l[lt]=l[lh];
		lh++;
	} 
	return 0;
}

T627222 围圈报数

思路:用手写队列模拟,q存入1-n,将那个数存到后面这样报k次数,在输出头,这样重复n次,就完成了。

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,k,h=0,t=0;
int q[100005];
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		q[t++]=i;
	}
	for(int j=1;j<=n;j++)
	{
		for(int i=1;i<k;i++)
		{
			q[t++]=q[h];
			h++;
		}
		cout<<q[h]<<" ";
        h++;
	}
	return 0;
}

P1090 [NOIP 2004 提高组] 合并果子

思路:用优先队列,先加入a[1]-a[n],在合并n-1次,每次将两队一合,用cut计数,队列中再加入新的一堆,最终输出cut。

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int n,a[10005],cut=0;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		q.push(a[i]);
	}
	for(int i=1;i<=n-1;i++)
	{
		int x=q.top();
		q.pop();
		cut+=q.top()+x;
		q.push(q.top()+x);
		q.pop();
		
	}
	cout<<cut;
	return 0;
}

P5724 【深基4.习5】求极差 / 最大跨度值 / 最大值和最小值的差

思路:用优先小的队列和用优先大的队列存入n个数在输出两个对头相差。

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
priority_queue<int>q;
priority_queue<int,vector<int>,greater<int> > p; 
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int n,x,a,b;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		q.push(x);
		p.push(x);
	}
	a=q.top();
	b=p.top();
	cout<<a-b;
	return 0;
}

P2952 [USACO09OPEN] Cow Line S

思路:用双调队列,先输入操作,再根据操作在队列中进行操作,最终输出队列。

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
deque<int>q;
int main()
{
	int n,x=1;
	string a;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		char op1, op2;
		int xx;
		cin>>op1>>op2; 
    if(op1=='A'&&op2=='L')q.push_front(x),x++;
		if(op1=='A'&&op2=='R')q.push_back(x),x++;
		if(op1=='D'&&op2=='L')
		{
			cin>>xx;
			while(xx--)q.pop_front();
		}
		if(op1=='D'&&op2=='R')
		{
			cin>>xx;
			while(xx--)q.pop_back();
		}
	}
	while(q.size())
	{
		cout<<q.front()<<"\n";
		q.pop_front();
	}
	return 0;
}

作业:

B3616 【模板】队列

代码:

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
queue<ll>q;
int main()
{
 	ll n,c,d;
 	cin>>n;
 	while(n--)
 	{
 		cin>>c;
 		if(c==1)
 		{
 			cin>>d;
 			q.push(d);
		}
		else if(c==2)
		{
			if(q.size()==0)
			{
				cout<<"ERR_CANNOT_POP\n";
			}
			else
			{
				q.pop();
			}
		}
		else if(c==3)
		{
			if(q.size()==0)
			{
				cout<<"ERR_CANNOT_QUERY\n";
			}
			else
			{
				cout<<q.front()<<endl;
			}
		}
		else
		{
			cout<<q.size()<<"\n";
		}
	}
	return 0;
}

P10457 占卜DIY

代码

C++
#include<bits/stdc++.h>
using namespace std;
deque<int> q[20];
int a[20],c;
char x;
int main()
{
	for(int i=1;i<=13;i++)
	{
		for(int j=1;j<=4;j++)
		{
			cin>>x;
			if(x<='9'&&x>='1')q[i].push_back(x-'0');
			else if(x=='0')q[i].push_back(10);
			else if(x=='J')q[i].push_back(11);
			else if(x=='Q')q[i].push_back(12);
			else if(x=='K')q[i].push_back(13);
			else if(x=='A')q[i].push_back(1);
		}
	}
	while(!q[13].empty()) 
	{
        int v=q[13].front();
		q[13].pop_front();
        while(v!=13) 
		{
            a[v]++;
            if(a[v]>=4)c++;
            int p=q[v].back();
			q[v].pop_back();
			v=p;
        }
    }
	cout<<c;
	return 0;
}

P3887 [GDOI2014] 世界杯

代码

C++
#include<bits/stdc++.h> 
using namespace std;
priority_queue<long long> k; 
priority_queue<long long> d; 
priority_queue<long long> m;
priority_queue<long long> f;
int n;
int a,b,c,e;
long long t;
int x,y,z;
int main()
{
    cin>>a>>b>>c>>e;
    for(int i=1;i<=a;i++){cin>>t;k.push(t);} 
    for(int i=1;i<=b;i++){cin>>t;d.push(t);} 
    for(int i=1;i<=c;i++){cin>>t;m.push(t);} 
    for(int i=1;i<=e;i++){cin>>t;f.push(t);}
    cin>>n;
    cout<<fixed<<setprecision(2);
    for(int i=1;i<=n;i++)
    {
        long long ans=0; 
        cin>>x>>y>>z;
        if(!k.empty()) 
		{
            ans+=k.top();
            k.pop();
        }
        while(x--)
        {
            if(!d.empty()) 
			{
                ans+=d.top();
                d.pop();
            }
        }
        while(y--)
        {
            if(!m.empty()) 
			{
                ans+=m.top();
                m.pop();
            }
        }
        while(z--)
        {
            if(!f.empty()) 
			{
                ans+=f.top();
                f.pop();
            }
    	}
        cout<<(ans)/11.0<<"\n";
    }
    return 0;
}
原文