UVA 12657 Boxes in a Line 双向链表
题目连接: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 双向链表的更多相关文章
- 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 ...
- 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 ...
- UVA 12657 Boxes in a Line
双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...
- UVa 12657 Boxes in a Line(数组模拟双链表)
题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...
- 2019 SCUT SE 新生训练第四波 L - Boxes in a Line——双向链表
先上一波题目 https://vjudge.net/contest/338760#problem/L 这道题我们维护一个双向链表 操作1 2 3 都是双向链表的基本操作 4操作考虑到手动将链表反转时间 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- springMVC使用与生成序列号
springMVC使用与生成序列号 我是以springMVC的方式提供序列号 代码可以直接在项目中用 第一步:controller类 @Autowired private PkGenerator pk ...
- 使用虚幻引擎中的C++导论(一-生成C++类)
使用虚幻引擎中的C++导论(一) 第一,这篇是我翻译的虚幻4官网的新手编程教程,原文传送门,有的翻译不太好,但大体意思差不多,请支持我O(∩_∩)O谢谢. 第二,某些细节操作,这篇文章省略了,如果有不 ...
- Inside The C++ Object Model - 03
object Lessons 1.C++中布局以及存取时间上的的额外负担是由virtual引起的:virtual function.virtual base class.或是由于多继承引起的. 2.C ...
- 【229】Raster Calculator - 栅格计算器
参考:分段函数进行复制,利用语句 参考:ArcGIS栅格计算器 - CSDN 参考:ArcGIS栅格计算器con条件函数使用 参考:ArcGIS栅格计算器 - 电脑玩物 ("lyr" ...
- javascript中document.appendChild和document.body.appendChild的问题
在IE7中 var conentDiv = document.createElement("div"); document .body .appendChild(conentDiv ...
- 循序渐进Python3(二) -- 数据类型
数据类型 一.数字(int) Python可以处理任意大小的正负整数,但是实际中跟我们计算机的内存有关,在32位机器上,整数的位数为32位,取值范围为 -2**31-2**31-1,在64位系统上,整 ...
- sql left join、right join、inner join
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- [转]Excel 取汉字拼音首位
转自:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html 转自:http://jingyan.baidu.com/articl ...
- Solaris 命令 小结
Solaris 命令 小结 prstat -a 系统进程监控 Solaris 10默认的shell是sh,可以改成bash #useradd -m -d /home/dave dave -s /bin ...
- JavaScript 中2个等号与3个等号的区别
首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 下面分别说明: 先说 === ...