例题6-5 Boxes in a line uVa12657

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

  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;
}

时间: 2024-08-27 07:08:12

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

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

题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令).3 X Y表示交换盒子X和Y的位置.4 表示反转整条链. 盒子个数n和指令条数m(1<=n,m<=100,000) 题解 用数组来模拟链表操作,对于每个节点设置一个前驱和后继. 1操作是把x的前驱节点和x的后继节点连接,y节点的前驱和x节点连接,x节点和y

【日常学习】【模拟双向链表】【疑问】Uva12657 - Boxes in a Line题解

这道题目我做的不对.事实上,我按书上的标程抄的,几乎一模一样,我认为他没有什么错误,可我就是不知道为什么我在代码仓库下的刘汝佳写的程序就AC,我写的就WA.跳了一下午,两程序样例输出完全一样(奇怪的是和书上答案不一样)一个字一个字的比对,就是找不出哪里不一样.我觉得极少不一样的地方应该没有影响,哪位大神愿意给看看? 这是一道双向链表,同样没有用指针,而是用两个数组模拟,道理和上面的那道非指针单向链表题目一样 刘汝佳的代码 // UVa12657 Boxes in a Line // Rujia

UVA12657 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 simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to the right to Y (ig

Uva12657 (Boxes in a Line)移动盒子

UVA 12657 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 simulate 4kinds of commands:• 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y )• 2 X Y : move box

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

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 task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th

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 task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th

O - Boxes in a Line

O - Boxes in a Line Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: • 1 X Y : move box X t

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 task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th