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

直接模拟肯定会超时 用stl中的链表也超时 只能用数组自己模拟一个双向链表了 le[i],ri[i]分别表示第i个盒子左边盒子的序号和右边盒子的序号

参考代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <cmath>
  6 #include <string>
  7 #include <vector>
  8 #include <set>
  9 #include <map>
 10 #include <queue>
 11 #include <stack>
 12 #include <sstream>
 13 #include <cctype>
 14 #include <cassert>
 15 #include <typeinfo>
 16 #include <utility>    //std::move()
 17 using std::cin;
 18 using std::cout;
 19 using std::endl;
 20 const int INF = 0x7fffffff;
 21 const double EXP = 1e-8;
 22 const int MS = 100005;
 23 typedef long long LL;
 24 int left[MS], right[MS];
 25 int n, m, kase;
 26 void link(int l, int r)
 27 {
 28     right[l] = r;
 29     left[r] = l;
 30 }
 31
 32 int main()
 33 {
 34     int kase = 0;
 35     while (cin >> n >> m)
 36     {
 37         for (int i = 1; i <= n; i++)
 38         {
 39             left[i] = i - 1;
 40             right[i] = (i + 1) % (n + 1);
 41         }
 42         right[0] = 1;
 43         left[0] = n;
 44         int op, x, y, inv = 0;
 45         while (m--)
 46         {
 47             cin >> op;
 48             if (op == 4)
 49                 inv = !inv;
 50             else
 51             {
 52                 cin >> x >> y;
 53                 if (op == 3 && right[y] == x)
 54                     std::swap(x, y);
 55                 if (op != 3 && inv)
 56                     op = 3 - op;
 57                 if (op == 1 && x == left[y])
 58                     continue;
 59                 if (op == 2 && x == right[y])
 60                     continue;
 61                 int lx = left[x], rx = right[x], ly = left[y], ry = right[y];
 62                 if (op == 1)
 63                 {
 64                     link(lx, rx);
 65                     link(ly, x);
 66                     link(x, y);
 67                 }
 68                 else if (op == 2)
 69                 {
 70                     link(lx, rx);
 71                     link(y, x);
 72                     link(x, ry);
 73                 }
 74                 else if (op == 3)
 75                 {
 76                     if (right[x] == y)
 77                     {
 78                         link(lx, y);
 79                         link(y, x);
 80                         link(x, ry);
 81                     }
 82                     else
 83                     {
 84                         link(lx, y);
 85                         link(y, rx);
 86                         link(ly, x);
 87                         link(x, ry);
 88                     }
 89                 }
 90             }
 91
 92         }
 93         int  b = 0;
 94         LL ans = 0;
 95         for (int i = 1; i <= n; i++)
 96         {
 97             b = right[b];
 98             if (i % 2)
 99                 ans += b;
100         }
101         if (inv&&n % 2 == 0)
102             ans = (LL)n*(n + 1) / 2 - ans;
103         cout << "Case " << ++kase << ": " << ans << endl;
104     }
105     return 0;
106 }
时间: 2024-08-09 20:35:07

Boxes in a Line的相关文章

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

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

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

O - Boxes in a Line

O - Boxes in a Line Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 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 t

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

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

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

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

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

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