题目:

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. BZOJ 4828 DP+BFS

    被一道简单BFS坑了这么长时间我也是hhh了 //By SiriusRen #include <bits/stdc++.h> using namespace std; ,,):d(D),x ...

  2. org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException。

    jdk1.8环境tomcat运行项目报错, org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException.解决方法:更改jdk1.7

  3. sql 循环分割字符

    DECLARE @Items VARCHAR(1000)='148' --待处理拼接字符串 --开始处理SET @Items=@Items+',' --必须追加“,”否则最后一个无法输出DECLARE ...

  4. C#学习-程序集和反射

    准备项目 1.新建一个空的解决方案MyProj.sln 2.在该解决方案下,建一个控制台项目P01.csproj 3.在该项目下,自己新建一个类MyFirstClass.cs 查看解决方案MyProj ...

  5. [ USACO 2018 OPEN ] Out of Sorts (Gold)

    \(\\\) \(Description\) 运行以下代码对一长为\(N\)的数列\(A\)排序,不保证数列元素互异: cnt = 0 sorted = false while (not sorted ...

  6. CSS布局——三栏布局

    说到三栏布局,很多都会提到圣杯布局和双飞翼布局这两个经典的三栏布局方式.于是,我在网上搜了一些相关资料,阅读并跟着代码敲了一遍,发现在处理三栏布局上,他们采用的都是两边栏固定,中间栏自适应的策略.在处 ...

  7. Android 使用WebView浏览有声音或者视频的网页,关闭WebView之后,声音或者视频不停止的解决办法

    笔者最近使用Eclipse开发Android移动应用app,其实有一个功能是使用Android系统自带的WebView控件加载Web页面.开发很顺利,浏览也很正常.不过有个比较特殊的一点就是加载的We ...

  8. CSS——img标签消除3px

    1.dispaly:block 2.float:left 这两种都可以消除3px

  9. %2d

    %2d是C语言中printf函数的输出格式说明符. 具体解释如下: 使输出的int型的数值以2位的固定位宽输出.如果不足2位,则在前面补空格:如果超过2位,则按实际位数输出. 注:如果输出的数值不是i ...

  10. vc++6.0创建console32之.c的应用程序详解

    文件-->新建-->win32-->取一个名字,确定 文件-->新建-->c++Source-->取一个名字,记住以.c为后缀,确定 编写简单的程序调试