题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066

利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际的链表对一个数字进行定位需要扫一遍,时间复杂度难以承受,因此使用数组模拟双向链表。

易错点:1.要对特殊位置进行处理,例如xy相邻的情况

2.注意链表头和尾可能在任意一个操作中变化,要进行检查

#include <bits/stdc++.h>
using namespace std;
const int Max=1e5+;
typedef long long LL;
#define scan(x) scanf("%d",&x);
struct node
{
int data,p[];
}lists[Max];
int n,m,head,tail,swi; //!swi--left,swi--right
int lm,rm;
int Scan()
{
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
}
inline void index(int x,int &x1,int &x2,int &x3)
{
x2=x;
x1=lists[x2].p[!swi];
x3=lists[x2].p[swi];
}
void check(int x)
{
if(x<||x>n) return;
if(lists[x].p[!swi]==lm) head=x;
if(lists[x].p[swi]==rm) tail=x;
}
void movesl(int x,int y)
{
int x1,x2,x3,y1,y2,y3;
index(x,x1,x2,x3);index(y,y1,y2,y3);
if(x1==y2) return;
lists[y3].p[!swi]=y1; lists[y1].p[swi]=y3;
lists[y2].p[!swi]=x1;lists[x1].p[swi]=y2;
lists[y2].p[swi]=x2;lists[x2].p[!swi]=y2;
check(y1);check(x1);check(x3);check(x2);check(y2);check(y3);
}
void movesr(int x,int y)
{
int x1,x2,x3,y1,y2,y3;
index(x,x1,x2,x3);index(y,y1,y2,y3);
if(x3==y2) return;
lists[y3].p[!swi]=y1;
lists[y1].p[swi]=y3;
lists[y2].p[!swi]=x2;lists[x2].p[swi]=y2;
lists[y2].p[swi]=x3;lists[x3].p[!swi]=y2;
check(y1);check(x1);check(x3);check(x2);check(y2);check(y3);
}
void swaps1(int x1,int x,int y,int y1)
{
lists[x].p[!swi]=y;
lists[x].p[swi]=y1;
lists[y].p[!swi]=x1;
lists[y].p[swi]=x;
lists[x1].p[swi]=y;
lists[y1].p[!swi]=x;
check(x1);check(x);check(y);check(y1);
}
void swaps(int x,int y)
{
int x1,x2,x3,y1,y2,y3;
index(x,x1,x2,x3);index(y,y1,y2,y3);
if(x3==y2) {swaps1(x1,x2,y2,y3);return;}
if(y3==x2) {swaps1(y1,y2,x2,x3);return;}
lists[x2].p[!swi]=y1;lists[x2].p[swi]=y3;
lists[x1].p[swi]=y2;lists[x3].p[!swi]=y2;
lists[y1].p[swi]=x2;lists[y3].p[!swi]=x2;
lists[y2].p[!swi]=x1;lists[y2].p[swi]=x3;
check(x2);check(y2);
}
void reverses()
{
swap(head,tail);
swi=!swi;
swap(lm,rm);
}
LL cacu()
{
int g=;
LL ans=;
for(int i=head;g<n&&i>=&&i<=n;i=lists[i].p[swi])
{ g++;
if(g%)
{
ans+=i;
}
}
return ans;
}
void open()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
int main()
{
// open();
int T=;
while(~scanf("%d%d",&n,&m))
{
swi=;
int x;
memset(lists,,sizeof(lists));
for(int i=; i<=n; i++)
{
lists[i].data=i;
lists[i].p[!swi]=i-;
lists[i].p[swi]=i+;
}
head=;tail=n;lm=;rm=n+;
int op,y;
for(int i=; i<=m; i++)
{
op=Scan();
if(op!=)
{
x=Scan();y=Scan();
if(x<||x>n||y<||y>n) continue;
if(x==y) continue;
}
switch(op)
{
case :
movesl(y,x);
break;
case :
movesr(y,x);
break;
case :
swaps(x,y);
break;
case :
reverses();break;
}
}
LL ans=,ans1;
ans=cacu();
printf("Case %d: %lld\n",++T,ans);
}
return ;
}

UVA 12657 Boxes in a Line 双向链表的更多相关文章

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

  2. UVA 12657 Boxes in a Line(双向链表+小技巧)

    题意:对于一行按照顺序排列盒子数字与位置都为 1,2,3,4....n 执行四种操作 c = 1    x 放到 y 的左边 c =2     x 放到 y 的右边 c =3 交换 x, y c =4 ...

  3. UVA 12657 Boxes in a Line

    双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...

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

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

  5. 2019 SCUT SE 新生训练第四波 L - Boxes in a Line——双向链表

    先上一波题目 https://vjudge.net/contest/338760#problem/L 这道题我们维护一个双向链表 操作1 2 3 都是双向链表的基本操作 4操作考虑到手动将链表反转时间 ...

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

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

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

  9. 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 sim ...

随机推荐

  1. AIDL学习

    (转自)可以参见:http://www.2cto.com/kf/201406/312244.html 1.为什么要有AIDL? 无论学什么东西,最先得弄明白为什么要有这个东西,不要说存在即是合理,存在 ...

  2. (知识分享)软硬件调试九法:第九条规则 如果你不修复一个bug,它将永远 存在

    1.查证问题确已被修复 如果遵循了“制造失败”这条规则,就知道如何验证你确实修复了问题.无论问题和修复看起来多么明显,你都无法保证修复是有效的,直到做了测试并验证. 2.查证确实你的修复措施解决了问题 ...

  3. Python自动化 【第九篇】:Python基础-线程、进程及python GIL全局解释器锁

    本节内容: 进程与线程区别 线程 a)  语法 b)  join c)  线程锁之Lock\Rlock\信号量 d)  将线程变为守护进程 e)  Event事件 f)   queue队列 g)  生 ...

  4. Android之使用Android-AQuery异步加载图片(一)

    第一节:转载地址(http://www.cnblogs.com/lee0oo0/archive/2012/10/25/2738299.html) // 必须实现AQuery这个类 AQuery aq ...

  5. CSS背景图拉伸自适应尺寸,全浏览器兼容

    突然有人问我这个问题,说网上CSS filter的方法在非IE浏览器下不奏效.思考之后,问题之外让我感慨万千啊,很多我们所谓的难题,都会随着时代的发展迎刃而解,或被新的问题所取代. 当CSS背景图片拉 ...

  6. 安装mysql5.5时候的报错解决办法:

    每次安装mysql5.5的时候总会报出一下错误: -- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_DIR) ...

  7. json传值以及ajax接收

    一开始进入公司,做起项目来比较不知所措,现在一个月过去了,越来越得心应手,下面是json向前端传值以及前端ajax接收,给自己记下也分享给大家. 这是两个类型不同的json与ajax的数据交互(集合. ...

  8. Bridge(桥接)-对象结构型模式

    1.意图 将抽象部分与它的实现部分分离,使它们都可以独立地变化. 2.动机 在抽象类与它的实现之间起到桥梁作用,使它们可以独立地变化. 3.适用性 不希望在抽象和它的实现部分之间有一个固定的绑定关系. ...

  9. DataGridView的DataGridViewComboBoxColumn列在编辑时自动弹出下拉列表

    在DataGridView的CellEnter的事件中添加如下代码即可: if (e.ColumnIndex == dataGridView1.Columns["仓库名"].Ind ...

  10. 爬虫--scrapy--windows上安装

    关于scrapy的安装网上有一大把教程,但是比较麻烦,各种包----------,这里给大家介绍一款神器: 下载地址:http://continuum.io/downloads 根据自己电脑的系统选择 ...