UVA - 12657 Boxes in a Line(双向链表)
来源:互联网 发布:linux more 下一页 编辑:程序博客网 时间:2024/05/22 02:32
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000+5;int n,m;int Left[maxn],Right[maxn];void link(int L ,int R){ Right[L] = R,Left[R] = L;}int main(){ int kase = 1; while(scanf("%d%d",&n,&m) == 2){ for(int i = 1; i <= n; i++){ Left[i] = i-1; Right[i] = (i+1)%(n+1); } Right[0] = 1;Left[0] = n; int x,y,check = 0,op; while(m--){ scanf("%d",&op); if(op == 4) check = !check; else{ scanf("%d%d",&x,&y); if(op == 3 && Right[y] == x) swap(x,y); if(op != 3 && check) op = 3-op; if(op == 1 && x == Left[y]) continue; if(op == 2 && x == Right[y]) continue; int Lx = Left[x],Rx = Right[x],Ly = Left[y],Ry = Right[y]; if(op == 1){ link(Lx,Rx);link(Ly,x);link(x,y);} else if(op == 2){link(Lx,Rx);link(y,x);link(x,Ry);} else if(op == 3){ if(Right[x] == y) {link(Lx,y);link(y,x);link(x,Ry);} else {link(Lx,y);link(y,Rx);link(Ly,x);link(x,Ry);} } } } int b = 0; long long ans = 0; for(int i = 1; i <= n; i++){ b = Right[b]; if(i%2 == 1)ans+=b; } if(check && n%2 == 0) ans = (long long) n * (n+1)/2 -ans; printf("Case %d: %lld\n",kase++,ans); } return 0;}
题目是这样的,给出n个盒子,m条指令(1<=n,m<=100000),
1 x y 把盒子x放在y的左边
2 x y 把盒子x放在y的右边
3 x y 把x,y调换
4 把整个序列反转
题解,利用双向链表的性质,有左链表和右链表。
主要是两个结点相互连接
void link(int L,int R){
right[L] = R, left[R] = L;
}
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
- PHP基础
- cvsnt 配置 cvsnt2.5.04配置 cvsnt服务器端配置
- 文章标题
- 数据库事务隔离级别
- ElasticSearch 入门
- UVA - 12657 Boxes in a Line(双向链表)
- 几条使用率最高的SQL语句
- 在命令里编译和执行 带包名 和 jar包 java的文件
- Android 随笔——Activity的生命周期的理解
- Centos下Apache访问网站根目录下子目录出现You don't have permission to access错误的解决办法
- Emacs的ASCII图模式
- iOS面试题
- mysql赋权与收回权限(grant&revoke) 删除权限
- 客户端apache配置