Problem B Boxes in a Line
手写链表。。其实很简单的。。。。
注:对相邻的元素需要特判。。。。。
Problem B
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 simulate 4 kinds of commands:
- 1 X Y: move box X to the left to Y (ignore this if X is already the left of Y)
- 2 X Y: move box X to the right to Y (ignore this if X is already the right of Y)
- 3 X Y: swap box X and Y
- 4: reverse the whole line.
Commands are guaranteed to be valid, i.e. X will be not equal to Y.
For example, if n=6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing 2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1. Then after executing 4, then line becomes 1 3 5 4 6 2
Input
There will be at most 10 test cases. Each test case begins with a line containing 2 integers n, m(1<=n, m<=100,000). Each of the following m lines contain a command.
Output
For each test case, print the sum of numbers at odd-indexed positions. Positions are numbered 1 to n from left to right.
Sample Input
6 4
1 1 4
2 3 5
3 1 6
4
6 3
1 1 4
2 3 5
3 1 6
100000 1
4
Output for the Sample Input
Case 1: 12
Case 2: 9
Case 3: 2500050000
The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu
Special Thanks: Feng Chen, Md. Mahbubul Hasan
#include <iostream>
#include <cstdio>
#include <cstring> #define toleft p
#define toright p^1 using namespace std; const int INF=0x3f3f3f3f;
typedef long long int LL; int lian[][],n,m,p; void Init()
{
p=;
for(int i=;i<=n;i++)
{
lian[i][]=i-;
lian[i][]=i+;
}
lian[][toleft]=INF;
lian[][toright]=;
lian[n+][toleft]=n;
lian[n+][toright]=INF;
} void cmdfour()
{
p=p^;
} void cmdone(int x,int y)
{
int xl=lian[x][toleft],xr=lian[x][toright];
int yl=lian[y][toleft],yr=lian[y][toright]; if(xr==y) return; lian[xl][toright]=xr;
lian[xr][toleft]=xl; lian[x][toright]=y;
lian[x][toleft]=yl; lian[yl][toright]=x;
lian[y][toleft]=x;
} void cmdtwo(int x,int y)
{
int xl=lian[x][toleft],xr=lian[x][toright];
int yl=lian[y][toleft],yr=lian[y][toright]; if(xl==y) return; lian[xl][toright]=xr;
lian[xr][toleft]=xl; lian[x][toleft]=y;
lian[x][toright]=yr; lian[y][toright]=x;
lian[yr][toleft]=x;
} void cmdthree(int x,int y)
{
int xl=lian[x][toleft],xr=lian[x][toright];
int yl=lian[y][toleft],yr=lian[y][toright]; if(xr!=y&&xl!=y)
{
lian[x][toleft]=yl;
lian[x][toright]=yr;
lian[yl][toright]=x;
lian[yr][toleft]=x; lian[y][toleft]=xl;
lian[y][toright]=xr;
lian[xl][toright]=y;
lian[xr][toleft]=y;
}
else
{
if(xr==y)
{
lian[x][toright]=yr;
lian[x][toleft]=y; lian[y][toright]=x;
lian[y][toleft]=xl; lian[xl][toright]=y;
lian[yr][toleft]=x;
}
else if(xl==y)
{
lian[x][toleft]=yl;
lian[x][toright]=y; lian[y][toleft]=x;
lian[y][toright]=xr; lian[xr][toleft]=y;
lian[yl][toright]=x;
} }
} LL getSum()
{
LL ans=;
int cnt=,pos=;
if(n%==||p==)
{
if(p) p=;
while(true)
{
if(cnt&) ans+=(LL)lian[pos][toright];
pos=lian[pos][toright];
cnt++;
if(cnt>=n+) break;
}
}
else if(p==)
{
if(p) p=;
while(true)
{
if(cnt%==) ans+=(LL)lian[pos][toright];
pos=lian[pos][toright];
cnt++;
if(cnt>=n+) break;
}
}
return ans;
} int main()
{
int cas=,cmd,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
Init(); while(m--)
{
scanf("%d",&cmd);
if(cmd==)
{
scanf("%d%d",&x,&y);
cmdone(x,y);
}
else if(cmd==)
{
scanf("%d%d",&x,&y);
cmdtwo(x,y);
}
else if(cmd==)
{
scanf("%d%d",&x,&y);
cmdthree(x,y);
}
else if(cmd==)
{
cmdfour();
}
}
LL ans=getSum();
printf("Case %d: %lld\n",cas++,ans);
}
return ;
}
Problem B Boxes in a Line的更多相关文章
- Boxes in a Line
Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...
- uva-12657 - Boxes in a Line(双向链表)
12657 - Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to righ ...
- 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 sim ...
- 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 ...
- UVa 12657 Boxes in a Line(应用双链表)
Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...
- Boxes in a Line UVA - 12657
You have n boxes in a line on the table numbered 1...n from left to right. Your task is to simulat ...
- UVA 12657 Boxes in a Line 双向链表
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...
- Boxes in a Line UVA - 12657 (双向链表)
题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m 代表有n个盒子 每个盒子最开始按1~n排成一行 m个操作, 1 x y :把盒子x放到y ...
- 2019 SCUT SE 新生训练第四波 L - Boxes in a Line——双向链表
先上一波题目 https://vjudge.net/contest/338760#problem/L 这道题我们维护一个双向链表 操作1 2 3 都是双向链表的基本操作 4操作考虑到手动将链表反转时间 ...
随机推荐
- 添加一个功能Action
1,只用一个handler类,所有都事件的处理器都在一个handler类 handler要创建以Action为名称的方法 event要单独分开,继承KDEvent package com.kingde ...
- iOS 关于本地持久化存储的探讨
目前,用以本地化存储的方式有很多,常用的有以下: 1.临时缓存 先说说临时缓存,临时缓存一般相当于用来管理应用程序中全局需要常用的一些内容.比如当前用户的ID或者当前的定位信息等. 常用的方式就是写一 ...
- HTML5系列五(Canvas详述)
写在前面 闲来无事的时候会来一场一个人说走就走的旅行或者宅家里系统性的看些技术方面的书,最近在看<html5与css3权威指南>,这本书挺适合初学前端的人,虽然对于我来说只是温习相关的知识 ...
- SSH 学习总结-01 SSH整合环境
一 Struts2+Spring3+Hibernate4+Maven 整合环境 1 开发工具 1)JDK下载地址:http://www.oracle.com/technetwork/java/java ...
- Node-webkit简介
Node-webkit 概述 Node-webkit 是Github 上一个非常热门的开源项目,它基于著名的浏览器开源项目 Chromium 和服务器端 JavaScript 实现 Node.js 的 ...
- Docker入门教程(四)Docker Registry
Docker入门教程(四)Docker Registry [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第四篇,介绍了Docker Registry,它 ...
- iOS开发-二维码
二维码 从ios7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器 生成二维码的步骤: 倒入CoreImage框架 通过滤镜CIFilter生成二维码 二维码的内 ...
- magelinux notes
[root@centos01 01]# cd ~jack #进入指定用户的家目录[root@centos01 jack]# cd - #切回上一次目录[root@centos01 home]# cd ...
- OC面向对象特性:封装
概念性知识 1.c语言是面向过程编程:分析解决问题的步骤,实现函数,依次调用 2.oc语言是面向对象编程:分析问题的组成的对象,协调对象间的联系和通信,解决问题 3.#include和#impo ...
- js获取锚点名称 #
var thisId = window.location.hash; alert(thisId); 输出 #2