UVa 12657 Boxes in a line (application doubly linked list)

Source: Internet
Author: User

Boxes in a line

You are n boxes in a line on the table numbered 1 ... Your task is to simulate 4
Kinds of commands:
? 1 x Y:move box X to the left and Y (ignore this if X are already the left of Y)
? 2 x Y:move box X to the right and Y (ignore this if X are already the right of Y)
? 3 x Y:swap box X and Y
? 4:reverse the whole line.
Commands is guaranteed to be valid, i.e. X would be is not equal to Y.
For example, if n = 6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing
2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1.
Then after executing 4, then line becomes 1 3 5 4 6 2

InputThere'll is at the most test cases. Each test case begins with a line containing 2 integers n, m
(1≤n, M≤100, 000). Each of the following m lines contain a command.
Output for each test case, print the sum of numbers at odd-indexed positions. Positions is numbered 1 to n
From left to right.
Sample Input6 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 OutputCase 1:12
Case 2:9
Case 3:2500050000

Test instructions start with n boxes in order of 1 to N to perform m operations on each of these boxes for one of the x, Y reverse order of the two to the left of Y.

After the operation is complete, all odd digits of the original box sequence number and;

Direct simulation will definitely time out with the list in the STL also time-out can only use the array itself to simulate a doubly linked list le[i],ri[i] respectively, the box to the left of the first box and the right box in the ordinal code has a stare

#include <cstdio> #include <cstring>using namespace std;const int n = 100005;int le[n], ri[n], N, M;typedef long Long ll;void link (int l, int r)//connect L and R.  L on the left {le[r] = l; Ri[l] = r;}    int main () {int cas = 0, op, x, Y, t; while (scanf ("%d%d", &n, &m)! = EOF) {for (int i = 1; I <= n; ++i) Ri[i] = i + 1, le[i]        = I-1;        Ri[n] = 0, le[0] = n, ri[0] = 1;                      int flag = 0;            Infers whether to flip while (m--) {scanf ("%d", &op);            if (op = = 4) flag =!flag;                else {scanf ("%d%d", &x, &y); if (flag && OP! = 3) op = 3-op;  After flipping the move operation is reversed if (ri[y] = = x && op = = 3)//Convenient to later infer whether the interchange is adjacent t = x, x = y, y =                T if (op = = 1 && le[y] = = x) | |  (OP = = 2 && Ri[y] = = x))                Continue           if (op = = 1)//x move to the right of Y         Link (le[x], ri[x]), Link (le[y], x), link (x, y);                else if (op = = 2)//x moves to the left of Y Link (le[x], ri[x]), link (x, ri[y]), link (y, x); else if (y = = ri[x])//op==3&&x,y adjacent link (le[x], y), link (x, ri[y]), link                (y, x);                    else//nonadjacent {int ry = Ri[y], ly = le[y];                Link (le[x], y), Link (y, ri[x]), Link (ly, x), link (x, ry); }}} t = 0;        ll ans = 0;            for (int i = 1; I <= n; ++i) {t = ri[t];        if (i% 2) ans + = t;        if (n 2 = = 0 && flag)//n is an even number and flipped over so that it is just even digits and ans = (ll) N/2 * (1 + N)-ans;    printf ("Case%d:%lld\n", ++cas, ans); } return 0;}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

UVa 12657 Boxes in a line (application 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.