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个盒子左边盒子的序号和右边盒子的序号

参考代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <cctype>
#include <cassert>
#include <typeinfo>
#include <utility> //std::move()
using std::cin;
using std::cout;
using std::endl;
const int INF = 0x7fffffff;
const double EXP = 1e-;
const int MS = ;
typedef long long LL;
int left[MS], right[MS];
int n, m, kase;
void link(int l, int r)
{
right[l] = r;
left[r] = l;
} int main()
{
int kase = ;
while (cin >> n >> m)
{
for (int i = ; i <= n; i++)
{
left[i] = i - ;
right[i] = (i + ) % (n + );
}
right[] = ;
left[] = n;
int op, x, y, inv = ;
while (m--)
{
cin >> op;
if (op == )
inv = !inv;
else
{
cin >> x >> y;
if (op == && right[y] == x)
std::swap(x, y);
if (op != && inv)
op = - op;
if (op == && x == left[y])
continue;
if (op == && x == right[y])
continue;
int lx = left[x], rx = right[x], ly = left[y], ry = right[y];
if (op == )
{
link(lx, rx);
link(ly, x);
link(x, y);
}
else if (op == )
{
link(lx, rx);
link(y, x);
link(x, ry);
}
else if (op == )
{
if (right[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 = ;
LL ans = ;
for (int i = ; i <= n; i++)
{
b = right[b];
if (i % )
ans += b;
}
if (inv&&n % == )
ans = (LL)n*(n + ) / - ans;
cout << "Case " << ++kase << ": " << ans << endl;
}
return ;
}

Boxes in a Line的更多相关文章

  1. Problem B Boxes in a Line

     省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...

  2. uva-12657 - Boxes in a Line(双向链表)

    12657 - Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to righ ...

  3. 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 sim ...

  4. C - 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 simul ...

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

  6. Boxes in a Line UVA - 12657

      You have n boxes in a line on the table numbered 1...n from left to right. Your task is to simulat ...

  7. UVA 12657 Boxes in a Line 双向链表

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...

  8. 例题6-5 Boxes in a line uVa12657

    这道题目的解决方案是双向链表,数据结构本身并不复杂,但对于四种情况的处理不够细致,主要体现在以下几点: 分类讨论不全面,没有考虑特殊情况(本身不需要操作,需要互换的两元素相邻) 没有考虑状态4改变后对 ...

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

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

随机推荐

  1. 多元线性回归(Linear Regression with multiple variables)与最小二乘(least squat)

    1.线性回归介绍 X指训练数据的feature,beta指待估计得参数. 详细见http://zh.wikipedia.org/wiki/%E4%B8%80%E8%88%AC%E7%BA%BF%E6% ...

  2. 一个鼠标键盘控制两台甚至多台主机的方法--Synergy

    在多台主机,不同系统中操作.避免了更换键鼠的麻烦.即使下面图中的功能. 鼠标同时在三台或者多台主机之间进行移动,而且是无缝滑动,鼠标直接从左滑倒右,而且支持,这台电脑复制,另一台黏贴.非常的方便实用. ...

  3. HBase Shell手动移动Region

    在生产环境中很有可能有那么几个Region比较大,但是都运行在同一个Regionserver中. 这个时候就需要手动将region移动到负载低的Regionserver中. 步骤: 1.找到要移动的r ...

  4. Java——观察者模式实例

    观察者模式(订阅/发布模式) 作者: 代码大湿 代码大湿 Java中观察者模式中主要是Observerable类(被观察者),和Observer接口(观察者).下面是个简单的demo //被观察者 p ...

  5. UVALive 7327 Digit Division (模拟)

    Digit Division 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/D Description We are given ...

  6. [C语言 - 7] 结构体struct

    A. 基本知识   与数组的对比 数组: 构造类型 只能有多个相同类型的数据构成   结构体: 结构体类型 可以由多个不同类型的数据构成   1. 定义类型 struct Student { int ...

  7. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. 浅析网站开发中的 meta 标签的作用

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  9. 沉金板VS 镀金板

    沉金板VS 镀金板一.沉金板与镀金板的区别1.原理区别FLASH GOLD 采用的是化学沉积的方法!PLANTINGGOLD 采用的是电解的原理!2.外观区别电金会有电金引线,而化金没有.而且若金厚要 ...

  10. linux知识积累

                                                         linux 操作系统一.linux 操作系统概述   简介            Linux是 ...