这道题目的解决方案是双向链表,数据结构本身并不复杂,但对于四种情况的处理不够细致,主要体现在以下几点:

  1. 分类讨论不全面,没有考虑特殊情况(本身不需要操作,需要互换的两元素相邻)
  2. 没有考虑状态4改变后对其他操作的影响
  3. 没有灵活运用数学知识(求偶只需要全部减去奇数即可)

以下贴出AC代码

#include <cstdio>
#include <algorithm>
const int maxn = 100000 + 10;
int left[maxn];
int right[maxn];
int s[maxn];
using namespace std;
void link(int x,int y){
    right[x] = y;
    left[y] = x;
}
int main(){
    #ifdef DEBUG
    freopen("6.5.in","r",stdin);
    #endif
    int n, m ,num = 0;
    while(scanf("%d %d", &n, &m)==2){
        for(int i = 1; i <= n; i++){
            right[i]= (i+1);
            left[i]=i-1;
        }
        right[0]=1;
        left[0]=n;
        int op,X,Y;
        int inv = 0;
        while(m--){
            scanf("%d",&op);
            if(op == 4) inv=!inv;
            else {
                scanf("%d%d",&X, &Y);
                if(op == 3 && right[Y] == X) swap(X,Y);
                if(op != 3 && inv) op = 3 - op;
                if(op == 1 && X == left[Y]) continue;
                if(op == 2 && X == right[Y]) continue;

int LX= left[X],RX = right[X],LY = left[Y], RY = right[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(op == 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);                      
                    }
                }
            }
        }
        long long ans = 0;
        int j=0;
        for(int i = 1;i<=n; i++){
            j=right[j];
            if(i % 2 == 1)   ans+=j;
        }
        if(inv && n %2 == 0) ans =(long long )n * (n+1) /2 -ans;
        printf("Case %d: %lld\n", ++num, ans);
    }
    return 0;
}

例题6-5 Boxes in a line uVa12657的更多相关文章

  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. Problem B Boxes in a Line

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

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

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

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

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

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

  8. UVA12657 Boxes in a Line:题解

    题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...

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

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

随机推荐

  1. Subsequence poj 3061 二分(nlog n)或尺取法(n)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9236   Accepted: 3701 Descr ...

  2. A C

    Problem Description Are you excited when you see the title "AC" ? If the answer is YES , A ...

  3. CBM-业务组件模型

  4. [设计模式] .NET设计模式笔记 - 了解设计模式

    今天在TerryLee的cnblog(http://terrylee.cnblogs.com)里看到了与设计模式相关的整套文章,初学设计模式看完每篇文章后做些笔记和摘抄. ●什么是设计模式,什么是架构 ...

  5. TFS 2010 使用手册(一)安装与配置

    本文转自cnblogs 大辉狼 的文章: http://www.cnblogs.com/wph1129/archive/2010/11/10/1873348.html http://www.cnblo ...

  6. (四)u-boot2013.01.01 for TQ210:《mkconfig分析》

    config.mk主要功能是配置单板信息和编译环境,将会被u-boot的makefile所调用,本文仍然侧重于句法分析. ####################################### ...

  7. VS2010关于WindowsService 制作安装程序包,无法自动install的问题解决方法

    当添加了 安装项目后,需要做2个步骤的操作,第一是 右键点击 “视图”---“文件系统”,添加“项目输出“,好了后,再右键点击 “视图”---“自定义操作” 在安装中添加自定义操作,将刚才的”项目输出 ...

  8. emoji表情符处理替换成空格

    /**    * 用filterOffUtf8Mb4    * Description: 过滤率四个字节的utf-8字符(emoji表情符),替换成四个空格.    *         四字节utf- ...

  9. 如何解决firefox下window.event的问题

    一.在函数中传递event参数 在函数中传递event参数,这样我们就可以兼容IE和FF的event的获取了,如下面的函数: function _test(evt){    var src = evt ...

  10. 分享一款简洁的jQuery轮播源码

    <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>无标题页</titl ...