Uva 12657 Mobile box (doubly linked list)

Source: Internet
Author: User

Test instructions

You have a row of boxes, numbered 1, 2, 3,..., n from left to right. You can perform the following 4 types of directives:
1 x y means to move the box X to the left of Box y (if X is already on the left side of Y ignore this directive).
2 x y means to move the box X to the right of Box Y (if X is already on the right side of Y ignore this directive).
3 x y indicates the position of the swap box X and Y.
4 indicates that the entire chain is reversed.

Analysis:

From the point of view of operation, there needs to be a data structure that records what is on the left and right side of each box.

Operation 4 If the simulation is really high complexity is also more troublesome, you can consider building a tag, indicating that there is no action performed 4

But note that if Operation 4 is performed, if the Operation 1 2 does not change, then the Operation 1 is 2,2 1 (left + invert = on the right)

The doubly linked list has a more practical function

The meaning is to connect the L,r two elements, L on the left of R, R on the right of L.

1#include <bits/stdc++.h>2 Const intMAXN =100000+7;3 intN, Left[maxn], RIGHT[MAXN];4 voidLinkintLintR) {//The first parameter is L, the second is R,5RIGHT[L] =R;6LEFT[R] =L;7 }8 intMain ()9 {Ten     intM, Kase =0; One      while(SCANF ("%d%d", &n, &m) = =2){ A          for(inti =1; I <= N; i++){ -Left[i] = i-1; -Right[i] = (i+1)% (n+1); the         } -right[0] =1;//Note 0 Right is 1 Left is n -left[0] =N; -         intOP, X, Y, INV =0; +          while(m--){ -scanf"%d", &op); +             if(OP = =4) INV =!INV; A             Else { atscanf"%d%d", &x, &Y); -                 if(OP = =3&& Right[y] = =X) - Std::swap (x, y); -                 if(OP! =3&& Inv) op =3-op; -                 if(OP = =1&& X = = Left[y])Continue; -                 if(OP = =2&& X = = Right[y])Continue; in              -                 intLX = Left[x], RX = right[x], LY = Left[y], RY =Right[y]; to                 if(OP = =1){ + Link (lx,rx); link ( ly,x); link (x, y); -                 } the                 Else if(OP = =2){ * Link (lx,rx); link ( y,x); link (x,ry) ; $                 }Panax Notoginseng                 Else if(OP = =3){ -                     if(Right[x] = =Y) { the link (lx,y); link (y,x); + link (x,ry); A                     } the                     Else { + link (lx,y); - link (y,rx); $ link (ly,x); $ link (x,ry); -                     } -                 } the             } -         }Wuyi         intb =0; the         Long LongAns =0; -          for(inti =1; I <= N; i++){ Wub =Right[b]; -             ifI2==1) ans + =b; About         } $         if(INV && n%2==0) ans = (Long Long) n * (n+1)/2-ans; -printf"Case %d:%lld\n", ++Kase, ans); -     } -}

Uva 12657 Mobile box (doubly linked list)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.