Uva-12657 Boxes in a Line(双向链表)

注意操作3是通过数字寻找位置的,不会受是否翻转的影响

#include <cstdio>
#include <algorithm>
using namespace std;

#define BG 100005

int be[BG],ne[BG];
int w=1;
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i;
        int x,a,b;
        int tn,tb;
        int inv=0;//use inv to show whether the link has been inverted for odd times.
        be[0]=n;
        ne[0]=1;
        for(i=1;i<n;i++)
            ne[i]=i+1;
        ne[n]=0;
        for(i=2;i<=n;i++)
            be[i]=i-1;
        be[1]=0;
        for(i=0;i<m;i++)
        {
            scanf("%d",&x);
            if(x==4)
                inv=!inv;//fetch the opposite value
            else
            {
                if(x!=3&&inv==1) x=3-x;//if x is 1 or 2,change the direction of the order.
                if(x==1)
                {
                    scanf("%d%d",&a,&b);
                    if(be[b]!=a)
                    {
                        ne[be[a]]=ne[a];
                        be[ne[a]]=be[a];
                        be[a]=be[b];
                        ne[a]=b;
                        ne[be[b]]=a;
                        be[b]=a;
                    }
                }
                else if(x==2)
                {
                    scanf("%d%d",&a,&b);
                    if(ne[b]!=a)
                    {
                        ne[be[a]]=ne[a];
                        be[ne[a]]=be[a];
                        ne[a]=ne[b];
                        be[a]=b;
                        be[ne[b]]=a;
                        ne[b]=a;
                    }
                }
                else if(x==3)//operation 3 isn't influenced by the inv
                {
                    scanf("%d%d",&a,&b);
                    ne[be[a]]=b;
                    be[ne[a]]=b;
                    tn=ne[b];
                    tb=be[b];
                    ne[b]=ne[a];
                    be[b]=be[a];
                    ne[tb]=a;
                    be[tn]=a;
                    ne[a]=tn;
                    be[a]=tb;
                }

            }
//            else if(x==4)//waste too much time.
//                inv=!inv;
//            {
//                
//                for(i=0;i<=n;i++)
//                {
//                    t=be[i];
//                    be[i]=ne[i];
//                    ne[i]=t;
//                }
//            }
        }
        i=ne[0];
        long long sum=0;
        int count=1;
        if(n%2==0&&inv==1)
            i=ne[ne[0]];
        while(i!=0)
        {
            if(count%2!=0)
                sum+=i;
            i=ne[i];
            count++;
        }
        printf("Case %d: ",w);
        printf("%lld\n",sum);
        w++;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值