UVA - 12657 Boxes in a Line 双向链表

Boxes in a Line

    You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands:

        •  1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ).

        • 2 X Y : move box X to the right to Y (ignore this if X is already the right of Y ).

        • 3 X Y : swap box X and Y.

        • 4: reverse the whole line.

    Commands are guaranteed to be valid, i.e. X will be 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.

Input

    There will be at most 10 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 are numbered 1 to nfrom left to right.

Sample Input

6 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 Output

Case 1: 12

Case 2: 9

Case 3: 2500050000

  分析

    数据量大,如果使用存粹的数组来表示某位置盒子的编号,会因为频繁的移动导致超时。就比如一直重复将第一个位置的盒子放到最后一个位置的右边。这时我们需要移动n-1次。如果这样操作m次也就是m*(n-1)次移动接近10^12次操作。肯定超时。

    可以利用链表来表示某个位置关系。这时我们的盒子没有移动,而是在盒子的上面贴上两个标签,一个表示这个盒子的左边的盒子是哪个,一个是这个盒子的 右边是哪个盒子。这样我们的操作就是修改这些盒子上的标签就能表示出所有盒子的位置。由于盒子不移动,所以我们能迅速定位某个盒子(移动操作针对的是盒子的编号而不是某个位置的盒子)。并且修改盒子上的标签。

    针对盒子标签的修改我们只需要模拟一下移动操作中对标签的修改就能模拟出这个过程。这里不赘述。直接放代码:

注意:两个盒子相邻与不相邻的交换操作有区别,需要分别讨论。
/*
	problem: https://vjudge.net/problem/UVA-12657
	author: zxz
	time:
	memory:
	disadvantage:
*/


#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <math.h>
#include <queue>
#include <string>
#include <sstream>
#include <vector>
#include <string.h>
#include <time.h>
using namespace std;

const int maxn = 1e5 + 5;
const int INF = 1e9;

int L[maxn];
int R[maxn];
//链接两个盒子 其中i为相对左边的盒子, j为相对右边的盒子
void link(int i, int j) {
    R[i] = j;
    L[j] = i;
}

void solve() {
    int n,m,kase = 1;
    while(scanf("%d %d", &n, &m) == 2 && n) {
        for(int i = 0; i <= n; i++) {
            link(i, i+1);
        }
        int op, x,y, inv = 1;
        while(m--) {
            scanf("%d", &op);
            if(op == 4) {
                inv = !inv;    //懒标记,因为反转一个链表需要将所有节点的左右两个标签交换。
                                //我们直接利用一个标志来代表是否反转。处理的时候注意处理这种情况就行
                continue;
            }
            scanf("%d %d", &x, &y);
            if(op == 3 && R[y] == x)    //如果y在x的左边 交换xy 方便后面代码操作。
                swap(x, y);
            int lx = L[x], ly = L[y];
            int rx = R[x], ry = R[y];
            if(op != 3 && !inv) { // reversed  如果已经反转,左边就是右边 右边就是左边
                op = 3 - op;
            }
            if(op == 1 && ly == x)
                continue;
            if(op == 2 && ry == x)
                continue;
            if(op == 1) {
                link(lx, rx);
                link(ly, x);
                link(x,y);
            } else if(op == 2) {
                link(lx,rx);
                link(x,ry);
                link(y,x);
            } else if(op == 3) {
                if(x == ly) {            //如果两个盒子相邻,则交换两个盒子的操作有点区别
                    link(lx,y);
                    link(x,ry);
                    link(y,x);
                } else {
                    link(lx,y);
                    link(y,rx);
                    link(ly,x);
                    link(x,ry);
                }
            }

        }
        long long ans = 0;
        int nxt = 0;
        for(int i = 1; i <= n; i++) {
            nxt = R[nxt];
            if(i % 2 == 1)
                ans += nxt;
        }
        if(!inv && n % 2 == 0)            
            ans = (long long )n*(n+1)/2 - ans;
        printf("Case %d: %lld\n", kase++, ans);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("cin.txt", "r", stdin);
    freopen("cout.txt", "w", stdout);
    int mao = clock();
#endif
    solve();
#ifndef ONLINE_JUDGE
    cerr << "Time:" << clock() - mao << "ms" << endl;
#endif
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值