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 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
Sample Output
Case 1: 12
Case 2: 9
Case 3: 2500050000
HINT
这个题重点就是如何解决超时的问题,如果老老实实的按照题目的要求来反转链表,那超时是肯定的了。另外利用函数查找也是超时。一开始值考虑到反转超时,没考虑查找超时问题,看了vj上一个大佬的思路用数组储存地址。因为链表只有反转后和反转前两种状态,而当链表为反转时,x放到y的右侧就是真的右侧,而反转后便是y的左侧。因此只需要一个标志位记录链表的状态就好。等到最后输出的时候在看链表时反转的还是正向的,进行选择反转。然后计算答案输出。
Accepted
#include<bits/stdc++.h>
using namespace std;
int main() {
int m, n, op, x, y, num = 0;
while (cin >> m >> n) {
int q = 1;
list<int>L(m); //这里必须先说明大小为m否则如果m过大会卡死程序,不知道为啥~
vector<list<int>::iterator>pointer(m+1); //这里必须先说明大小为m否则如果m过大会卡死程序,不知道为啥~
for (auto temp = L.begin();temp != L.end();temp++) { //初始化
*temp = q++;
pointer[q - 2] = temp;
}
int flag = 1; //判断正反1为正,-1为反
while (n--) {
cin >> op;
if (op == 4)flag = -1 * flag; //标记反转
else {
cin >> x >> y;
x--;y--;
if (op == 3) { //交换的时候要值和地址一起交换
swap(*pointer[x], *pointer[y]);
swap(pointer[x], pointer[y]);
}
else {
auto temp1 = pointer[x]; //先插到y的右面
pointer[x] = L.insert(pointer[y], *pointer[x]);
L.erase(temp1);
if (flag == 1 && op == 2 || flag == -1 && op == 1) { //如果要查到右边就交换x,y
swap(*pointer[x], *pointer[y]);
swap(pointer[x], pointer[y]);
}
}
}
}
unsigned long long int sum = 0; //计算
auto temp = L.begin();
if (flag == -1)temp = --L.end();
m = L.size() - 1;
for (int i = 0;i < L.size();i++, m--)
{
if (i % 2 == 0)sum += *temp;
if (flag == 1)temp++; //对反转还是正向进行区分
else if (temp != L.begin()) temp--;
}
cout << "Case " << ++num << ": " << sum << endl;
}
}
Boxes in a Line UVA - 12657的更多相关文章
- Boxes in a Line UVA - 12657 (双向链表)
题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m 代表有n个盒子 每个盒子最开始按1~n排成一行 m个操作, 1 x y :把盒子x放到y ...
- 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(双向链表)
12657 - Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to righ ...
- 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 ...
- 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 ...
- CDQ分治入门 + 例题 Arnooks's Defensive Line [Uva live 5871]
CDQ分治入门 简介 CDQ分治是一种特别的分治方法,它由CDQ(陈丹琦)神犇于09国家集训队作业中首次提出,因此得名.CDQ分治属于分治的一种.它一般只能处理非强制在线的问题,除此之外这个算法作为某 ...
- 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 双向链表
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...
随机推荐
- PriorityQueue使用介绍
这玩意儿叫优先级队列,是一个类,继承了AbstractQueue类,实现了Serializable接口. jdk文档里是这么描述这玩意的: 基于优先级堆的无限优先级queue . 优先级队列的元素根据 ...
- banner自用图床2
- E百科 | 基于MEC的边缘AI服务
简介: 阿里云边缘计算团队付哲解读5G下热门场景:边缘AI.作者:阿里云付哲,计算机科学与技术专业博士后,在流量检测.资源调度领域有深入研究,其论文<Astraea: Deploy AI Ser ...
- 微信小程序(六)-项目实例(原生框架 MINA基配搭建)==01-头搜索框tabbar
项目实例(原生框架 MINA) 1.新建小程序项目 1.用自已的小程序APPID 2.清除整理项目中初建默认无关的代码 1.app.json 中删除logs,同时删除pages下的losgs文件夹 2 ...
- 使用PowerDesigner进行数据库设计并直接把设计好的表导出相应的建表语句
Power Designer:数据库表设计工具 PowerDesigner是Sybase公司的一款软件,使用它可以方便地对系统进行分析设计,他几乎包括了数据库模型设计的全过程.利用PowerDesig ...
- LeetCode1576. 替换所有的问号
原题链接 1 class Solution { 2 public: 3 string modifyString(string s) { 4 int lens = s.length(); 5 for(i ...
- Hi3559AV100外接UVC/MJPEG相机实时采图设计(一):Linux USB摄像头驱动分析
下面将给出Hi3559AV100外接UVC/MJPEG相机实时采图设计的整体流程,主要实现是通过V4L2接口将UVC/MJPEG相机采集的数据送入至MPP平台,经过VDEC.VPSS.VO最后通过HD ...
- 【转载】Android异步消息处理机制详解及源码分析
PS一句:最终还是选择CSDN来整理发表这几年的知识点,该文章平行迁移到CSDN.因为CSDN也支持MarkDown语法了,牛逼啊! [工匠若水 http://blog.csdn.net/yanbob ...
- MyBatis文档
MyBatis 学习笔记 简介 什么是Mybatis MyBatis 是一款优秀的持久层框架,是Apache的一个Java开源项目 ,它支持自定义 SQL.存储过程以及高级映射, 免除了几乎所有的 J ...
- 什么是内存对齐,go中内存对齐分析
内存对齐 什么是内存对齐 为什么需要内存对齐 减少次数 保障原子性 对齐系数 对齐规则 总结 参考 内存对齐 什么是内存对齐 弄明白什么是内存对齐的时候,先来看一个demo type s struct ...