题目连接: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. Python:if __name__ == '__main__'

    很多模块里都会看到这句话,一般用于模块自测时使用. 所有的模块都有一个内置属性 __name__. 一个模块的 __name__ 的值取决于您如何应用模块. 一个Python文件有两种使用方式,直接使 ...

  2. nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件

    目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...

  3. Spark Streaming 事务处理彻底掌握

    本期内容: 1. Exactly once容错 2. 数据输出不重复 一. 事务场景 : 以银行转帐一次为例,A用户转账给B用户,如何保证事务的一致性,即A用户能够转出且只能转出一次,B用户能够收到且 ...

  4. [电脑常见问题] win8 ie浏览器打不开

    我安装的是win8专业版,正版的已经激活了,突然IE浏览器就打不开了,在桌面里面点IE没反应,在Metro界面点IE就回到开始界面 解决办法: 1.Win+R呼出运行窗口,键入Regedit,回车,打 ...

  5. MFCButton Memory leak(内存泄露问题)

    http://m.blog.csdn.net/blog/haoekin/8851219 1.无法显示右边箭头的问题 无论怎么折腾都没显示不出来,微软给的示例又能显示,度娘和谷歌也都不知道,经过不断地探 ...

  6. Ubuntu14.04通过pyenv配置多python

    参考链接: https://github.com/yyuu/pyenv-virtualenv https://github.com/yyuu/pyenv http://seisman.info/pyt ...

  7. 去掉hive字段中的tab

    去除空格用trim 去除tab用如下方法 select regexp_replace(secdomainname,'\\s+','') from dwb_cndns_node_secdomain_d ...

  8. Linxu学习之03_LInux文件与目录管理

    同样只介绍相关命令 这节相关主要的命令有这些: 1.目录的相关操作 cd----切换目录 pwd----显示当前目录 mkdir----新建一个新的目录 rmdir----删除一个空的目录

  9. random、面向对象编程

    一.random模块:随机数 import random print random.random() print random.randint(,) print random.randrange(,) ...

  10. DHCP服务器的开始方式

    方法一:采用DHCP服务器接口开启的方式 [Huawei]dhcp enable [Huawei]int g0/0/0[Huawei-GigabitEthernet0/0/0]ip add 192.1 ...