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. ASP.NET MVC5 + EF6 + LayUI实战教程,通用后台管理系统框架(3)

    前言 本节将我们自己的CSS样式替换系统自带的 开始搭建 将脚本文件夹删掉,将内容文件夹里的内容删掉,将我们自己的CSS样式文件,全部复制到内容里边 新建家庭控制器 给家庭控制器添加索引视图 指数代码 ...

  2. mysql中的游标使用

    1.游标的作用及属性 游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作:游标有下面这些属性: a.游标是只读的,也就是不能更新它: b.游标是不能滚动的,也就是只能在一个方向上进 ...

  3. scala 基础

    1.scala一些预热操作 1.1 to 是一个方法,()可以进行 参数传递,map()把每一个元素取出来进行相应的操作,  print(1.to(10).map(_*10))  结果  Vector ...

  4. openCV 调用摄像头

    OpenCV调用摄像头 环境 python:python3.6 摄像头:网络摄像头 Python库:openCV # -*- coding: utf-8 -*- # @author leone # @ ...

  5. cgi、fastcgi及php-fpm分别是什么

    cgi cgi是通用网关接口定义.当web server收到/index.php这个请求后,会启动对应的CGI程序,这里就是PHP的解析器.接下来PHP解析器会解析php.ini文件,初始化执行环境, ...

  6. 关于IOS下click事件委托失效的解决方案

    一.由于某些特殊情况下,需要用到事件委托,比如给动态创建的DOM绑定click事件,这里就需要事件委托(这里就牵扯到:目标元素和代理元素)目标元素:动态创建的元素,最终click事件需要绑定到该元素 ...

  7. sublime text3快速生成html头部信息

    1.在网站开发过程中尤其写前台页面时要写头部很麻烦,怎么办呢?直接生成不更好吗? 这是快速生成的信息 2.方法: ctrl+shift+P打开命令面板 点击安装控制器 3.输入emmet 安装(以下图 ...

  8. 单页面应用(SPA)

    此篇我们来瞅一瞅SPA,啥是SPA啊,实际上一点也不神秘,就是单页应用,可能有的同学又会问了,啥是单页面应用,别着急,我们慢慢来看 首先我们先来了解一下单页应用出现背景 背景: 在早期的 Web 应用 ...

  9. 用 JS 写 (轮播图 / 选项卡 / 滑动门)

    页面中经常会用到各式各样的轮播图,今天贺贺为大家介绍一种常用的方法,对于JS我们需要举一反三,一种方法可以对多个轮播样式进行渲染. <head> <meta charset=&quo ...

  10. Salesforce中的单点登录简介

    单点登录的定义 引自维基百科: 单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自独立的软件系统,提供访问控制的属性.当拥有这项属性时, ...