Problem UVA12657-Boxes in a Line(数组模拟双链表)
Problem UVA12657-Boxes in a Line
Accept: 725 Submit: 9255
Time Limit: 1000 mSec
Problem 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 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
题解:数组模拟双链表。虽然是模拟,但是操作起来并不是很简单,应用双链表比较自然,困难的是中间的各种修改,我的第一份代码用的是学C语言的时候类似指针的操作,什么pre的next是x的next,next的pre是什么什么之类的,这种操作比较容易错的就是顺序问题,一旦顺序搞错,信息就会丢失,然后就不知道连到哪了,紫书上的方法十分优秀,提前先记录下来前驱、后继,这样不会丢失信息,顺序什么的就不用考虑了,然后把连边的操作封装到函数里,这样更是简化了思考,或者说是缩短了思维长度,经过这两个操作,原来十分费劲的修改关系就变得几乎是无脑操作了,这么好的方法一定要学会。
代码完全是按照紫书写的......
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef long long LL; const int maxn = +;
int Next[maxn],Pre[maxn];
int cnt = ; void Link(int p,int n){
Next[p] = n,Pre[n] = p;
} int main()
{
//freopen("input.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i = ;i <= n;i++){
Next[i] = (i+)%(n+);
Pre[i] = i-;
}
Next[] = ,Pre[] = n;
int x,y,ope,inv = ;
for(int i = ;i <= m;i++){
scanf("%d",&ope);
if(ope == ) inv = !inv;
else{
scanf("%d%d",&x,&y);
if(inv && (ope== || ope==)) ope = -ope;
if(ope== && Pre[y]==x) continue;
if(ope== && Pre[x]==y) continue;
if(ope== && Next[y]==x) swap(x,y);
int nx = Next[x],px = Pre[x];
int ny = Next[y],py = Pre[y];
if(ope == ){
Link(px,nx);Link(py,x);Link(x,y);
}
if(ope == ){
Link(px,nx);Link(y,x);Link(x,ny);
}
if(ope == ){
if(Next[x]==y){
Link(px,y);Link(y,x);Link(x,ny);
}
else{
Link(px,y);Link(y,nx);
Link(py,x);Link(x,ny);
}
}
}
}
LL ans = ;
int t = Next[];
for(int i = ;i <= n;i++){
if(i% == ) ans += t;
t = Next[t];
}
if(inv && n%==) ans = 1LL*n*(n+)/-ans;
printf("Case %d: %lld\n",cnt++,ans);
}
return ;
}
Problem UVA12657-Boxes in a Line(数组模拟双链表)的更多相关文章
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
- 数组模拟双链表,你get到了吗?
数组模拟双链表 通过前面的学习我们知道单链表是单个指针指向操作,那么通过类比我们可以把指针设定为两个,并且让它们分别指向前后数据,这就是"双向链表".使用这种链表,不仅可以从前往后 ...
- UVa 12657 Boxes in a Line(数组模拟双链表)
题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...
- 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 ...
- Problem B Boxes in a Line
省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...
- 【数组模拟的链表or复杂模拟】PAT-L2-002. 链表去重
L2-002. 链表去重 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点.即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留.同时,所有被删除的结点必须 ...
- UVA12657 Boxes in a Line:题解
题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...
- 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 ...
- 数组模拟单向链表例题(UVa11988)
指针的链表实现方式是,当前节点的next指向下一个节点,用数组模拟就是 for(int i=next[0];i!=0;i=next[i]) i=next[i]:就是一条链. 例题: 你有一个破损的键盘 ...
随机推荐
- Trie(字典树)解析及其在编程竞赛中的典型应用举例
摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...
- Linux中重命名乱码文件
Linux下,如何将一个乱码的文件进行重命名 方法一: 命令格式:mv $(ls |egrep "[^a-zA-Z0-9.-]") tandao.tx [root@nb o]# l ...
- ModBus通信协议的【功能码】
Modbus的功能码 ModBus 功能码与数据类型对应表: RTU 方式读取整数据的例子: 解析一下:主机发送指令,访问从站地址为1,使用功能码03(读保持寄存器),起始地址高8位.低8位 ...
- c#方法转异步例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【Linux】CentOS7 安装rabbitmq
[1.安装erlang环境]yum install http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64. ...
- 【Java并发编程】19、DelayQueue源码分析
DelayQueue,带有延迟元素的线程安全队列,当非阻塞从队列中获取元素时,返回最早达到延迟时间的元素,或空(没有元素达到延迟时间).DelayQueue的泛型参数需要实现Delayed接口,Del ...
- Linux下编译、链接和装载
——<程序员的自我修养>读书笔记 编译过程 在Linux下使用GCC将源码编译成可执行文件的过程可以分解为4个步骤,分别是预处理(Prepressing).编译(Compilation). ...
- Oracle总结一
1 数据库相关概念 1.1 数据 数据是描述事物的符号,它有多种表现形式:文本,图形,音频,视频.计算机处理数据的基本单位是字节. 1.2 数据库(Database, 简称DB) 同粮库,车库类似,数 ...
- Dockerfile 中 RUN, CMD, ENTRYPOINT 的区别
RUN 指令:用于指定 docker build 过程中要运行的命令. 语法格式: RUN <command> 或 RUN ["<executeable>" ...
- JavaScript基础要点
一.值和类型及运算 JavaScript中的六种基本值类型 数字(number).字符串(string).布尔值(boolean).对象(object).函数(function).未定义类型(unde ...