**题目:**B3630 排队顺序
网址: https://www.luogu.com.cn/problem/B3630
**思路:**我们先找到队首那位同学,然后定义一个数组suf,suf[i]代表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,suf[i]代表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
**思路:**定义两个数组,suf[i]代表i同学左边的同学,pre[i]代表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
**思路:**定义两个数组,suf[i]代表i同学左边的同学,pre[i]代表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;
}