UVa12657 - Boxes in a Line(数组模拟链表)

题目大意

你有一行盒子,从左到右依次编号为1, 2, 3,…, n。你可以执行四种指令:

1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令)。
2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令)。
3 X Y表示交换盒子X和Y的位置。
4 表示反转整条链。

盒子个数n和指令条数m(1<=n,m<=100,000)

题解

用数组来模拟链表操作,对于每个节点设置一个前驱和后继。

1操作是把x的前驱节点和x的后继节点连接,y节点的前驱和x节点连接,x节点和y节点连接。

2,3,的做法和1差不多

4操作由于操作两次就等于没有操作,所以只要判断它最终是不是执行了奇数次,如果是就把n个节点的前驱和后继交换下。还有就是在执行1,2的时候如果之前4操作了奇数次,那么1,2两个执行的操作分别是2操作和1操作。

代码:

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <utility>
  6 #include <vector>
  7 #include <queue>
  8 using namespace std;
  9 #define INF 0x3f3f3f3f
 10 #define maxn 111111
 11 typedef long long LL;
 12 int nxt[maxn], pre[maxn];
 13 void init(int n)
 14 {
 15     for (int i = 1; i <= n;i++)
 16     {
 17         pre[i] = i - 1;
 18         nxt[i] = i + 1;
 19     }
 20     nxt[n] = 0;
 21     nxt[0] = 1; pre[0] = n;
 22 }
 23 void link(int l, int r)
 24 {
 25     pre[r] = l; nxt[l] = r;
 26 }
 27 int main()
 28 {
 29     int n, m,kase=0;
 30     while (scanf("%d%d", &n, &m) != EOF)
 31     {
 32         int rev = 0;
 33         init(n);
 34         while (m--)
 35         {
 36             int op, x, y;
 37             scanf("%d", &op);
 38             if (op == 4) rev = 1 - rev;
 39             else
 40             {
 41                 scanf("%d%d", &x, &y);
 42                 if ((op == 1 || op == 2) && rev) op = 3 - op;
 43                 if (op == 3&&nxt[y] == x) swap(x, y);
 44                 int lx, rx, ly, ry;
 45                 lx = pre[x]; rx = nxt[x];
 46                 ly = pre[y]; ry = nxt[y];
 47                 if (op == 1)
 48                 {
 49                     if (nxt[x]==y) continue;
 50                     link(lx, rx);
 51                     link(ly, x);
 52                     link(x, y);
 53
 54                 }
 55                 else if (op == 2)
 56                 {
 57                     if (nxt[y]==x) continue;
 58                     link(lx, rx);
 59                     link(x, ry);
 60                     link(y, x);
 61                 }
 62                 else
 63                 {
 64                     if (nxt[x] == y)
 65                     {
 66                         link(lx, y);
 67                         link(y, x);
 68                         link(x, ry);
 69                     }
 70                     else
 71                     {
 72                         link(lx, y);
 73                         link(y, rx);
 74                         link(ly, x);
 75                         link(x, ry);
 76                     }
 77                 }
 78             }
 79         }
 80         if (rev)
 81         {
 82             for (int i = 1; i <= n; i++) swap(pre[i], nxt[i]);
 83         }
 84         int pos;
 85         for (int i = 1; i <= n; i++)
 86         {
 87             if (pre[i] == 0)
 88             {
 89                 pos = i;
 90                 break;
 91             }
 92         }
 93         int cnt = 1;
 94         LL ans = 0;
 95         while (pos!=0)
 96         {
 97             if (cnt & 1) ans += pos;
 98             pos = nxt[pos];
 99             cnt++;
100         }
101         printf("Case %d: %I64d\n", ++kase,ans);
102     }
103     return 0;
104 }

UVa12657 - Boxes in a Line(数组模拟链表)

时间: 2024-08-29 04:13:33

UVa12657 - Boxes in a Line(数组模拟链表)的相关文章

【日常学习】【模拟双向链表】【疑问】Uva12657 - Boxes in a Line题解

这道题目我做的不对.事实上,我按书上的标程抄的,几乎一模一样,我认为他没有什么错误,可我就是不知道为什么我在代码仓库下的刘汝佳写的程序就AC,我写的就WA.跳了一下午,两程序样例输出完全一样(奇怪的是和书上答案不一样)一个字一个字的比对,就是找不出哪里不一样.我觉得极少不一样的地方应该没有影响,哪位大神愿意给看看? 这是一道双向链表,同样没有用指针,而是用两个数组模拟,道理和上面的那道非指针单向链表题目一样 刘汝佳的代码 // UVa12657 Boxes in a Line // Rujia

B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (internally). You're not aware of this iss

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, y

【日常学习】【SPFA负环+数组模拟链表实现】codevs2645 Spore题解

之前刚刚写了一道"香甜的黄油",是USACO的经典题目了.那道题用SPFA怎么找都过不了,看着别人的PAS轻松过各种拙计.黄学长说最佳方案应当是堆优化的dij,我还没有血,等学了那个之后再写黄油题解吧. 题目: 题目描述 Description 在星系1 的某颗美丽的行星之上.某陈将去标号为N 的星系,从星系g1 到达g2,某陈需要花费c1 的代价[主要是燃料,另外还有与沿途Grox 的势力作战的花费],c1 小于0 则是因为 这样的星系旅行,会给某陈带来收益[来源于物流差价,以及一些

UVa 11988 数组模拟链表

题目:在一个没有显示器的电脑上输入一个字符串,键盘坏掉了,会随机的出现home,和end按键, 字符串中'['代表home键(句首),']'代表end键(句尾),问最后输出的字符串的格式. 分析:模拟屏幕操作,移动光标,模拟缓冲区输出操作. 说明:数组模拟链表操作,跟随链表操作,形象化模拟. 1 // UVa11988 Broken Keyboard 2 // Rujia Liu 3 #include<cstdio> 4 #include<cstring> 5 const int

UVA12657 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 (ig

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 th

UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】

Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (inter

Uva12657 (Boxes in a Line)移动盒子

UVA 12657 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 4kinds 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