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

  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. java.util Pattern 和 Mathcer

    1.测试给定的正则表达式是否匹配输入的字符串,这里该正则表达式只使用一次 private String regex ; private String input; @Before public voi ...

  2. 1.6.4 Uploading Structured Data Store Data with the Data Import Handler

    1.使用DIH上传结构化数据 许多搜索应用索引结构化数据,如关系型数据库.DIH提供了一个这样的存储并索引结构化数据的机制.除了关系型数据库,solr可以索引来自HTTP的内容,基于数据源如RSS和A ...

  3. Android(java)学习笔记267:Android线程池形态

    1. 线程池简介  多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力.     假设一个服务器完成一项任务所需时间为:T1 创建线程时间, ...

  4. java 动态代理范例 InvocationHandler与Proxy

    java 动态代理范例 InvocationHandler与Proxy,拦截与代理 java.lang.reflect.Proxy,Proxy 提供用于创建动态代理类和实例的静态方法.newProxy ...

  5. IE浏览器部分版本不支持opacity透明度属性问题

    半透明部分设置样式:opacity:0.7在ie9/ie10/ff/chrome/opera/safari显示正常. 但是这样在ie6-ie8中是不支持的,需要加上下面这句话: filter: pro ...

  6. HTML CSS编码规范(黄金定律)

    HTML 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 嵌套元素应当缩进一次(即两个空格). 对于属性的定义,确保全部使用双引号,绝不要使用单引号. 不 ...

  7. Part 82 to 85 Talking about Generic queue, stack collection class

    Part 82   Generic queue collection class Part 83   Generic stack collection class Part 84   Real tim ...

  8. DWZ(JUI) 教程 中如何整合第三方jQuery插件

    Query插件一般是$(document).ready()中初始化 $(document).ready(function(){  // 文档就绪,初始化jQuery插件| });  // 或者或缩写形 ...

  9. MyElcipse之问题小结

    运行MyEclipse时,遇到这一错误提示: An internal error occurred during: "Launching chat on MyEclipse Tomcat & ...

  10. 北大ACM(POJ1006-Biorhythms)

    Question:http://poj.org/problem?id=1006 问题点:孙子定理 Memory: 248K Time: 0MS Language: C++ Result: Accept ...