CSUOJ 1329 一行盒子(数组模拟链表)
题目: 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 一行盒子(数组模拟链表)的更多相关文章
- csuoj 1329: 一行盒子
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1329 1329: 一行盒子 Time Limit: 1 Sec Memory Limit: 12 ...
- csu 1329 一行盒子(链表操作)
1329: 一行盒子 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 693 Solved: 134 [Submit][Status][Web Boa ...
- UVA11988-Broken Keyboard(数组模拟链表)
Problem UVA11988-Broken Keyboard Accept: 5642 Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...
- 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 ...
- 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 ...
- CSU 1329: 一行盒子
1329: 一行盒子 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 740 Solved: 145[Submit][Status][Web Board ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
随机推荐
- $P3931 SAC E一道难题 Tree$
problem #include <bits/stdc++.h> #define rep(i,j,n) for(register int i=j;i<=n;i++) #define ...
- 【洛谷1117_BZOJ4650】[NOI2016] 优秀的拆分(哈希_后缀数组_RMQ)
题目: 洛谷1117 分析: 定义把我校某兔姓神犇Tzz和他的妹子拆分,为"优秀的拆分" 随便写个哈希就能有\(95\)分的好成绩-- 我的\(95\)分做法比fei较chang奇 ...
- ACM_哥德巴赫猜想(素数筛)
哥德巴赫猜想 Time Limit: 2000/1000ms (Java/Others) Problem Description: 哥德巴赫猜想大概是这么一回事:“偶数(>=4) == 两个质数 ...
- sql语句优化:用join取代not in
不要太多使用not in查询,最好用表连接来取代它.如: select ID,name from Table_A where ID not in (select ID from Table_B) 这句 ...
- .net Jquery动态显示当前时间
<span id="Timer"></span> <script type="text/javascript"> $(fun ...
- html5——多列布局
基本概念 1.多列布局类似报纸或杂志中的排版方式,上要用以控制大篇幅文本. 2.跨列属性可以控制横跨列的数量 /*列数*/ -webkit-column-count: ; /*分割线*/ -webki ...
- SQL基本操作——ALTER
ALTER TABLE 语句用于在已有的表中添加.修改或删除列. Persons 表: ID LastName FirstName Address City 1 Adams John Oxford S ...
- [Windows Server 2008] 安装IIS7.5及FTP
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IISII ...
- [源码阅读]RocketMQ-策略篇
一:为什么要阅读rocketmq的源码? 1 可以了解mq的底层实现逻辑. 二:打算怎么读,行动路径是哪儿些? 1: 本地启动 2 分步调试 3 fork项目,添加中文注释,提交到自己的代码库.并改 ...
- =new、=null、.clear()、system.gc()的区别
开发经验告诉我 = new是指向另一个地址空间 =null对象被回收 .clear()对象被清空,但是仍然指向原来的地址空间 这三种方式都并没有真正的清理内存 只有system.gc()是直接清理,但 ...