UVA 12657 Boxes in a Line
双向链表
注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理;
指令3 中交换盒子要注意两个盒子相邻的情况
#include <iostream>
#include <cstring>
using namespace std; int ri[],le[]; void link (int l,int r){
ri[l]=r;le[r]=l;
} void moveleft (int l,int r){
link (le[l],ri[l]);
link (le[r],l);
link (l,r);
} void moveright (int l,int r){
link (le[l],ri[l]);
link (l,ri[r]);
link (r,l);
} void exchange (int l,int r){
int ll,rl,lr,rr;
ll=le[l];rl=ri[l];lr=le[r];rr=ri[r];
if (ll==r){
link (lr,l);link (l,r);link (r,rl);
}
else if (l==lr){
link (ll,r);link (r,l);link (l,rr);
}
else {
link (ll,r);link (r,rl);
link (lr,l);link (l,rr);
}
} int main (){
int n,m,kase=,inv;
while (cin>>n>>m){
inv=;
memset (ri,,sizeof ri);
memset (le,,sizeof le);
for (int i=;i<=n;i++)
link (i,i+);
for (int i=;i<m;i++){
int c;
cin>>c;
if (c==)
inv++;
else {
int x,y;
cin>>x>>y;
if (x==y)
continue ;
if (inv%) //经过指令4后指令1,2也要翻转处理;
c=-c;
if (c==){
moveleft (x,y);
}
else if (c==){
moveright (x,y);
}
else {
exchange (x,y);
}
}
//int temp=0;
//for (int i=ri[0];i<=n&&temp<n;i=ri[i]){
// cout<<i<<" ";
// temp++;
//}
}
long long ans=;
int f;
if (n%)
f=;
else f=inv%;
int temp=;
for (int i=ri[];i<=n&&temp<n;i=ri[i]){// cout<<ri[i]<<" ";
if (temp%==f){
ans+=i;
}
temp++;
}
cout<<"Case "<<++kase<<": ";
cout<<ans<<endl;
}
return ;
}
UVA 12657 Boxes in a Line的更多相关文章
- 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 ...
- UVA 12657 Boxes in a Line 双向链表
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...
- UVa 12657 Boxes in a Line(数组模拟双链表)
题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示 ...
- UVA 12657 Boxes in a Line(双向链表+小技巧)
题意:对于一行按照顺序排列盒子数字与位置都为 1,2,3,4....n 执行四种操作 c = 1 x 放到 y 的左边 c =2 x 放到 y 的右边 c =3 交换 x, y c =4 ...
- 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 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 11174 Stand in a Line
// uva 11174 Stand in a Line // // 题目大意: // // 村子有n个村民,有多少种方法,使村民排成一条线 // 使得没有人站在他父亲的前面. // // 解题思路: ...
- Problem B Boxes in a Line
省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...
- 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 ...
随机推荐
- 无法添加sql server ER图
Database diagram support objects cannot be installed because this database does not have a valid own ...
- 就是爱Java
就是爱Java,提供了Java代码示例,文章和教程,可以帮助你学习Java编程语言. 网站名称:就是爱Java 网站地址:http://java.openyu.org
- Silverlight 结合ArcGis 在地图画点
原文 http://www.dotblogs.com.tw/justforgood/archive/2012/05/10/72076.aspx 我们直接来看完成后的画面读者会比较有兴趣 如上,小马在地 ...
- 算法(Algorithm)是什么?
我们用煎蛋来打个比方.煎蛋的一般步骤是: 1.>取煎锅. 2.>取油. ->我们有油吗? ****有,把油倒入煎锅. ****没有,我们要去买油吗? #########要买,出去买油 ...
- ZOJ(3455)
Shizuka's Letter Time Limit: 2 Seconds Memory Limit: 65536 KB Nobita receives a letter from Shi ...
- NicEdit - WYSIWYG Content Editor, Inline Rich Text Application
NicEdit - WYSIWYG Content Editor, Inline Rich Text Application By calling the nicEditors.allTextarea ...
- PHP 文件打开/读取
PHP Open File - fopen() 打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. 在课程中,我们将使用文本文件 "w ...
- UGUI 锚点
今天我们来学习下UGUI的锚点, 他是做什么的呢? 基本上就是用于界面布局. 1. 1个控件对应1个描点. 2. 描点分成四个小叶片, 每1个叶片 对应 控件四边框的角点 3. 不管屏幕如何放大缩 ...
- Google Go 语言从入门到应用必备开源项目
Go 语言于 2009 年 11 月正式宣布推出,成为开放源代码项目,发展至今已经具有越来越广泛的影响力,今年更是在 TIOBE 编程语言排行榜中跻身 20 强.很多开发者也逐渐将目光投向这门语言,本 ...
- One Way Roads(搜索)
One Way Roads Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...