Boxes in a Line UVA - 12657 (双向链表)

题目链接:https://vjudge.net/problem/UVA-12657

题目大意:输入n,m  代表有n个盒子 每个盒子最开始按1~n排成一行  m个操作,

1 x y  :把盒子x放到y的左边

2 x y: 把盒子x放到y 的右边

3 x y:调换x y盒子的位置

4    表示反转整条链

思路:也是很明显的暴力 模拟 。  但是值得提的是 虽然是暴力,但是却是用的双向链表来暴力。

有很多要注意的地方 :

当操作4的时候,我们可以把本次操作记录一下,不必直接把全部的位置反转 试想一下,如果每次出现一个4  每次都反转  那多麻烦 (我们要反转的话,首先得找到最后一个盒子,然后从最后一个盒子往前重新存储一遍)

但是如果有记录操作4注意再碰到其他操作 也要随之改变  :

当有记录4的时候  操作1就相当于操作2     操作2相当于操作1

还有注意的是  操作3  两个盒子交换位置   :  两个盒子相邻和不相邻是不一样的  切记!!!

最后   要输出结果的话 :  注意有没有操作4  如果有操作4的话  如果是奇数 反转一次对结果没有影响   但是偶数 的话    反转一次 我们现在求的刚好的偶数位的   总的减掉就是答案了

具体看代码:

#include<iostream>
#include<string.h>
#include<vector>
#include<stdio.h>
using namespace std;
const int maxn=1e5+5;
int n;
int Left[maxn];
int Right[maxn];
void link(int l,int r) //两个节点相连
{
    Right[l]=r;
    Left[r]=l;
}
int main()
{
    int m,ca=0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)//存好节点
        {
            Left[i]=i-1;
            Right[i]=(i+1)%(n+1);//这里为何%(n+1)   试想一下 最后一个元素的右端是谁   显然是第一个元素0
        }
        Left[0]=n;
        Right[0]=1;
        int op,x,y,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;//当调整一次顺序的时候 操作1和2  刚好相反 可以自己举例子
                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(x,ry);
                    link(y,x);
                }
                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);
                    }
                }
            }

        }
        int b=0;
        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            b=Right[b];//其实b就是i位置对应的值  可以仔细想一下
            if(i%2==1) ans+=b;
        }
        if(inv&&n%2==0) ans=(long long)n*(n+1)/2-ans;//自己举个例子就可以明白 n为奇数没有影响
        printf("Case %d: %lld\n",++ca,ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caijiaming/p/10345830.html

时间: 2024-08-29 11:20:51

Boxes in a Line UVA - 12657 (双向链表)的相关文章

双向链表 Boxes in a Line UVA - 12657

首先记录一下对链表的理解,最初看链表是在教材上看的,上面是用结构体, 理解起来还不是很困难,我也以为自己懂了,然而看到紫书上链表用的是数组(UVA11988),真的是..到最后把他的代码背下来了都还是有点晕(/(ㄒoㄒ)/~~),那个时候做题有点匆忙,也就没深究,不过后面那道(也就是这道)也就没再写了,差不多隔了一个月吧,有那么一点点感觉就是这次没看代码自己写过一遍后. 单向每一个结构体有两个元素(或者更多),其中一个是下一个元素的地址,其他的是他本身有的东西,在这道题中,1 2 3 4 5,

Uva 12657 双向链表

题目链接:https://uva.onlinejudge.org/external/126/12657.pdf 题意: 给你一个从1~n的数,然后给你操作方案 • 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 (ignore this if X is already the right of Y )• 3

uva 12657(双向链表)

一定要注意swap(x, y),x, y可能相邻! #include <cstdio> #define N 100005 #define ll long long int n, m; struct node{ int l, r; node() : l(0), r(0) {} node(int l_, int r_) : l(l_), r(r_) {} }num[N]; void init() { for(int i = 0; i <= n + 1; i++) num[i].l = i -

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

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题解

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

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 the right to