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(数组模拟双链表)的更多相关文章

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

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

  2. 数组模拟双链表,你get到了吗?

    数组模拟双链表 通过前面的学习我们知道单链表是单个指针指向操作,那么通过类比我们可以把指针设定为两个,并且让它们分别指向前后数据,这就是"双向链表".使用这种链表,不仅可以从前往后 ...

  3. UVa 12657 Boxes in a Line(数组模拟双链表)

    题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...

  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. Problem B Boxes in a Line

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

  6. 【数组模拟的链表or复杂模拟】PAT-L2-002. 链表去重

    L2-002. 链表去重 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点.即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留.同时,所有被删除的结点必须 ...

  7. UVA12657 Boxes in a Line:题解

    题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...

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

  9. 数组模拟单向链表例题(UVa11988)

    指针的链表实现方式是,当前节点的next指向下一个节点,用数组模拟就是 for(int i=next[0];i!=0;i=next[i]) i=next[i]:就是一条链. 例题: 你有一个破损的键盘 ...

随机推荐

  1. 基于vue框架手写一个notify插件,实现通知功能

    简单编写一个vue插件,当点击时触发notify插件,dom中出现相应内容并且在相应时间之后清除,我们可以在根组件中设定通知内容和延迟消失时间. 1. 基础知识 我们首先初始化一个vue项目,删除不需 ...

  2. c# 数字之间的计算

    1.c# 65*1.0F/102=0.637254901960784 2.mysql TRUNCATE(65/102,9)  9代表的是保留几位小数 3.多张表关联,修改某张表的字段 UPDATE s ...

  3. 【Java并发编程】22、Exchanger源码解析(JDK1.7)

    Exchanger是双向的数据传输,2个线程在一个同步点,交换数据.先到的线程会等待第二个线程执行exchangeSynchronousQueue,是2个线程之间单向的数据传输,一个put,一个tak ...

  4. 【Spring】20、使用TransactionSynchronizationManager在spring事务提交之后进行一些操作。

    本文内容 如何在spring事务提交之后进行一些操作,这些操作必须得在该事务成功提交后才执行,回滚则不执行. 要点 如何在spring事务提交之后操作 如何在spring事务回滚之后操作 实现方案 使 ...

  5. Java 10新特性

    ref:http://www.cocoachina.com/industry/20180309/22520.html https://www.oschina.net/news/94402/java-1 ...

  6. 7种html5css3网页图片展示特效代码

    鼠标拖拽图片渐变透明切换特效 mobile手机左右滑动切换幻灯片 游戏透明提示图层幻灯片特效 可以编辑滚动条灯片颜色的scroll插件 几种文字动画显示插件代码 360度背景图片旋转的css3动画 左 ...

  7. homebrew 安装 formula 的不同历史版本——以安装 node 为例

    homebrew 安装 formula 的不同历史版本--以安装 node 为例 系统环境 macOS Mojave 10.14 Homebrew 1.8.0 Homebrew/homebrew-co ...

  8. python之装饰器(函数)

    1. 装饰器 遵循的原则: 开闭原则:   对功能的扩展开放 对代码的修改是封闭 # 通用装饰器写法 # 存在的意义: 在不破坏原有函数和原有函数调用的基础上,给函数添加新的功能. def wrapp ...

  9. HTTP协议web开发知识点

    HTTP协议 HTTP协议简介 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数 ...

  10. loadrunner 场景设计-制定负载测试计划

    by:授客 QQ:1033553122 场景设计-制定负载测试计划 步骤1.分析应用程序 你应该对硬件和软件组建,系统配置和典型的使用场景很熟悉.这些应用程序的分析保证你在使用loadrunner进行 ...