题目:

id=1329">http://acm.csu.edu.cn/OnlineJudge/problem.php?

id=1329

题意:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="middle" alt="">

分析:数组模拟指针,每一个节点有两个指针(前驱和后继),每一个操作仅仅需改变相关前驱指针和后继指针的值。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <time.h>
using namespace std;
const int maxn = 1e5+6;
int *Next,*Per,N,M; void Init()
{
for(int i=0;i<=N+1;i++)
{
Next[i]=i+1;
Per[i]=i-1;
}
}
inline void Link1(int x,int y) //put x on the left of y
{
Next[Per[x]]=Next[x];
Per[Next[x]]=Per[x];
Next[x]=y;
Per[x]=Per[y];
Next[Per[y]]=x;
Per[y]=x;
}
inline void Link2(int x,int y) //put x on the right of y
{
Next[Per[x]]=Next[x];
Per[Next[x]]=Per[x];
Next[x]=Next[y];
Per[x]=y;
Per[Next[y]]=x;
Next[y]=x;
}
inline void Link3(int x,int y) //swap a and b
{
if(x==Per[y])
{
Next[Per[x]]=y;
Per[Next[y]]=x;
Per[y]=Per[x];
Next[x]=Next[y];
Next[y]=x;
Per[x]=y;
}
else if(x==Next[y])
{
Next[Per[y]]=x;
Per[Next[x]]=y;
Next[y]=Next[x];
Per[x]=Per[y];
Next[x]=y;
Per[y]=x;
}
else
{
Next[Per[x]]=y;
Next[Per[y]]=x;
int XPE=Per[x],XNE=Next[x],YPE=Per[y],YNE=Next[y];
Per[y]=XPE;
Per[x]=YPE;
Next[x]=YNE;
Next[y]=XNE;
Per[XNE]=y;
Per[YNE]=x;
}
} int main()
{
Next=(int *)malloc(sizeof(int)*maxn);
Per=(int *)malloc(sizeof(int)*maxn);
int i,j,ty,x,y,cnt,tp,ncase=1;
while(scanf("%d%d",&N,&M)!=EOF)
{
Init();
cnt=0;
for(i=1;i<=M;i++)
{
scanf("%d",&tp);
if(tp==1)
{
scanf("%d%d",&x,&y);
if(x==Per[y])
continue ;
Link1(x,y);
}
else if(tp==2)
{
scanf("%d%d",&x,&y);
if(x==Next[y])
continue ;
Link2(x,y);
}
else if(tp==3)
{
scanf("%d%d",&x,&y);
Link3(x,y);
}
else
{
cnt++;
swap(Next,Per);
}
}
long long ans=0;
if(cnt&1)
for(i=Next[N+1];i<=N && i>=1;i=Next[Next[i]])
ans+=i;
else
for(i=Next[0];i<=N && i>=1;i=Next[Next[i]])
ans+=i;
printf("Case %d: %lld\n",ncase++,ans);
}
return 0;
}

CSUOJ 1329 一行盒子(数组模拟链表)的更多相关文章

  1. csuoj 1329: 一行盒子

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1329 1329: 一行盒子 Time Limit: 1 Sec  Memory Limit: 12 ...

  2. csu 1329 一行盒子(链表操作)

    1329: 一行盒子 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 693  Solved: 134 [Submit][Status][Web Boa ...

  3. UVA11988-Broken Keyboard(数组模拟链表)

    Problem UVA11988-Broken Keyboard Accept: 5642  Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...

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

  5. B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  6. CSU 1329: 一行盒子

    1329: 一行盒子 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 740  Solved: 145[Submit][Status][Web Board ...

  7. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

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

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

  9. 天梯赛 L2-022. (数组模拟链表) 重排链表

    题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...

随机推荐

  1. iview日期选择框,获取的日期总是少一天

    使用iview的datepicker时间选择器发现获取的value值是比实际要少一天,严格来说应该是时间格式不一样,datepicker获取的时间是UTC时间 格式,也就是:yyyy-MM-ddTHH ...

  2. PHP入门开发

    1.下载开发工具phpstorm 地址:http://www.jetbrains.com/phpstorm/download/download-thanks.html?platform=windows ...

  3. python自动化测试学习笔记-6redis应用

    上次我们学到了redis的一些操作,下面来实际运用以下. 这里我们先来学习一下什么是cookie和session. 什么是Cookie 其实简单的说就是当用户通过http协议访问一个服务器的时候,这个 ...

  4. sqlyog注册码激活

    姓     名(Name):ttrar 序 列 号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c 或者(OR) 姓     名(Name):ttrar 序 列 ...

  5. python--9、进程及并发知识

    进程 一个文件的正在执行.运行过程就成为一个进程.执行多个程序,把程序文件都加载到内存,并且多个程序的内存空间隔离--空间上的复用. 遇到IO等待,切CPU到别的程序,提升效率.没有IO,一个程序占用 ...

  6. 增加删除div

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  7. shell编程之grep命令的使用

    大家在学习正则表达式之前,首先要明确一点,并把它牢牢记在心里,那就是: 在linux中,通配符是由shell解释的,而正则表达式则是由命令解释的,不要把二者搞混了.切记!!! 通常有三种文本处理工具/ ...

  8. 非常好用的1款UI自动化测试工具:airTest

    网易团队开发的UI自动化测试神器airTest,下载地址:http://airtest.netease.com/tutorial/Tutorial.html Appium和airTest对比,我的看法 ...

  9. 10.3 io流 正篇 FileReader FileWriter读写代码

    一.FileWriter 小节: 1)FileWriter fw = new FileWriter("a.txt",true);//表示追加写入,默认是false.正常情况:执行多 ...

  10. Linux内核源码特殊用法

    崇拜并且转载的: http://ilinuxkernel.com/files/5/Linux_Kernel_Source_Code.htm Linux内核源码特殊用法 1 前言 Linux内核源码主要 ...