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 ...
随机推荐
- Zepto源码
// Zepto.js // (c) 2010-2016 Thomas Fuchs // Zepto.js may be freely distributed under the MIT licens ...
- VS2013使用winsock.h和winsock2.h发生冲突后的终极解决方法
问题:彻底无语了,不小心某个文件包含了windows.h头文件,而windows.h文件里面包含着winsock.h文件, 如果你下次使用winsock2.h文件时,位置不对,然后编译器会给你一大堆重 ...
- java之注解Annotation
元注解:负责注解其他注解,java5提供的4个meta-annotation元注解 @Target 规定注解修饰的范围 ElementType.CONSTRUCTOR:构造器声明 ElementTyp ...
- oneCMDB
OneCMDB开源地址: http://www.oschina.net/p/onecmdb/, 官方网站:http://www.onecmdb.org/wiki/index.php?title=Mai ...
- hadoop单机and集群模式安装
最近在学习hadoop,第一步当然是亲手装一下hadoop了. 下面记录我hadoop安装的过程: 注意: 1,首先明确hadoop的安装是一个非常简单的过程,装hadoop的主要工作都在配置文件上, ...
- javascript 函数执行上下文
在js里,每个函数都有一个执行的上下文,我们可以通过this来访问. 如: 全局函数 function test(){ var local = this; } 我们发现local等于window(do ...
- Linux内核分析第二周学习总结:操作系统是如何工作的?
韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.函数调用堆栈 ...
- expdp\impdp及exp\imp
数据泵文件 expdp介绍 EXPDP命令行选项1. ATTACH该选项用于在客户会话与已存在导出作用之间建立关联.语法如下ATTACH=[schema_name.]job_nameSchema_na ...
- android通过gradle打包
这里是最简单的打包方法,实际上gradle的语法是groovy,可以通过编写脚本实现更智能的构建,这个我还不懂==,等我学习了解后,单独整理一个gradle的随笔,这里先应付打包吧 环境要求 安装 ...
- asp.net Gridview 的用法
留个档. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...