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; 
}
相关推荐
小雪崩30 分钟前
嵌入式学习 day25:哈希表及排序与查找
linux·c语言·数据结构·学习·排序算法
白露与泡影1 小时前
解密 Pi 的 Harness 工程:Agent 会话如何实现持久化与恢复
java·人工智能·算法
happyprince2 小时前
01-整体观-Codex全貌解读(源码)
算法·ai编程
俊基科技3 小时前
别自己写算法了,让这颗37.5毫米的模组替你“闭嘴干活”
神经网络·算法·硬件开发·ai降噪·语音模块·回音消除
_Narcissus_3 小时前
动态规划初探(含完整代码实现)
c语言·数据结构·c++·算法·动态规划·背包问题·最短路径
世人万千丶4 小时前
鸿蒙项目实战 - 社区活动编排板:标签云布局算法与自动换行
学习·算法·华为·harmonyos·鸿蒙
zander2584 小时前
LeetCode 279. 完全平方数
算法·深度优先
法哥20124 小时前
用 C++ 控制 CMW100,到底在干什么?
开发语言·c++
典典分享指南5 小时前
VS Code Git 工作树:解锁多分支并行开发新体验
算法·决策树·逻辑回归·启发式算法