【数组模拟链表(双向)】UVA
来源:互联网 发布:java项目案例分析 编辑:程序博客网 时间:2024/05/30 02:23
Problem Description
给你n和m,分别对应1-n的编号,和m行操作,有四种情况1 X Y:X移动到Y的左边, 2 X Y: X移动到Y的右边, 3 X Y:交换位置, 4 反转整个编号。
代码:
#include<bits/stdc++.h>using namespace std;int lt[100055];//记录i点的左边下标int rt[100055];//记录i点的右边下标void link(int L, int R)//连接L,R,L在左边R在右边的连接方式{ rt[L] = R; lt[R] = L;}int main(){ int n, m, cas = 1; while(~scanf("%d %d", &n, &m)) { for(int i = 1; i <= n; i++)//初始化记录 { rt[i] = (i + 1) % (n + 1); lt[i] = i - 1; } rt[0] = 1; lt[0] = n; int op, x, y, inv = 0; while(m--) { scanf("%d", &op); if(op == 4) inv = !inv;//标记反转 else { scanf("%d %d", &x, &y); if(op != 3 && inv) op = 3 - op; //如果反转 1 变为2, 2 变为1 if(op == 1 && x == lt[y]) continue; if(op == 2 && x == rt[y]) continue; int Lx = lt[x], Rx = rt[x], Ly = lt[y], Ry = rt[y]; if(op == 1)//x到y的左边 { link(Lx, Rx); link(Ly, x); link(x, y); } else if(op == 2)//x到y的右边 { link(Lx, Rx); link(x, Ry); link(y, x); } else if(op == 3)//交换位置 { if(rt[y] == x)//相邻 { link(Ly, x); link(x, y); link(y, Rx); } else if(rt[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 = rt[b]; if(i % 2 != 0) ans += b; } if(inv && n % 2 == 0) ans = (long long) n * (n + 1) / 2 - ans;//n为奇数求和结果不变 printf("Case %d: %lld\n", cas++, ans); }}
阅读全文
0 0
- 【数组模拟链表(双向)】UVA
- UVA 12657(双向链表模拟)
- 紫书章六例题五 移动盒子 UVA 12657 (数组模拟双向链表)
- UVA-133 双向链表模拟题
- UVa 12657 Boxes in a Line 数组模拟双向循环链表
- SOJ-4072(数组模拟双向链表)
- 用数组模拟双向链表
- 【数组模拟链表】UVA
- UVA 12657 Boxes in a Line 模拟 双向链表
- UVa 11988 数组模拟链表
- UVa-11988(数组模拟链表)
- 用动态数组模拟双向循环链表
- UVA12657 Boxes in a Line【双向链表】【数组模拟】
- UVa 12657 双向链表
- UVa - 11988 Broken Keyboard(数组模拟链表)
- DLX解9*9数独 数组模拟双向十字链表
- (双向链表之数组模拟)Boxes in a Line,第九届湖南省赛,B题
- 约瑟夫环问题(双向循环链表模拟)
- USACO之Section1.1.1 Your Ride Is Here
- 31.左旋转字符串
- 用友U8获取指定用户拥有指定权限的账套
- Cannot load project: com.intellij.ide.plugins.PluginManager$StartupAbortedException
- 初识三层
- 【数组模拟链表(双向)】UVA
- HTML和CSS常见面试题
- Raising Bacteria——个人c++解
- 代理模式和反射API
- 简单实现c++动态链接库
- HTML与XML比较
- 凸包的解法
- winform
- 561. Array Partition I