Uva12657 Boxes in a Line
题目链接:传送门

分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了.
不过这样有一个问题:第4个操作似乎无法很高效地进行,如果真的按照它说的那样模拟翻转,恐怕复杂度和暴力也要差不多了,那么我们怎么处理呢?
观察发现题目只让我们输出奇数位置的编号和,也就是说,如果题目给定的序列长度就是奇数的,那么无论用多少次4操作都没有影响,否则4的次数是奇数,则用总和减去当前奇数位置的和就是答案。这就相当于线段树中的lazy标记的做法,现在要考虑这个标记对其它操作的影响,显然对3操作是没有任何影响的,而对1,2操作无非就是把1变成了2,把2变成了1,这样就很快地就能解决问题了。
如果数据结构上的某一个操作很耗时,有时可以用加标记的方式处理,而不需要真的执行那个操作。但同时,该数据结构的所有其他操作都要考虑这个标记!
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int maxn = ; int n, m, kase, l[maxn], r[maxn],flag;
long long ans; int main()
{
while (scanf("%d%d", &n, &m) == )
{
for (int i = ; i <= n; i++)
{
l[i] = i - ;
r[i] = (i + ) % (n + );
}
l[] = n;
r[] = ;
flag = ;
for (int i = ; i <= m; i++)
{
int op,x,y;
scanf("%d", &op);
if (op == )
flag = (flag + ) % ;
else
{
scanf("%d%d", &x, &y);
if (op != && flag)
op = - op;
if (op == && l[y] == x)
continue;
if (op == && r[y] == x)
continue; if (op == )
{
l[r[x]] = l[x];
r[l[x]] = r[x];
r[l[y]] = x;
r[x] = y;
l[x] = l[y];
l[y] = x;
}
else
if (op == )
{
l[r[x]] = l[x];
r[l[x]] = r[x];
l[x] = y;
r[x] = r[y];
l[r[y]] = x;
r[y] = x;
}
else
if (op == )
{
if (r[x] == y)
{
r[l[x]] = y;
l[y] = l[x];
r[y] = x;
l[x] = y;
r[x] = r[y];
l[r[y]] = x;
}
else
{
r[l[y]] = x;
l[r[y]] = x;
r[l[x]] = y;
l[r[x]] = y;
swap(l[x], l[y]);
swap(r[x], r[y]);
}
}
}
int cur = ;
ans = ;
for (int i = ; i <= n; i++)
{
cur = r[cur];
if (i % == )
ans += cur;
}
if (flag && n % == )
ans = (long long)n * (n + ) / - ans;
}
printf("Case %d: %lld\n", ++kase, ans);
} return ;
}
Uva12657 Boxes in a Line的更多相关文章
- 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 ...
- UVA12657 Boxes in a Line:题解
题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
- UVA-12657 Boxes in a Line (双向链表)
题目大意:一个1~n的升序数字序列,有4种操作.操作1,将x放到y前面一个位置:操作2将x放到y后面的一个位置:操作3交换x和y的位置:操作4反转整个序列.求经过m次操作后的所有奇数项的和. 题目分析 ...
- Problem B Boxes in a Line
省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】
枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...
- bzoj 1691: [Usaco2007 Dec]挑剔的美食家【贪心+splay】
高端贪心,好久没写splay调了好久-- 以下v为价格,w为鲜嫩度 把牛和草都按v排升序,扫草,首先把v小于等于当前草的牛都丢进splay,这样一来splay里全是可选的牛了,按w排序,然后贪心的为当 ...
- [Swift通天遁地]一、超级工具-(11)使用EZLoadingActivity制作Loading加载等待动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- taro.js & dva 脚手架搭建及常见问题
## taro.js & dva 脚手架 ### 启动 npm install -g @tarojs/cli // 全局安装taro-cli npm i npm run dev:weapp / ...
- java Class.getResource和ClassLoader.getResource
http://www.cnblogs.com/wang-meng/p/5574071.html http://blog.csdn.net/earbao/article/details/50009241 ...
- fiddler不同代理模式的区别
Fiddler有不同的代理模式,分为以下两种: 流模式(Streaming)和缓冲模式(Buffering). 流模式可以理解为一种实时通信的模式,有请求就有返回,也就是实时返回. 缓冲模式是等所有请 ...
- Unity笔记(3)自学第三天
学习记录: 脚本使用:
- 介绍Git的17条基本用法
本文将介绍Git的17条基本用法.本文选自<Python全栈开发实践入门>. 1.初始化Git仓库 Git仓库分为两种类型:一种是存放在服务器上面的裸仓库,里面没有保存文件,只是存放.gi ...
- QT开发之旅-Udp聊天室编程
一.概要设计 登录对话框(继承自QDialog类)进行用户登录查询数据库用户是否存在,注册插入数据到用户表.用户表字段: (chatid int primary key, passwd varchar ...
- python学习笔记(3)——进制符号&转换公式
进制转换法则: 进制符号 bin().oct().hex().int('',进制)+待转格式数 10进制→其他进制 # dec2bin # 十进制 to 二进制: bin() >>> ...