项链 牛客小白月赛29

https://ac.nowcoder.com/acm/contest/8564/F

题目描述

scimoon 意外得到了一个项链,这个项链非常的神奇:它有 N 个珠子,一开始每个珠子有一个编号, 从左到右编号分别是 1 至
N,scimoon 进行了 M 次操作,每次操作有下面这么几种:

1 x y :表示将编号为 x 的珠子移到编号为 y 的珠子的后面

2 x y :表示将编号为 x 的珠子移到编号为 y 的珠子的前面

3 :表示翻转这个项链,注意翻转后 1,2 操作中的前后关系会改变

4 :从编号 1 的珠子开始从左到右输出每个珠子的编号

如果您没有完全理解题意,您可以通过阅读样例解释帮助理解

输入描述:

第一行两个数: N 和 M

第 2∼M+1 行:输入每一次操作

输出描述:

对于每次操作 4,输出这个项链每位置上的珠子的编号。

示例1
输入

5 6
1 2 4
4
2 5 3
4
3
4

输出

1 3 4 2 5
1 5 3 4 2
1 2 4 3 5

说明

第一次操作后,2 号珠子被放到了 4 号珠子的后面,项链为 [1,3,4,2,5]

第二次操作输出当前的项链 [1,3,4,2,5]

第三次操作后,5 号珠子被放到了 3 号珠子的前面,项链为 [1,5,3,4,2]

第四次操作输出当前的项链 [1,5,3,4,2]

第五次操作翻转当前项链 [2,4,3,5,1]

第六次操作中,由于我们需要从 1 开始输出项链,因此输出 [1,2,4,3,5]

备注:

1≤N≤10000,1≤M≤100000

用数组模拟双向链表,最后只过了96%的数据,先把代码存放在这。

随后写出来后再更新。 (已更新)

#include<iostream>

using namespace std;
int n,m,op,x,y,a[10005],b[10005],c[10005];
void print_op(){
	int ans=1,tmp=1;
	while(ans <= n){
		if(ans == n) cout<<tmp<<endl;
		else cout<<tmp<<" ";
		tmp = b[tmp];
		ans++;
	}	
}
void test()
{
	cout<<"........"<<endl;
	for(int i=1;i<=n;i++){
		cout<<b[i]<<" ";
	}
	cout<<endl;
	for(int i=1;i<=n;i++){
		cout<<c[i]<<" ";
	}
	cout<<endl<<"........"<<endl;
}
void operation(int op){
	if(op==1){
		cin>>x>>y;
		b[c[x]] = b[x];
		b[x] = b[y];
		b[y] = x;
		for(int i=1;i<=n;i++){
			c[b[i]] = i;
		}
	}else if(op == 2){
		cin>>x>>y;
		b[c[x]] = b[x];
		b[c[y]] = x;
		b[x] = y;
		for(int i=1;i<=n;i++){
			c[b[i]] = i;
		}
	}else if(op == 3){
		int tmp;
		for(int i=1;i<=n;i++){
			tmp = b[i];
			b[i] = c[i];
			c[i] = tmp;
		}
	}else if(op == 4){
		print_op();
	}
	//test();
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		a[i] = i;
		if(i+1 == n) b[i] = n;
		else b[i] = (i+1)%n;
		if(i==1) c[i] = n;
		else c[i] = i-1;
	}
	//test();
	while(m--){
		cin>>op;
		operation(op);
	}
	
}
过100%数据的代码
#include<iostream>

using namespace std;
int n,m,op,x,y,a[10005],to[10005],from[10005],flag=0;
void print_op(){
	int ans=1,tmp=1;
	if(flag==0){
		while(ans <= n){
		if(ans == n) cout<<tmp<<endl;
		else cout<<tmp<<" ";
		tmp = to[tmp];
		ans++;
		}
	}else{
		while(ans <= n){
			if(ans == n) cout<<tmp<<endl;
			else cout<<tmp<<" ";
			tmp = from[tmp];
			ans++;
		}	
	}		
	
}
void f0(){//表示将编号为 x 的珠子移到编号为 y 的珠子的后面
	int a=from[x],b=to[x];
    to[a]=b,from[b]=a;
    int c=from[y],d=to[y];
    to[y]=x,to[x]=d,to[c]=y;
    from[x]=y,from[d]=x,from[y]=c;
}
void f1(){//表示将编号为 x 的珠子移到编号为 y 的珠子的前面
	int a=to[x],b=from[x];
    to[b]=a,from[a]=b;
    int c=to[y],d=from[y];
    from[y]=x,from[x]=d,from[c]=y;
    to[x]=y,to[d]=x,to[y]=c;
}
void test()
{
	cout<<"........"<<endl;
	for(int i=1;i<=n;i++)
		cout<<i<<" ";
	cout<<endl;
	for(int i=1;i<=n;i++){
		cout<<to[i]<<" ";
	}
	cout<<endl;
	for(int i=1;i<=n;i++){
		cout<<from[i]<<" ";
	}
	cout<<endl<<"........"<<endl;
}
void operation(int op){
	if(op==1){
		cin>>x>>y;
		if(flag==0){
			f0();
		}else{
			f1();
		}
		
	}else if(op == 2){
		cin>>x>>y;
		if(flag==0){
			f1();
		}else{
			f0();
		}
		
	}else if(op == 3){
		flag ^= 1;
	}else if(op == 4){
		print_op();
	}
	//test();
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		a[i] = i;
		if(i+1 == n) to[i] = n;
		else to[i] = (i+1)%n;
		if(i==1) from[i] = n;
		else from[i] = i-1;
	}
	//test();
	while(m--){
		cin>>op;
		operation(op);
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值