题目链接:https://www.luogu.org/problemnew/show/UVA12657

分析:

此题使用手写链表+模拟即可。(其实可以用list,而且更简便,但是会大大的超时)

肯定是不能直接用数组模拟了,因为n,m的大小会达到100000. 然后,

1.可以编写一些辅助函数来设置链接关系。

2.注意 op==3的时候,要对xy相邻的情况进行特判,因为有这种情况

2 1 (头节点)

3 1 2 (尾节点)

3.我们会发现如果反转两次,就相当于没有翻转。如果翻转一次,op=1变为op=2;op=2变为op=1;如果翻转一次,n为奇数时,奇数位置不变,但是n为偶数的时候,奇数变偶数。

(为什么要用双向链表?因为我们需要知道它左边和右边)

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
int left[maxn],right[maxn],inv;
void link(int L,int R)
{
left[R]=L;
right[L]=R;
}
long long number(int n)
{
int x=0;
long long total=0;
for(int i=1;i<=n;i++)
{
x=right[x];
if(i%2!=0)total+=x;
}
if(inv!=0&&n%2==0)total=(long long)n*(n+1)/2-total;
return total;
}
int main()
{
int n,m,T=1;
while(scanf("%d %d",&n,&m)==2)
{
for(int i=1;i<=n;i++)
{
left[i]=i-1;
right[i]=(i+1)%(n+1);
}
right[0]=1;
left[0]=n;
inv=0;
while(m--)
{
int order,X,Y;
scanf("%d",&order);
if(order==4)
{
inv=!inv;
continue;
}
scanf("%d %d",&X,&Y);
if(order==3&&right[Y]==X)swap(X,Y);//X,Y相邻要特殊考虑
if(order!=3&&inv)order=3-order;
if(order==1&&left[Y]==X)continue;
if(order==2&&right[Y]==X)continue;
int LX,RX,LY,RY;
LX=left[X];
RX=right[X];
LY=left[Y];
RY=right[Y];
if(order==1)
{
link(X,Y);
link(LX,RX);
link(LY,X);
}
else
if(order==2)
{
link(Y,X);
link(LX,RX);
link(X,RY);
}
else
if(order==3)
{
if(right[X]==Y)
{
link(LX,Y);
link(Y,X);
link(X,RY);
}
else
{
link(LX,Y);
link(Y,RX);
link(LY,X);
link(X,RY);
}
}
}
printf("Case %d: %lld\n",T++,number(n));
}
return 0;
}
撒花~

UVA12657 Boxes in a Line:题解的更多相关文章

  1. 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 ...

  2. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

  3. UVA-12657 Boxes in a Line (双向链表)

    题目大意:一个1~n的升序数字序列,有4种操作.操作1,将x放到y前面一个位置:操作2将x放到y后面的一个位置:操作3交换x和y的位置:操作4反转整个序列.求经过m次操作后的所有奇数项的和. 题目分析 ...

  4. Uva12657 Boxes in a Line

    题目链接:传送门 分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了 ...

  5. Problem B Boxes in a Line

     省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. How to setup Assigned Access in Windows 10 (Kiosk Mode) 设置分配的访问权限(Kiosk模式)

    Let’s say you’re building some sort of ingenious mechanical contraption to be displayed in public th ...

  2. CoolFormat(Qt Creator也可管理VC的Project)

    http://download.csdn.net/download/akof1314/8457593 https://github.com/akof1314/CoolFormat http://dow ...

  3. delphi Stomp客户端连接 RabbitMQ(1)

    最近公司想上个消息推送系统,网上搜了很多,因公司主要产品是Delphi,我选择了开源的RabbitMQ,Erlang语言开发,天生并行. 代码下载地址:delphistomp下载地址 windows上 ...

  4. qt sql事务操作

    事务是数据库的一个重要功能,所谓事务是用户定义的一个数据库操作序列,这些操作要么全做要么全不做,是一个不可分割的工作单位.在Qt中用transaction()开始一个事务操作,用commit()函数或 ...

  5. 为什么使用剪切板时都用GlobalAlloc分配内存(历史遗留问题,其实没关系了)

    我在使用剪切板时,发现通用的都是使用GlobalAlloc来分配内存,我就想不是说在Win32中GlobalAlloc和LocalAlloc是一样的那为什么不用LocalAlloc呢,原谅我的好奇心吧 ...

  6. Delphi驱动开发研究第一篇--实现原理

    Delphi能不能开发Windows的驱动程序(这里的驱动程序当然不是指VxD了^_^)一直是广大Delphi fans关注的问题.姑且先不说能或者不能,我们先来看看用Delphi开发驱动程序需要解决 ...

  7. 编译Qt5.0连接MySql5.5数据库的驱动(5.0版本的编译,我记得5.2开始自带了)

    第一步 1.准备好Mysql数据库安装文件,Qt5.0完整的离线安装包,以及Qt5.0的完整的源代码.安装好程序,假设Mysql的安装路径为:C:\MySQL5.5,Qt5.0的安装路径:C:\Qt\ ...

  8. Ionic 4 核心概念

    对于那些对Ionic应用程序开发完全陌生的人来说,了解项目背后的核心理念,概念和工具可能会有所帮助.下面介绍Ionic Framework的基础知识. UI组件 Ionic Framework是一个U ...

  9. Python socket文件上传下载

    python网络编程 程序的目录结构 socketDemo ├── client │   ├── cli.py │   └── local_dir │   └── lianxijiangjie.mp4 ...

  10. 通往Google之路:***

    *** & BBR 安装 系统支持:CentOS 6+, Debian 7+, Ubuntu 12+ 内存要求:≥128M --- 前提 满足以上要求的VPS服务器一台 安装基础命令工具:yu ...