12657-Boxes in a Line

双向链表的模拟,快被这水题弄无语了。。。。。第四种方法一开始暴利翻转,之后发现太麻烦了,最后参考LRJ书上的方法 AC了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<list>
#include<set>
#define MAXD 100000 + 10
using namespace std;
typedef long long LL;
int main(){
    int n,m;
    int Case = 1;
    while(scanf("%d%d",&n,&m) != EOF){
        int Left[MAXD],Right[MAXD];
        int List[MAXD];
        Left[0]  = 0;
        Right[0] = 1;
        Left [n + 1] = n;
        Right[n + 1] = 0;
        int _count = 0;
        for(int i = 1 ; i <= n ; i++){
            List[i] =  i;
            Left[i] =  i - 1;
            Right[i] = i + 1;
        }
        for(int i = 0 ; i < m ; i ++){
             int t;
             scanf("%d",&t);
             if(t < 3 && _count & 1) t = 3 - t;
             if(t == 1){
                 int x,y;
                 scanf("%d%d",&x,&y); /*将x移动到y的左边*/
                 if(x == Left[y]) continue;
                 Right[Left[x]] = Right[x]; Left[Right[x]] = Left[x];
                 Right[Left[y]] = x;        Left[x] = Left[y];
                 Right[x] = y;              Left[y] = x;
             }
             else if(t == 2){
                 int x,y;
                 scanf("%d%d",&x,&y); /*将x移动到y的右边*/
                 if(x == Right[y]) continue;
                 Right[Left[x]] = Right[x]; Left[Right[x]] = Left[x];
                 Left[Right[y]] = x;        Right[x] = Right[y];
                 Right[y] = x;              Left[x] = y;
             }
             else if(t == 3){
                 int x, y;
                 scanf("%d%d",&x,&y);
                 int a1 = Left[x],b1 = Right[x];
                 int a2 = Left[y],b2 = Right[y];
                 if(x == Left[y]){
                     Right[a1] = y;Left[y] = a1;
                     Left[b2] = x; Right[x] = b2;
                     Left[x]  = y; Right[y] = x;
                 }
                 else if(x == Right[y]){
                     Right[a2] = x; Left[x] = a2;
                     Left[b1] = y ; Right[y] = b1;
                     Right[x] = y;  Left[y] = x;
                 }
                 else{
                 Left[x] = a2; Right[a2] = x; Right[x] = b2 ; Left[b2]= x;
                 Left[y] = a1; Right[a1] = y; Right[y] = b1 ; Left[b1]= y;
                 }
             }
             else if(t == 4){
                 _count ++ ;
             }
         }
            if(_count & 1){ /*逆转过*/
                 LL sum = 0;
                 for(int i = Left[n + 1] ,t = 1; i != 0 ; i = Left[i])
                 if(i != n + 1){
                 if(t  & 1) {sum += i;}
                 t++;
                 }
                 printf("Case %d: %lld\n",Case++,sum);
             }
             else{
                 LL sum = 0;
                 for(int i = Right[0] ,t = 1; i != 0 ; i = Right[i])
                 if(i != n + 1){
                 if(t  & 1) {sum += i;}
                 t++;
                 }
                 printf("Case %d: %lld\n",Case++,sum);
             }
    }
    return 0;
}


你可能感兴趣的:(12657-Boxes in a Line)