UVA12657 Boxes in a Line:题解
题目链接: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:题解的更多相关文章
- 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(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为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次操作后的所有奇数项的和. 题目分析 ...
- Uva12657 Boxes in a Line
题目链接:传送门 分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了 ...
- 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 ...
随机推荐
- Delphi 编写IC控件
编写控件的基本步骤 1.确定一个祖先类 2.创建一个组件单元 3.在新控件中添加属性.方法和事件 事件定义方法如下: type private FOnClick:TNotifyEvent ;//( 声 ...
- Attention is all you need及其在TTS中的应用Close to Human Quality TTS with Transformer和BERT
论文地址:Attention is you need 序列编码 深度学习做NLP的方法,基本都是先将句子分词,然后每个词转化为对应的的词向量序列,每个句子都对应的是一个矩阵\(X=(x_1,x_2,. ...
- ASP.NET WebForm项目--页面正确返回404及500等状态
实现方法 项目路径 <system.webServer> <httpErrors errorMode="Custom" > <remove statu ...
- Color gradient in Delphi FireMonkey
Introduction to color gradients in Delphi FireMonkey. Video This video covers the basics of color gr ...
- 【操作系统】elementary OS 和 deepin OS
文章摘自:http://blog.sina.com.cn/s/blog_64fb59d90102x3xa.html 部分参考:https://bbs.deepin.org/forum.php?mod= ...
- Linux ssh及远程连接工具
putty:http://www.so.com/link?url=http%3A%2F%2Fsoftdl.360tpcdn.com%2FPuTTY%2FPuTTY_0.67.zip&q=put ...
- shell多线程之进程间通信
# 这是一个简单的并发程序,有如下要求: # .有两个程序a和b,希望他们能并发执行,以节约时间 # .a和b都是按照日期顺序执行,但b每日程序的前提条件是当日a的程序已经执行完毕 #解决方案: # ...
- spring源码解析之IOC容器(三)——依赖注入
上一篇主要是跟踪了IOC容器对bean标签进行解析之后存入Map中的过程,这些bean只是以BeanDefinition为载体单纯的存储起来了,并没有转换成一个个的对象,今天继续进行跟踪,看一看IOC ...
- HTML连载10-details标签&summary标签&marquee标签
1.详情(details)与概要(summary)标签 (1)作用:我们希望用尽可能少的空间来表达更多的信息,利用summary标签来描述概要信息,用details标签来描述详情信息 (2)格式: ...
- Flutter学习笔记(2)--Dart语言简介
Dart简介: Dart诞生于2011年10月10日,Dart是一种"结构化的web编程"语言,Dart虽然是谷歌开发的计算机编程语言,但后来被ECMA认定位标准,这门语言用于We ...