UVa12657移动盒子,双向链表

题目大意:

这道题目是考察双向链表的使用,下面是我用c语言实现的代码:

 #include <stdio.h>

#define maxn 100000+10

int m,n;
int pre[maxn],next[maxn];

void init(){
    int i;
    for(i=1;i<=n;i++){pre[i] = i-1;next[i] = i+1; }
    pre[n+1] = n;
    next[0] = 1;
}
void move(int x,int y,int dir){
    if(dir == 1){
        next[pre[x]] = next[x];
        pre[next[x]] = pre[x];
        pre[x] = pre[y];
        next[x] = y;
        next[pre[y]] = x;
        pre[y] = x;
    }
    else if(dir == 2){
        next[pre[x]] = next[x];
        pre[next[x]] = pre[x];
        pre[x] = y;
        next[x] = next[y];
        pre[next[y]] = x;
        next[y] = x;
    }
}
int main(){
    int kase = 0;
    while(scanf("%d%d",&n,&m)){
        init();
        int i,x,y,op,dir = 1;
        for(i=0;i<m;i++){
            scanf("%d",&op);
            if(op == 4)dir = !dir;
            else{
                scanf("%d%d",&x,&y);
                if(op < 3){
                    if(dir)move(x,y,op);
                    else move(x,y,3-op);
                }else{
                    int lx = pre[x];
                    move(x,y,1);
                    move(y,lx,2);
                }
            }
        }
        int b = 0;
        long long ans = 0;
        for(i = 1;i <= n;i++){
            b = next[b];
            if(i % 2 == 1)ans += b;
        }
        if(!dir && n % 2 == 0) ans = (long long)(1+n)*n/2 - ans;
        printf("Case %d: %lld\n",++kase,ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值