Uva-12657 Boxes in a Line(双向链表)
2015-08-24 20:59
363 查看
注意操作3是通过数字寻找位置的,不会受是否翻转的影响
#include <cstdio>
#include <algorithm>
using namespace std;
#define BG 100005
int be[BG],ne[BG];
int w=1;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
int x,a,b;
int tn,tb;
int inv=0;//use inv to show whether the link has been inverted for odd times.
be[0]=n;
ne[0]=1;
for(i=1;i<n;i++)
ne[i]=i+1;
ne
=0;
for(i=2;i<=n;i++)
be[i]=i-1;
be[1]=0;
for(i=0;i<m;i++)
{
scanf("%d",&x);
if(x==4)
inv=!inv;//fetch the opposite value
else
{
if(x!=3&&inv==1) x=3-x;//if x is 1 or 2,change the direction of the order.
if(x==1)
{
scanf("%d%d",&a,&b);
if(be[b]!=a)
{
ne[be[a]]=ne[a];
be[ne[a]]=be[a];
be[a]=be[b];
ne[a]=b;
ne[be[b]]=a;
be[b]=a;
}
}
else if(x==2)
{
scanf("%d%d",&a,&b);
if(ne[b]!=a)
{
ne[be[a]]=ne[a];
be[ne[a]]=be[a];
ne[a]=ne[b];
be[a]=b;
be[ne[b]]=a;
ne[b]=a;
}
}
else if(x==3)//operation 3 isn't influenced by the inv
{
scanf("%d%d",&a,&b);
ne[be[a]]=b;
be[ne[a]]=b;
tn=ne[b];
tb=be[b];
ne[b]=ne[a];
be[b]=be[a];
ne[tb]=a;
be[tn]=a;
ne[a]=tn;
be[a]=tb;
}
}
// else if(x==4)//waste too much time.
// inv=!inv;
// {
//
// for(i=0;i<=n;i++)
// {
// t=be[i];
// be[i]=ne[i];
// ne[i]=t;
// }
// }
}
i=ne[0];
long long sum=0;
int count=1;
if(n%2==0&&inv==1)
i=ne[ne[0]];
while(i!=0)
{
if(count%2!=0)
sum+=i;
i=ne[i];
count++;
}
printf("Case %d: ",w);
printf("%lld\n",sum);
w++;
}
}
相关文章推荐
- Gradle学习笔记
- android下使用graphview做图形显示界面
- HDU 2841-Visible Trees(容斥)
- [leetcode-229]Majority Element II(c++)
- 【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动
- 算法入门经典习题2-10
- 单例和多例
- EditView的属性
- 强连通分量问题
- FZU 1686 神龙的难题 (DLX重复覆盖)
- 欢迎使用CSDN-markdown编辑器
- java中的异常机制(编译时异常)
- 在AndroidStudio不能找到so文件问题:couldn't find libweibosdkcore.so
- 【cocos2d-x 3.7 飞机大战】 决战南海I (七) 控制器的实现
- Native和H5两种情况的头像上传
- 别等待-想到什么就去做
- 关于Qt几个问题的答案
- Linux系统启动流程
- Android控件之ImageView
- UIScorllView和UIPageController的区别和实现图片轮播