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 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 executing2 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
使用双向链表解决,静态链表,挺简单的
#include<iostream>
using namespace std; const int size = 100000 + 5; void Link(int L, int R, int* right, int*left)
{
right[L] = R;
left[R] = L;
} void op1(int X, int Y, int* right, int*left) //操作一
{
int lx = left[X];
int rx = right[X];
int ly = left[Y];
Link(X, Y, right, left);
Link(ly, X, right, left);
Link(lx, rx, right, left);
} void op2(int X, int Y, int* right, int*left) //操作二
{
int lx = left[X];
int rx = right[X];
int ry = right[Y];
Link(Y, X, right, left);
Link(X, ry, right, left);
Link(lx, rx, right, left);
} void op3(int X, int Y, int* right, int*left) //操作三
{
int lx = left[X];
int rx = right[X];
int ly = left[Y];
int ry = right[Y];
Link(X, ry, right, left);
Link(ly, X, right, left);
Link(Y, rx, right, left);
Link(lx, Y, right, left);
} int main()
{
int right[size] = {0};
int left[size] = {0}; int n, m, kcase = 0;
while (cin >> n >> m)
{
//初始化
for (int i = 1; i <= n; i++)
{
left[i] = i - 1;
right[i] = i + 1;
}
left[0] = n;
right[0] = 0;
int op, X, Y, inv = 0; //inv是一个操作,如果进行了操作就变为1 while (m--)
{
cin >> op;
if (op == 4)inv = 1;
else
{
cin >> X >> Y;
if (op == 3 && right[Y] == X)
{
int rx = right[X];
int ly = left[Y];
Link(ly, X, right, left);
Link(Y, rx, right, left);
Link(X, Y, right, left);
}
else if (op == 3 && right[X] == Y)
{
int lx = left[X];
int ry = right[Y];
Link(X, ry, right, left);
Link(lx, Y, right, left);
Link(Y, X, right, left);
}
else if (op == 3 && right[X] != Y&&right[Y] != X)
{
op3(X, Y, right, left);
}
else if (op == 1 && inv)op2(X, Y, right, left);
else if (op == 2 && inv)op1(X, Y, right, left);
else if (op == 1 && X == left[Y])continue;
else if (op == 2 && Y == right[X])continue;
else if (op == 1 && !inv)op1(X, Y, right, left);
else if (op == 2 && !inv)op2(X, Y, right, left);
}
}
int b = 0;
long long result = 0;
for (int i = 1; i <= n; i++)
{
b = right[b];
if (i % 2 != 0)result += b;
}
if (inv&&n % 2 == 0)result = (long long)n*(n + 1) / 2 - result;
cout << "Case " << ++kcase << ": " << result << endl;
} return 0;
}
**如果数据结构上的某个操作很耗时,有时可以用加标记的方式处理,而不需真的执行那个操作,但同时,该数据结构的所有其他操作都要考虑这个标记。
Boxes in a Line(移动盒子)的更多相关文章
- 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 ...
- 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 ...
- Problem B Boxes in a Line
省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...
- 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 ...
- 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 ...
- 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 ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
- UVA 12657 Boxes in a Line
双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...
- UVa 12657 Boxes in a Line(数组模拟双链表)
题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...
随机推荐
- 特殊函数(__all__)
python里__all__ 属性分别于模块和包之中的用法 一. 在模块(*.py)中使用意为导出__all__列表里的类.函数.变量等成员,否则将导出modualA中所有不以下划线开头(私有)的成员 ...
- CentOS 下安装apt-get
CentOS 下安装apt-get 最近在学习Linux系统时,网上好多帖子都是用apt-get里下载rpm包,于是就在Google上找了个CentOS下安装apt-get的方法 1.下载地址:htt ...
- turn to help
1 'On The Exact Recovery Condition of Simultaneous Orthogonal Matching Pursuit', JF Determe,J Louvea ...
- 最直接的教你OC中Block的简单使用场景
场景一: A控制器跳转到B控制器 -- B控制器事件处理通过Block回调给A控制器 A 跳转前界面如下 点击ToB按钮到控制器B 在控制器B中点击按钮返回到A界面如下 ...
- bigdecimal使用
float和double类型 一般用于科学计算,用于金融的都用bigdecimal类型.在项目中浮点型数据没有指定 默认是double类型.bigdecimal的构造参数有浮点型和String类型.但 ...
- Android非常有用的开源库介绍整理
Android开源库 自己一直很喜欢Android开发,就如博客副标题一样,我想做个好的App. 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者,而且我会 ...
- Maven使用笔记
Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model), 一组标准插件(生命周期各阶段是绑定插件中的目标来完成项目构建工作) 一个项目生命周期(Proje ...
- Sass入门:第三章
1.声明变量 Sass声明变量以美元符号"$"开头.例如: $width : 300px; 上面的例子中,Sass的变量包括三个部分: (1)声明变量的符号"$" ...
- 使用php创建WebSocket服务
执行方法:首先先修改server.php与index.html的ip通过命令行执行 [php路径]php.exe "[文件路径]server.php"然后通过浏览器打开index. ...
- Talking Ben App砸壳记
需求: 导出Talking Ben app的头文件 实施: 1)准备材料: 越狱IOS设备一部,并安装Talking Ben游戏 IOS设备上安装open SSH IOS设备的/usr/bin 中安装 ...