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 ...
随机推荐
- 利用 java.lang.Runtime.addShutdownHook() 钩子程序,保证java程序安全退出
以前在开发时只知道依靠数据库事务来保证程序关闭时数据的完整性. 但有些时候一个业务上要求的原子操作,不一定只包括数据库,比如外部接口或者消息队列.此时数据库事务就无能为力了. 这时我们可以依靠java ...
- Entity Framework学习 - 1.连接数据库
1.添加项,选择ADO.NET实体数据模型 2.选择从数据库生成 3.建立数据库连接 4.选择要关联的表 点击完成,数据库已连接完毕,并生成了对应的Model
- js、javascript正则表达式验证身份证号码
function isCardNo(card) { // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X var reg = /(^\d{1 ...
- n人比赛,可轮空,比赛轮数和场数
#include<stdio.h> int chang(int x,int s){ ) return s; ) ; !=){ s+=(x-)/; )/,s); } else{ s+=x/; ...
- Qt之进程间通信(TCP/IP)
简述 可以通过Qt提供的IPC使用TCP/IP,使用QtNetwork模块即可实现,TCP/IP在实现应用程序和进程内部通信或与远程进程间的通信方面非常有用. QtNetwork模块提供的类能够创建基 ...
- Sass结合Modernizr的使用方法
Modernizr在初始化的时候会首先找寻class=“no-js”的元素: <!DOCTYPE html> <html class="no-js"> &l ...
- apache开源项目 -- tomee
Apache TomEE,发音是“Tommy”,是一个经Apache.JavaEE6.Web框架认证的适配器,其在Tomcat服务器中是最强大的.Apache TomEE是由香草项目(简化常见编程任务 ...
- matlab中,计算,记录,程序运行,起始,结束 时间,间隔 &matlab中 tic,toc函数的用法
Tic和toc函数可以计算运行一段时间的代码. 例如: clc tic d=zeros(1,10000); for i=1:10000 d(i)=i; end toc tic c=1; for i=1 ...
- Java [leetcode 5] Longest Palindromic Substring
问题描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- zoj 1119 /poj 1523 SPF
题目描述:考虑图8.9中的两个网络,假定网络中的数据只在有线路直接连接的2个结点之间以点对点的方式传输.一个结点出现故障,比如图(a)所示的网络中结点3出现故障,将会阻止其他某些结点之间的通信.结点1 ...