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]分别表示 ...
随机推荐
- Redis集合相关命令
1.无序集合集合的特性:①确定性②互异性③无序性redis的set是string类型的无序集合set元素最大可以包含(2^32-1)个元素 sadd key value1....valueN 将将元素 ...
- js 处理数据里面的空格
str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...
- SQL 范式(转载)
装载于"http://www.cnblogs.com/KissKnife/ 理论性的东西,往往容易把人人都看得懂的东西写成连鬼都看不懂,近似于主任医生开的药方.从前学范式的时候,把书中得概念 ...
- filter过滤action的问题
今天犯了一个错误,结果白白浪费了半个下午的时间,特记于此. filter过滤Action的时候,要把过滤器配置在Struts2拦截器的前面,这样过滤器才能过滤到Action,否则不可以.
- Ray Tracing
Ray Tracing 题目链接:http://codeforces.com/problemset/problem/724/C 拓展欧几里得 //为什么这次C题这么难啊=.= 可以观察到,光线在矩形中 ...
- HDU 1532 最大流入门
1.HDU 1532 最大流入门,n个n条边,求第1点到第m点的最大流.只用EK做了一下. #include<bits/stdc++.h> using namespace std; #pr ...
- iOS开发打电话的功能
1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 NSMutableString * phoneStr=[[NSMutableString alloc] init ...
- PAT 团体程序设计天梯赛-练习集 L1-005. 考试座位号
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
- Sass入门:第二章
1.Sass语法格式 假设有这样一段CSS代码: body{ font : 100% Helvetica , sans-serif; color : #333; } Sass最初的语法格式 $font ...
- NSCondition
一.NSCondition是对条件变量和互斥量的一个封装,用于线程之间的同步. 其中的互斥量用于保护对条件变量的修改,条件变量变化以信号量的方式通知其它线程实现线程之间的同步. 二.NSConditi ...