CSU 1329 一行盒子(模拟链表)

115 篇文章 0 订阅
22 篇文章 0 订阅

一行盒子

Description

你有一行盒子,从左到右依次编号为1, 2, 3,…, n。你可以执行四种指令:

1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令)。
2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令)。
3 X Y表示交换盒子X和Y的位置。
4 表示反转整条链。

指令保证合法,即X不等于Y。例如,当n=6时在初始状态下执行1 1 4后,盒子序列为2 3 1 4 5 6。接下来执行2 3 5,盒子序列变成2 1 4 5 3 6。再执行3 1 6,得到2 6 4 5 3 1。最终执行4,得到1 3 5 4 6 2。

Input

输入包含不超过10组数据,每组数据第一行为盒子个数n和指令条数m(1<=n,m<=100,000),以下m行每行包含一条指令。

Output

每组数据输出一行,即所有奇数位置的盒子编号之和。位置从左到右编号为1~n。

Sample Input

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

Sample Output

Case 1: 12
Case 2: 9
Case 3: 2500050000

Source
湖南省第九届大学生计算机程序设计竞赛



思路:用数组模拟链表,next[]表示当前结点的下一个节点,pre[]表示当前节点的上一个节点

当进行反转操作时,实际上并不需要反转,只需要记录反转的次数就行了。当反转次数为奇数时,op=1的操作变成op=2的操作,op=2的操作变成op=1的操作,其他的不变。

还有就是要让它成环,这样方便找到起点和终点

具体细节详见代码

代码:

#include<stdio.h>

#define maxn 100000+10
typedef long long LL;
int pre[maxn],next[maxn];
int n,m;

int main()
{
    int T=0;
    while(~scanf("%d%d",&n,&m))
    {
        int tot=0;
        for(int i=1; i<=n; ++i)
        {
            pre[i]=i-1;
            next[i]=i+1;
        }
        next[n]=0;//使之成环,对最后寻找开头和结尾有用
        int op,x,y;
        for(int i=1; i<=m; ++i)
        {
            scanf("%d",&op);
            if(op!=4)
                scanf("%d%d",&x,&y);
            else
            {
                ++tot;
                continue;
            }
            if((tot&1)&&op<3)
                op=3-op;
            if(op==1)
            {
                if(pre[y]==x)
                    continue;
                next[pre[x]]=next[x];
                pre[next[x]]=pre[x];
                next[pre[y]]=x;
                pre[x]=pre[y];
                next[x]=y;
                pre[y]=x;
            }
            else if(op==2)
            {
                if(next[y]==x)
                    continue;
                next[pre[x]]=next[x];
                pre[next[x]]=pre[x];
                pre[next[y]]=x;
                next[x]=next[y];
                pre[x]=y;
                next[y]=x;
            }
            else if(op==3)//注意op==3时,要对xy相邻的情况特判
            {
                if(next[x]==y)
                {
                    int temp1=pre[x],temp2=next[y];
                    next[temp1]=y;
                    pre[y]=temp1;
                    pre[temp2]=x;
                    next[x]=temp2;
                    next[y]=x;
                    pre[x]=y;
                }
                else if(next[y]==x)
                {
                    int temp1=pre[y],temp2=next[x];
                    next[temp1]=x;
                    pre[x]=temp1;
                    pre[temp2]=y;
                    next[y]=temp2;
                    next[x]=y;
                    pre[y]=x;
                }
                else
                {
                    int temp1=next[y],temp2=pre[y];
                    next[pre[x]]=y;
                    pre[next[x]]=y;
                    next[pre[y]]=x;
                    pre[next[y]]=x;
                    next[y]=next[x];
                    pre[y]=pre[x];
                    next[x]=temp1;
                    pre[x]=temp2;
                }
            }
        }
        int *st,*ed;
        if(tot&1)//反转奇数次,最终优化为反转一次
        {
            st=next;//将数组地址赋给指针
            ed=pre;
        }
        else//反转偶数次就是不反转
        {
            st=pre;
            ed=next;
        }
        LL ans=0;
        for(int i=1; i<=n; ++i)
        {
            if(!st[i])//找到开头
            {
                for(int j=1; i; i=ed[i],++j)
                    if(j&1)
                        ans+=i;
                break;
            }
        }
        printf("Case %d: %lld\n",++T,ans);
    }
    return 0;
}



总结:这种题还是需要多做一些,别怕麻烦,不然比赛的时候还是做不出来

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值