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 ...
随机推荐
- django若干问题
1.使用post方式 在views.py里要出发post请求的函数前加入@csrf_exempt ,之前要引入from django.views.decorators.csrf import csrf ...
- xml 中转意字符&\/使用方法
所有 XML 元素都须有关闭标签 在 HTML,经常会看到没有关闭标签的元素: <p>This is a paragraph <p>This is another paragr ...
- COOKIE&&SESSION
---------------------------------------------------------------------------COOKIE------------------- ...
- 关于对 maximio平台的五个常用类的初步理解及总结
AppBean:绑定应用的默认Bean类,控制主对象集/单个对象,和ui关联的类 继承:DataBean DataBean:任何对象集/单个对象,在ui端处理类. 1.在对象层:写一个Fld类,调用构 ...
- 搭建nexus后,进入首页的时候出现warning: Could not connect to Nexus.错误
nexus出现这种问题,一般是版本太旧,换一个高版本的nexus就能解决了.
- java 常见关键字的使用
Super 关键字:指向父类对象的引用空间. 作用:1.当子类和父类存在同名的成员变量时,可以通过super来调用父类的成员变量. 2.super可以用来调用父类的构造方法. Instanceof 关 ...
- iOS nib file owner
nib文件中的file owner属性,设定后app在运行时加载nib文件的过程中会通过file owner重新建立nib文件中描述的控件与其在file owner中对应的IBOutlet或IBAct ...
- css绘制特殊图形基础
1.等腰三角形 .isosceles{ width:; height:; border:30px solid; border-left-color: transparent; border-right ...
- 循序渐进Python3(四) -- 装饰器、迭代器和生成器
初识装饰器(decorator ) Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法 ...
- Prefab强制使用文本模式
[Prefab强制使用文本模式] Edit -> ProjectSetting -> Editor: