UVA12657

description
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 n
from 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
这道题是紫书上的原题,用数组写了双向链表。分别用le[x], ri[x]存储x左边节点和右边节点的位置。在处理一些小问题上也用了比较巧妙的方法。例如用inv变量来存储是否反转过队列,如果反转过,就将op = 3 - op。link函数的使用也是很巧妙的方法。

#include <iostream>
#include <cstdio>

using namespace std;
const int maxn = 1000000 + 5;
int ri[maxn], le[maxn];

//用link函数实现类似于指针p->q的功能
void link(int l, int r)
{
    ri[l] = r;
    le[r] = l;
}

int main()
{
    int n, m, kase = 0;
    while (cin >> n >> m) {
        for (int i = 1; i <= n; i++) {
            le[i] = i - 1;
            ri[i] = (i + 1) % (n + 1);//最后一个值指向数组开头,为0的地方
        }
        ri[0] = 1;
        le[0] = n;
        int op, inv = 0, x, y;

        while (m--) {
            cin >> op;
            if (op == 4) inv = !inv;
            else {
                cin >> x >> y;
                if (op == 3 && ri[y] == x) swap (x, y);//必须交换,否则数组指错
                if (op != 3 && inv) op = 3 - op;//反转后就使op = 3 - op
                if (op == 1 && x == le[y]) continue;
                if (op == 2 && x == ri[y]) continue;

                int lx = le[x], rx = ri[x], ly = le[y], ry = ri[y];
                if (op == 1) {
                    link (lx, rx);
                    link (ly, x);
                    link (x, y);
                } else if (op == 2) {
                    link (lx, rx);
                    link (y, x);
                    link (x, ry);
                } else if (op == 3) {
                    if (ri[x] == y) {
                        link (lx, y);
                        link (y, x);
                        link (x, ry);
                    } else {
                        link (lx, y);
                        link (y, rx);
                        link (ly, x);
                        link (x, ry);
                    }
                }
            }
        }

        int b = 0;
        long long ans = 0;//long long型存储
        for (int i = 1; i <= n; i++) {
            b = ri[b];
            if (i % 2) ans += b;
        }
        if (inv && n % 2 == 0) ans = (long long) n * (n + 1) / 2 - ans;
        printf ("Case %d: %lld\n", ++kase, ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值