双向链表

注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理;

   指令3 中交换盒子要注意两个盒子相邻的情况

 #include <iostream>
#include <cstring>
using namespace std; int ri[],le[]; void link (int l,int r){
ri[l]=r;le[r]=l;
} void moveleft (int l,int r){
link (le[l],ri[l]);
link (le[r],l);
link (l,r);
} void moveright (int l,int r){
link (le[l],ri[l]);
link (l,ri[r]);
link (r,l);
} void exchange (int l,int r){
int ll,rl,lr,rr;
ll=le[l];rl=ri[l];lr=le[r];rr=ri[r];
if (ll==r){
link (lr,l);link (l,r);link (r,rl);
}
else if (l==lr){
link (ll,r);link (r,l);link (l,rr);
}
else {
link (ll,r);link (r,rl);
link (lr,l);link (l,rr);
}
} int main (){
int n,m,kase=,inv;
while (cin>>n>>m){
inv=;
memset (ri,,sizeof ri);
memset (le,,sizeof le);
for (int i=;i<=n;i++)
link (i,i+);
for (int i=;i<m;i++){
int c;
cin>>c;
if (c==)
inv++;
else {
int x,y;
cin>>x>>y;
if (x==y)
continue ;
if (inv%)  //经过指令4后指令1,2也要翻转处理;
c=-c;  
if (c==){
moveleft (x,y);
}
else if (c==){
moveright (x,y);
}
else {
exchange (x,y);
}
}
//int temp=0;
//for (int i=ri[0];i<=n&&temp<n;i=ri[i]){
// cout<<i<<" ";
// temp++;
//}
}
long long ans=;
int f;
if (n%)
f=;
else f=inv%;
int temp=;
for (int i=ri[];i<=n&&temp<n;i=ri[i]){// cout<<ri[i]<<" ";
if (temp%==f){
ans+=i;
}
temp++;
}
cout<<"Case "<<++kase<<": ";
cout<<ans<<endl;
}
return ;
}

UVA 12657 Boxes in a Line的更多相关文章

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

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

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

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

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

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

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

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

  7. uva 11174 Stand in a Line

    // uva 11174 Stand in a Line // // 题目大意: // // 村子有n个村民,有多少种方法,使村民排成一条线 // 使得没有人站在他父亲的前面. // // 解题思路: ...

  8. Problem B Boxes in a Line

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

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

随机推荐

  1. 学习第一个头文件stdio.h

    使用标准输入输出库函数时要用到 “stdio.h”文件,因此源文件开头应有以下预编译命令: #include<stdio.h> stdio是standard input&outup ...

  2. Excel导出cs文件

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. TFS2013团队使用纪要

    Team Foundation Server(TFS)是微软推出的团队项目管理工具,主要包含代码版本控制,工作任务分派,持续集成,测试等一系列管理任务. 由于团队之前人数较少,使用的仅有代码版本控制( ...

  4. Linux系统维护修复模式

    基于PXE方式的Linux系统维护工具箱     在安装RedHat Linux系统的过程中,我们知道可以通过PXE方式进行安装,从而解决了无光驱或无安装介质(光盘)来安装操作系统.但是当系统由于某种 ...

  5. 经过一年时间的沉淀 再次回首 TCP Socket服务器编程--转

    ------------------ 前言 ------------------ 开发了这么多年,发现最困难的程序开发就是通讯系统. 其他大部分系统,例如CRM/CMS/权限框架/MIS之类的,无论怎 ...

  6. UESTC_Tournament CDOJ 124

    A sports company is planning to advertise in a tournament. It is a single round-robin tournament, th ...

  7. Median of Two Sorted Arrays 解答

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  8. 自定义View的编写

    在项目的时候,很多情况要用到自定义View来达到自己想要的效果,所有自定义View的编写很重要. 首先看看所要实现的效果: 最上面的一行字“LogicView”每次从左向右滚动,下面的圆从角度0到36 ...

  9. Java程序员面试题集(116-135)

    摘要:这一部分讲解基于Java的Web开发相关面试题,即便在Java走向没落的当下,基于Java的Web开发因为拥有非常成熟的解决方案,仍然被广泛应用.不管你的Web开发中是否使用框架,JSP和Ser ...

  10. 使用isql连接Sybase ASE数据库的常见错误及处理方式

    使用isql连接Sybase ASE数据库 Sybase ASE客户端工具中有一个比较实用的命令行工具isql.利用isql可以对ASE数据库服务器进行几乎所有的管理维护工作. 下面用isql工具连接 ...