uva 12657 Boxes in a Line

题目:Boxes in a Line


题意:

有n个盒子,初始按1~n排列。

给定4个操作:

 1 x y  将x移动到y的左边;

 2 x y  将x移动到y的右边;

 3 x y  将x和y的位置交换;

 4        将序列的位置颠倒。

输出变换后在奇数位置的木块编号总和。


思路:

用链表存储木块的位置。由于操作4比较费时间,所以可以用 bool rev 来表示序列是否颠倒了,true为颠倒。每次执行操作4时,rev^=1。当rev=true时,操作1和2是反着的。


注意:

1、最后结果用 long long 存储。

2、操作3要讨论两木块是否挨着。


代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
using namespace std;

struct list {
	int front,back;
	list(int one,int two) {
		front=one,back=two;
	}
	list() {}
};
int n,m;
list a[100005];

void init() {
	for(int i=1; i<=n; i++) {
		a[i]=list(i-1,i+1);
	}
	a[1].front=100001;
	a[n].back=100001;
}

int main() {
	int T=0;
	while(scanf("%d%d",&n,&m)==2) {
		init();
		bool rev=0;
		while(m--) {
			int opr;
			scanf("%d",&opr);
			if((opr==1&&rev==0)||(opr==2&&rev==1)) {
				int x,y;
				scanf("%d%d",&x,&y);
				a[a[x].back].front=a[x].front;
				a[a[x].front].back=a[x].back;
				a[a[y].front].back=x;
				a[x].back=y;
				a[x].front=a[y].front;
				a[y].front=x;
			}
			if((opr==2&&rev==0)||(opr==1&&rev==1)) {
				int x,y;
				scanf("%d%d",&x,&y);
				a[a[x].back].front=a[x].front;
				a[a[x].front].back=a[x].back;
				a[a[y].back].front=x;
				a[x].front=y;
				a[x].back=a[y].back;
				a[y].back=x;
			}
			if(opr==3) {
				int x,y;
				scanf("%d%d",&x,&y);
				if(a[x].front==y) {
					int s=x;
					x=y;
					y=s;
				}
				if(a[x].back==y) {
					a[a[x].front].back=y;
					a[a[y].back].front=x;
					list t=list(a[x].front,a[x].back);
					a[x].front=y;
					a[x].back=a[y].back;
					a[y].front=t.front;
					a[y].back=x;
				} else {
					a[a[x].back].front=y;
					a[a[x].front].back=y;
					a[a[y].back].front=x;
					a[a[y].front].back=x;
					list t=list(a[x].front,a[x].back);
					a[x].front=a[y].front;
					a[x].back=a[y].back;
					a[y].front=t.front;
					a[y].back=t.back;
				}
			}
			if(opr==4) rev^=1;
		}
		long long ans=0;
		int now=0,odd=false;
		if(rev==1) {
			for(int i=1; i<=n; i++) {
				a[i]=list(a[i].back,a[i].front);
			}
		}
		for(int i=1; i<=n; i++) {
			if(a[i].front==100001) now=i;
		}
		while(now!=100001) {
			odd^=1;
			if(odd==true)
				ans+=now;
			now=a[now].back;
		}
		printf("Case %d: %lld\n",++T,ans);
	}
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值