noi-2026年3月24号作业

**题目:**B3630 排队顺序

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

**思路:**我们先找到队首那位同学,然后定义一个数组suf,sufi代表i同学后面的那位同学

**知识点:**模拟

代码:

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 db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn]; 
ll n,m;
int suf[maxn];
vector<int>vt;
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	int x;
    	cin>>x;
    	suf[i]=x;
	}
	int now;
	cin>>now;
	while(now!=0)
	{
		vt.pb(now);
		now=suf[now];
	}
	for(auto it:vt)
	cout<<it<<" ";
	
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

**题目:**B3631 单向链表

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

**思路:**定义一个数组suf,sufi代表i后面的元素

**知识点:**模拟

代码:

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 db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn]; 
ll n,m;
vector<int>vt;
int suf[maxn];
void solve()
{
    int q;
    cin>>q;
    suf[1]=0;
    while(q--)
    {
    	int op;
    	cin>>op;
    	if(op==1)
    	{
    		int x,y;
    		cin>>x>>y;
    		int old=suf[x];
    		suf[x]=y;
    		suf[y]=old;
		}else if(op==2)
		{
			int x;
			cin>>x;
			cout<<suf[x]<<'\n';
		}else if(op==3)
		{
			int x;
			cin>>x;
			if(suf[x]!=0)
			{
				suf[x]=suf[suf[x]];
			}
		}
	}
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

**题目:**P1160 队列安排

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

**思路:**定义两个数组,sufi代表i同学左边的同学,prei代表i同学右边的同学

**知识点:**模拟,用数组模拟链表

代码:

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 db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn]; 
ll n,m;
vector<int>vt;
int pre[maxn],suf[maxn];
void solve()
{
    cin>>n;
    pre[1]=0;
    suf[0]=1;
    pre[n+1]=1;
    suf[1]=n+1;
    for(int i=2;i<=n;i++)
    {
    	int k,p;
    	cin>>k>>p;
    	if(p==0)
    	{
    		int old=pre[k];
    		pre[k]=i;
    		suf[i]=k;
    		pre[i]=old;
    		suf[old]=i;
		}else if(p==1)
		{
			int old=suf[k];
			suf[k]=i;
			pre[i]=k;
			suf[i]=old;
			pre[old]=i;
		}
	}
	cin>>m;
	set<int>st;
	for(int i=1;i<=m;i++)
	{
		int x;
		cin>>x;
		st.insert(x);
	}
	int now=suf[0];
	while(now>=1&&now<=n)
	{
		if(st.find(now)==st.end())
		cout<<now<<' ';
		now=suf[now];
	}
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

**题目:**P1996 约瑟夫问题

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

**思路:**定义两个数组,sufi代表i同学左边的同学,prei代表i同学右边的同学。因为数据很小,那么我们就直接while循环枚举

**知识点:**模拟,数组模拟链表

代码:

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 db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn]; 
ll n,m;
vector<int>vt;
int pre[maxn],suf[maxn];
void solve()
{
    cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
	   suf[i]=i+1;	
	} 
	suf[n]=1;
	for(int i=2;i<=n;i++)
	{
		pre[i]=i-1;
	}
	pre[1]=n;
	int now=1;
	while(n>=1)
	{
		for(int i=2;i<=m;i++)
		{
			now=suf[now];
		}
		cout<<now<<" ";
		int old_pre=pre[now],old_suf=suf[now];
		suf[old_pre]=old_suf;
		pre[old_suf]=old_pre;
		now=old_suf;
		n--;
	}
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

**题目:**P7912 CSP-J 2021 小熊的果篮

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

**思路:**我们把连续的同一个数字的当作一个group,然后我们直接模拟

**知识点:**模拟,分块

代码:

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 db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn]; 
ll n,m;
vector<int>vt;
struct node{
	int st,ed,co;
};
queue<node>q,q2;
void solve()
{
    cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	a[n+1]=1-a[n];
	int st=1;
	for(int i=2;i<=n+1;i++)
	{
		if(a[i]!=a[i-1])
		{
			q.push({st,i-1,a[i-1]});
			st=i;
		}
	}
	int cnt=n;
	while(cnt)
	{
		while(q.size())
		{
			node n1=q.front();
			q.pop();
			while(vis[n1.st])
			n1.st++;
			if(n1.st>n1.ed)
			continue;
			vis[n1.st]=1;
			cout<<n1.st<<" ";
			cnt--;
			n1.st++;
			if(n1.st<=n1.ed)
			q2.push(n1);
		}
		cout<<'\n';
		while(q2.size())
		{
			node n2=q2.front();
			q2.pop();
			while(q2.size())
			{
				node res=q2.front();
				if(res.co==n2.co)
				{
					n2.ed=res.ed;
					q2.pop();
				}else break;
			}
			q.push(n2);
		}
	}
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}
相关推荐
leobertlan1 小时前
好玩系列 | 以简构繁,从把玩一把天平进入信息论和编码
算法
电科_银尘1 小时前
【大模型算法与工程 48】项目实战:可调用、可追踪的大模型问答服务
算法
电科_银尘1 小时前
【大模型算法与工程 47】服务工程:并发、排队、限流与可观测性
算法
西数科技CDRSA2 小时前
BGA封装NAND芯片不识别,Flash Extractor引脚直读还原原始镜像
数据结构·算法·硬件工程
星恒随风2 小时前
C++11 加餐(2)并发支持库——mutex、lock_guard、unique_lock 与死锁问题
开发语言·c++
AvocadoZeroOneOI3 小时前
Kadane 算法(最大子段和)
c++·学习·算法·动态规划
汉克老师5 小时前
GESP六级第二课:森林里的分叉城堡——二叉树的构造与存储
c++·二叉树·树·gesp·小学生·小学生学编程
Lazyafei6 小时前
滑动窗口:扩张与收缩的动态区间
算法
FRing037 小时前
for循环、while循环、Java高级语法、数组
java·开发语言·后端·算法
老歌老听老掉牙7 小时前
三个等价答案:遗传算法如何逼近 Branin 函数
c++·算法·ga