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 颠倒链
最后求出奇数位置的数的总和
题解:直接维护无论如何每次每次都需要维护一段区间的位置,因此不去看位置、只需要知道每个盒子左边是哪个盒子右边是哪个盒子
这样就直接使用双向链表维护,接着颠倒时注意只是标记就好
最后注意几个细节:
首先颠倒后1与2的交换需要互换;
维护链表时可以提取出一个函数,每次都只需要多次使用这个函数;
c等于1,2,3时x与y的位置相邻时会出现很大的问题
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const ll INF=1LL<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
struct node
{
int left,right;
} ball[Max];//**表示i这个数字左右两边的数字**
void initPosition(int n)//双向链表初始化
{
for(int i=; i<=n; ++i)
{
ball[i].left=i-;
ball[i].right=(i+)%(n+);
}
}
void link(int l,int r)//左右相互连接
{
ball[l].right=r;
ball[r].left=l;
}
inline void Solve(int type,int &mark)
{
if(type==)
{
mark=(mark^);
return;
}
if(type!=&&!mark)
type=-type;//注意反转后只需交换type的1与2就好
int x,y;
scanf("%d %d",&x,&y);
int Lx=ball[x].left;
int Rx=ball[x].right;
int Ly=ball[y].left;
int Ry=ball[y].right;
if(type==&&Rx==y||type==&&Lx==y)
return;
if(type==)//通过下标找到x、y,将x放在y的左边
{
link(Lx,Rx);//**找到6个交换的规律**
link(Ly,x);
link(x,y);
}
else if(type==)
{
link(Lx,Rx);
link(x,Ry);
link(y,x);
}
else if(type==)
{
if(Rx==y)//注意
{
link(Lx,y);
link(y,x);
link(x,Ry);
}
else if(Lx==y){
link(y,Rx);
link(Ly,x);
link(x,y);
}
else
{
link(y,Rx);
link(Ly,x);
link(Lx,y);
link(x,Ry);
}
}
}
ll Print(int n,int mark)
{
ll res=0LL;
int start=;
for(int i=; i<=n; ++i)
{
if(ball[i].left==)
{
start=i;
break;
}
}
int now=start;
int coun=;
while(now)
{
res+=now;
now=ball[now].right;
if(now)
now=ball[now].right;
}
if(!(n&)&&!mark)
res=(ll)n*(n+)/-res;
return res;
}
int main()
{
int n,m;
int coun=;
while(~scanf("%d %d",&n,&m))
{
initPosition(n);
int mark=;//标记是否反转
while(m--)
{
int type;
scanf("%d",&type);
Solve(type,mark);
}
printf("Case %d: %lld\n",++coun,Print(n,mark));
}
return ;
}
UVA 12657 Boxes in a Line(双向链表+小技巧)的更多相关文章
- UVA 12657 Boxes in a Line 双向链表
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为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 ...
- 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 ...
随机推荐
- Yii框架2.0的过滤器
过滤器是 控制器 动作 执行之前或之后执行的对象. 例如访问控制过滤器可在动作执行之前来控制特殊终端用户是否有权限执行动作, 内容压缩过滤器可在动作执行之后发给终端用户之前压缩响应内容. 过滤器可包含 ...
- golang的指针和切片
首先为什么要讲go的指针和切片放在一起? 因为go指针和切片都是引用类型 引用类型就是说切片和指针保存的只是内存的地址,而不是具体的值,效率在大数据读取方面效率会高很多. 1.怎么定义一个切片 方法1 ...
- Azkaban简介及使用
一.Azkaban概述 Azkaban是一个分布式工作流管理器,在LinkedIn上实现,以解决Hadoop作业依赖性问题. 我们有需要按顺序运行的工作,从ETL工作到数据分析产品. 特点: 1)给用 ...
- react 将字符串解析为markdown
react 中有许多组件支持把字符串转化为markdown,并以html的形式展示出来.这里我试用了 react-markdown 和 hyperdown 两个库,发现hyperdown比较好,因为r ...
- Python 之父谈放弃 Python:我对核心成员们失望至极!
Python 之父讲述退位原因,以及 Python 的未来将何去何从. 在 Python 社区,Python 的发明者 Guido Van Rossum 被称为 “仁慈的终生独裁者”(BDFL,B ...
- mysql应用实例
目录: 表结构 sql练习 1.表结构 SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- T ...
- abap 开发之创建表维护生成器
在sap开发中有时需要对一些自建表维护数据,但又不想写程序,怎么办呢??这个时候我们可以直接生成个表维护生成器,为其定义一个事物码就ok了.以下是表格维护生成器的生成步骤. 首先我们需要先定义表.输入 ...
- Spark2.0 协同过滤推荐
ALS矩阵分解 http://blog.csdn.net/oucpowerman/article/details/49847979 http://www.open-open.com/lib/view/ ...
- UVA+POJ中大数实现的题目,持续更新(JAVA实现)
UVA10494:If We Were a Child Again 大数除法加取余 import java.util.Arrays; import java.util.Scanner; import ...
- R语言之多重共线性的判别以及解决方法
多重共线性(Multicollinearity)是指线性回归模型中的解释变量之间由于存在精确相关关系或高度相关关系而使模型估计失真或难以估计准确. 1.可以计算X矩阵的秩qr(X)$rank,如果 ...