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

题目分析:建立双向链表,每次操作只需修改链表中的元素指向。

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; int l[100005],r[100005]; void link(int a,int b)
{
r[a]=b,l[b]=a;
} int main()
{
int n,m,op,x,y,flag,cas=0;
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;++i)
r[i]=(i+1)%(n+1),l[i]=i-1;
r[0]=1,l[0]=n; flag=0;
while(m--)
{
scanf("%d",&op);
if(op==4)
flag=!flag;
else{
scanf("%d%d",&x,&y);
if(op==3&&r[y]==x)///方便下面的操作;
swap(x,y);
if(op!=3&&flag)
op=3-op;
if(op==1&&l[y]==x)
continue;
if(op==2&&r[y]==x)
continue; int lx=l[x],rx=r[x],ly=l[y],ry=r[y];
if(op==1){
link(lx,rx);
link(ly,x);
link(x,y);
}else if(op==2){
link(lx,rx);
link(y,x);
link(x,ry);
}else{
if(r[x]==y)
link(lx,y),link(y,x),link(x,ry);
else
link(lx,y),link(y,rx),link(ly,x),link(x,ry);
}
}
}
int k=0;
long long ans=0;
for(int i=1;i<=n;++i){
k=r[k];
if(i&1)
ans+=k;
}
if(flag&&n%2==0)
ans=(long long)n/2*(n+1)-ans;
printf("Case %d: %lld\n",++cas,ans);
}
return 0;
}

  

UVA-12657 Boxes in a Line (双向链表)的更多相关文章

  1. UVA 12657 Boxes in a Line 双向链表

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...

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

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

  4. UVA 12657 Boxes in a Line

    双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...

  5. UVa 12657 Boxes in a Line(数组模拟双链表)

    题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...

  6. 2019 SCUT SE 新生训练第四波 L - Boxes in a Line——双向链表

    先上一波题目 https://vjudge.net/contest/338760#problem/L 这道题我们维护一个双向链表 操作1 2 3 都是双向链表的基本操作 4操作考虑到手动将链表反转时间 ...

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

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

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

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

随机推荐

  1. Centos设置SSH限制登录用户及IP

    1,系统版本查看 2,编辑ssh配置文件 vim /etc/ssh/sshd_config 在尾部加一行 允许sysman用户从ip1.1.1.*登录 3,重启sshd即可 /etc/init.d/s ...

  2. 沈阳网络赛G-Spare Tire【容斥】

    17.64% 1000ms 131072K   A sequence of integer \lbrace a_n \rbrace{an​} can be expressed as: \display ...

  3. 知乎live 我的读书经验 总结

    https://www.zhihu.com/lives/757587093366009856/messages 碎片化阅读没有意义, 捡硬币捡成富翁 kindle不能全文检索   短篇文章的阅读是否有 ...

  4. talib 中文文档(九):# Volatility Indicator Functions 波动率指标函数

    Volatility Indicator Functions 波动率指标函数 ATR - Average True Range 函数名:ATR 名称:真实波动幅度均值 简介:真实波动幅度均值(ATR) ...

  5. nginx:服务器集群

    一.Nginx的事件处理机制 对于一个基本的web服务器来说,事件通常有三种类型,网络事件.信号.定时器. 首先看一个请求的基本过程:建立连接---接收数据---发送数据 . 再次看系统底层的操作 : ...

  6. IT运营新世界大会:广通软件开启双态运维大时代

    10月28日,第一届“IT运营新世界大会”在北京成功举办.大会上由10家ITOM领域的标杆企业宣布结成“ITOM联盟”. 广通软件(证券代码:833322)作为大会的创始成员全程推动见证了这一历史时刻 ...

  7. 获取List、Set、Map等字段的泛型参数

    测试类加单元测试方法,运行结果在注释里面: package temp; import org.junit.Test; import java.lang.reflect.Field; import ja ...

  8. python学习笔记(二十八)日志模块

    我们在写程序的时候经常会打一些日志来帮助我们查找问题,这次学习一下logging模块,在python里面如何操作日志.介绍一下logging模块,logging模块就是python里面用来操作日志的模 ...

  9. Linux /proc/pid目录下各文件含义

    /proc 是一个伪文件系统, 被用作内核数据结构的接口, 而不仅仅是解释说明/dev/kmem. /proc 里的大多数文件都是只读的, 但也可以通过写一些文件来改变内核变量. ( Linux 内核 ...

  10. Xcel 测试版使用手册

    基于无任何文笔可言,所以直接上使用方法吧. 1.引用dll,如何引用dll请谷歌. 2.使用 //实例化对象 LT.XMLExcel.XlsxOption xOption = new LT.XMLEx ...