UVa12657 - Boxes in a Line(数组模拟链表)
题目大意
你有一行盒子,从左到右依次编号为1, 2, 3,…, n。你可以执行四种指令:
1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令)。
2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令)。
3 X Y表示交换盒子X和Y的位置。
4 表示反转整条链。
盒子个数n和指令条数m(1<=n,m<=100,000)
题解
用数组来模拟链表操作,对于每个节点设置一个前驱和后继。
1操作是把x的前驱节点和x的后继节点连接,y节点的前驱和x节点连接,x节点和y节点连接。
2,3,的做法和1差不多
4操作由于操作两次就等于没有操作,所以只要判断它最终是不是执行了奇数次,如果是就把n个节点的前驱和后继交换下。还有就是在执行1,2的时候如果之前4操作了奇数次,那么1,2两个执行的操作分别是2操作和1操作。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 111111
typedef long long LL;
int nxt[maxn], pre[maxn];
void init(int n)
{
for (int i = ; i <= n;i++)
{
pre[i] = i - ;
nxt[i] = i + ;
}
nxt[n] = ;
nxt[] = ; pre[] = n;
}
void link(int l, int r)
{
pre[r] = l; nxt[l] = r;
}
int main()
{
int n, m,kase=;
while (scanf("%d%d", &n, &m) != EOF)
{
int rev = ;
init(n);
while (m--)
{
int op, x, y;
scanf("%d", &op);
if (op == ) rev = - rev;
else
{
scanf("%d%d", &x, &y);
if ((op == || op == ) && rev) op = - op;
if (op == &&nxt[y] == x) swap(x, y);
int lx, rx, ly, ry;
lx = pre[x]; rx = nxt[x];
ly = pre[y]; ry = nxt[y];
if (op == )
{
if (nxt[x]==y) continue;
link(lx, rx);
link(ly, x);
link(x, y); }
else if (op == )
{
if (nxt[y]==x) continue;
link(lx, rx);
link(x, ry);
link(y, x);
}
else
{
if (nxt[x] == y)
{
link(lx, y);
link(y, x);
link(x, ry);
}
else
{
link(lx, y);
link(y, rx);
link(ly, x);
link(x, ry);
}
}
}
}
if (rev)
{
for (int i = ; i <= n; i++) swap(pre[i], nxt[i]);
}
int pos;
for (int i = ; i <= n; i++)
{
if (pre[i] == )
{
pos = i;
break;
}
}
int cnt = ;
LL ans = ;
while (pos!=)
{
if (cnt & ) ans += pos;
pos = nxt[pos];
cnt++;
}
printf("Case %d: %I64d\n", ++kase,ans);
}
return ;
}
UVa12657 - Boxes in a Line(数组模拟链表)的更多相关文章
- C - 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 simul ...
- UVA11988-Broken Keyboard(数组模拟链表)
Problem UVA11988-Broken Keyboard Accept: 5642 Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...
- B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 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 ...
- UVA12657 Boxes in a Line:题解
题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...
- 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 ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
- CSUOJ 1329 一行盒子(数组模拟链表)
题目:id=1329">http://acm.csu.edu.cn/OnlineJudge/problem.php? id=1329 题意: watermark/2/text/aHR0 ...
随机推荐
- C++:运算符重载函数之"++"、"--"、"[ ]"、"=="的应用
5.2.5 "++"和"--"的重载 对于前缀方式++ob,可以用运算符函数重载为: ob.operator++() //成员函数重载 或 operator++ ...
- 机器人学 —— 飞行机器人(Introduction)
UPNN课程 aerial robotics 教授: VJ Kummer 1.四旋翼飞行器的控制对象是各个旋翼对应的电机 2.飞行器的能源主要消耗于hovering. 3.飞行器在设计时需要考虑各个 ...
- 转 Android的消息处理机制
来自:http://blog.csdn.net/andyhuabing/article/details/7368217 Windows编程的朋友可能知道Windows程序是消息驱动的,并且有全局的消息 ...
- JDynamic :支持Json反序列化为Dynamic对象
JDynamic :支持Json反序列化为Dynamic对象 2010年 .NET 4.0 发布前后,从3.5向4.0迁移,那时也有一些异构系统的需求,主要是和PHP打交道,通信使用的HTTP 格 ...
- QTP公开课视频-持续更新中。。。
以下是视频的下载地址: http://pan.baidu.com/share/link?shareid=1760499709&uk=3711405498
- ConcurrentDictionary和Dictionary
http://stackoverflow.com/questions/6739193/is-the-concurrentdictionary-thread-safe-to-the-point-that ...
- [HDOJ2795]Billboard(线段树,单点更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:w*h的公告板要贴公告,公告是w*1的,每个公告有先后顺序,要使每个公告贴的位置尽可能地高 ...
- SQL server函数大全
函数类别 作用 聚合函数 执行的操作是将多个值合并为一个值.例如 COUNT.SUM.MIN 和MAX. 配置函数 是一种标量函数,可返回有关配置设置的信息. 转换函数 将值从一种数据类型转换为另一种 ...
- 百度地图api经纬度气死我了!
百度地图api经纬度气死我了! 百度地图官网api中例子的经纬度.我测试了2天才好用.一直是不能用.坑死我了.原来是获取的经纬度.和实际调用的经纬度尽然是反的.调转过来就好用了.气死我了.弄了两天 ...
- servlet应用具体实例
web,xml应用文件 1.<filter>参数 <filter> <filter-name>encodingFilter</filter-name> ...