题目链接: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 X Y : swap box X and Y
• 4: reverse the whole line.

WA的地方:

1、指针的赋值顺序,还是先保存一下。

2、交换的时候,如果相邻,要特判,否则指针会乱。

3、反转的时候,我这里是实际上没有反转的,因此,操作 1,2,就会由于是否反转而混乱。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn = ;
int lefts[maxn];
int rights[maxn]; void link(int x,int y)
{
rights[x] = y;
lefts[y] = x;
} int main()
{
int n,m;
int kase = ;
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<=n; i++)
{
lefts[i] = i-;
rights[i] = (i+)%(n+);
}
lefts[] = n;
rights[] = ;
int re = ;
while(m--)
{
int op;
scanf("%d",&op);
if(op==)
{
re = !re;
continue;
}
int x,y;
scanf("%d%d",&x,&y); if(op==&&rights[y]==x) swap(x,y);
if(op!=&&re) op = - op;
if(op==&&lefts[y]==x) continue;
if(op==&&rights[y]==x) continue; int lx = lefts[x],rx = rights[x],ly =lefts[y],ry = rights[y];
if(op==)
{
link(lx,rx);
link(ly,x);
link(x,y);
}
else if(op==)
{
link(lx,rx);
link(x,ry);
link(y,x);
}
else if(op==) { if(rights[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 = ;
long long ans = ;
for(int i=; i<=n; i++)
{
b = rights[b];
if(i%==)
ans +=(long long)b;
}
if(re)
{
ans = (long long)n*(long long)(n+)/ - ans;
}
printf("Case %d: %lld\n",kase++,ans);
}
return ;
}

Uva 12657 双向链表的更多相关文章

  1. uva 12657(双向链表)

    一定要注意swap(x, y),x, y可能相邻! #include <cstdio> #define N 100005 #define ll long long int n, m; st ...

  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(双向链表+小技巧)

    题意:对于一行按照顺序排列盒子数字与位置都为 1,2,3,4....n 执行四种操作 c = 1    x 放到 y 的左边 c =2     x 放到 y 的右边 c =3 交换 x, y c =4 ...

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

    题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m  代表有n个盒子 每个盒子最开始按1~n排成一行  m个操作, 1 x y  :把盒子x放到y ...

  5. Uva 12657 移动盒子(双向链表)

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

  6. UVA 12657 Boxes in a Line

    双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...

  7. 【例题 6-5 UVA 12657 】Boxes in a Line

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 双向链表模拟题. 移动的时候,要注意它就在所需要的位置的情况.那种情况不移动. (如果已经在所需位置了,还用链表的插入方式强行移动的 ...

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

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

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

随机推荐

  1. Wireshark抓取TCP包分析

    介绍 本篇文章是使用wireshrak对某个https请求的tcp包进行分析. 目的 通过抓包实际分析了解tcp包. 准备工作 在我自己机子上安装的是wireshark2.2.6版本,随机查找了某个T ...

  2. 经典网络LeNet5看卷积神经网络各层的维度变化

    本文介绍以下几个CNN经典模型:Lenet(1986年).Alexnet(2012年).GoogleNet(2014年).VGG(2014年).Deep Residual Learning(2015年 ...

  3. js学习笔记 -- 随记

    js不区分整数和浮点数,统一用Number表示, js'=='比较会自动转换类型,会产生奇怪结果,'==='不会转换比较类型,如果不一致返回false,因此js判断始终用'===' `` 保留换行,也 ...

  4. maya安装不了

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  5. UGUI 哪些显示在前方的问题

    1.对于一个场景里不同的Canvas来说,越最后创建的或者越最后激活的Canvas,越显示在前方. 2.对于同一个Canvas里的UI来说在Hierarchy面板越靠下越显示在前方.

  6. [转]LazyLoad.js及scrollLoading.js

    本文转自:http://blog.csdn.net/ning109314/article/details/7042829 目前图片延迟加载主要分两大块,一是触发加载(根据滚动条位置加载图片):二是自动 ...

  7. 如何才能够写出优美的C代码呢?

    转载自http://developer.51cto.com/art/201601/503802.htm 面向对象的语言更接近人的思维方式,而且在很大程度上降低了代码的复杂性,同时提高了代码的可读性和可 ...

  8. pat1082. Read Number in Chinese (25)

    1082. Read Number in Chinese (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  9. [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  10. poj 3162 树DP+单调队列

    http://acm.hust.edu.cn/vjudge/problem/11552 http://blog.csdn.net/woshi250hua/article/details/7727677 ...