Uva-12657 Boxes in a Line(双向链表)
来源:互联网 发布:飞腾排版软件win7 编辑:程序博客网 时间:2024/05/21 17:31
注意操作3是通过数字寻找位置的,不会受是否翻转的影响
#include <cstdio>#include <algorithm>using namespace std;#define BG 100005int 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[n]=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++; }}
0 0
- UVA - 12657 Boxes in a Line(双向链表)
- UVA 12657 Boxes in a Line 【双向链表】
- Uva-12657 Boxes in a Line(双向链表)
- UVa--12657 Boxes in a line(双向链表)
- UVA 12657 Boxes in a Line(双向链表)
- UVA 12657 Boxes in a Line(双向链表)
- UVA 12657 Boxes in a Line【双向链表】
- UVA 12657 Boxes in a Line 模拟 双向链表
- UVA 12657 Boxes in a Line(双向链表)
- UVa 12657 Boxes in a Line(双向链表的应用)
- UVa 12657 Boxes in a Line(双向链表的应用)
- UVa 12657 Boxes in a Line(双向链表的应用)
- UVa 12657 Boxes in a Line 数组模拟双向循环链表
- UVA12657 Boxes in a Line【双向链表】【数组模拟】
- uva12657 Boxes in a Line 双向链表
- UVA 12657 - Boxes in a Line
- UVA - 12657 Boxes in a Line
- UVa 12657 - Boxes in a Line
- 【cocos2d-x 3.7 飞机大战】 决战南海I (七) 控制器的实现
- Linux系统启动流程
- 单例和多例
- poj-1260 Pearls
- Android控件之ImageView
- Uva-12657 Boxes in a Line(双向链表)
- 在AndroidStudio不能找到so文件问题:couldn't find libweibosdkcore.so
- 欢迎使用CSDN-markdown编辑器
- hdu5015 233 Matrix(矩阵快速幂)
- 【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动
- 强连通分量问题
- 算法入门经典习题2-10
- EditView的属性
- HDU 2841-Visible Trees(容斥)