UVA12657 Boxes in a Line:题解】的更多相关文章

题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是不能直接用数组模拟了,因为n,m的大小会达到100000. 然后, 1.可以编写一些辅助函数来设置链接关系. 2.注意 op==3的时候,要对xy相邻的情况进行特判,因为有这种情况 2 1 (头节点) 3 1 2 (尾节点) 3.我们会发现如果反转两次,就相当于没有翻转.如果翻转一次,op=1变为o…
12657 - 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 4kinds 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…
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令).3 X Y表示交换盒子X和Y的位置.4 表示反转整条链. 盒子个数n和指令条数m(1<=n,m<=100,000) 题解 用数组来模拟链表操作,对于每个节点设置一个前驱和后继. 1操作是把x的前驱节点和x的后继节点连接,y节点的前驱和x节点连接,x节点和y…
题目大意:一个1~n的升序数字序列,有4种操作.操作1,将x放到y前面一个位置:操作2将x放到y后面的一个位置:操作3交换x和y的位置:操作4反转整个序列.求经过m次操作后的所有奇数项的和. 题目分析:建立双向链表,每次操作只需修改链表中的元素指向. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std;…
题目链接:传送门 分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了. 不过这样有一个问题:第4个操作似乎无法很高效地进行,如果真的按照它说的那样模拟翻转,恐怕复杂度和暴力也要差不多了,那么我们怎么处理呢? 观察发现题目只让我们输出奇数位置的编号和,也就是说,如果题目给定的序列长度就是奇数的,那么无论用多少次4操作都没有影响,否则4的次数是奇数,则用总和减去当前奇数…
 省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... 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 lef…
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 th…
  You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4kinds 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 (i…
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 (ig…
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 th…
  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 (igno…
这道题目的解决方案是双向链表,数据结构本身并不复杂,但对于四种情况的处理不够细致,主要体现在以下几点: 分类讨论不全面,没有考虑特殊情况(本身不需要操作,需要互换的两元素相邻) 没有考虑状态4改变后对其他操作的影响 没有灵活运用数学知识(求偶只需要全部减去奇数即可) 以下贴出AC代码 #include <cstdio>#include <algorithm>const int maxn = 100000 + 10;int left[maxn];int right[maxn];int…
题意 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 意思就是说在给定的节点中计算出在同一条直线上的最大节点个数. 思路 这道题,题意很容易理解,但是需要注意的东西包括,如果你用斜率计算,那么就需要注意到精确度的问题,否则就会变成相等的斜率,无奈之下,只能提高精确度,比如说用long double,但这不是持久的办法,目前的办法是使用最大公约数.…
题意:对于一行按照顺序排列盒子数字与位置都为 1,2,3,4....n 执行四种操作 c = 1    x 放到 y 的左边 c =2     x 放到 y 的右边 c =3 交换 x, y c =4 颠倒链 最后求出奇数位置的数的总和 题解:直接维护无论如何每次每次都需要维护一段区间的位置,因此不去看位置.只需要知道每个盒子左边是哪个盒子右边是哪个盒子 这样就直接使用双向链表维护,接着颠倒时注意只是标记就好 最后注意几个细节: 首先颠倒后1与2的交换需要互换: 维护链表时可以提取出一个函数,每…
题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m  代表有n个盒子 每个盒子最开始按1~n排成一行  m个操作, 1 x y  :把盒子x放到y的左边 2 x y: 把盒子x放到y 的右边 3 x y:调换x y盒子的位置 4    表示反转整条链 思路:也是很明显的暴力 模拟 .  但是值得提的是 虽然是暴力,但是却是用的双向链表来暴力. 有很多要注意的地方 : 当操作4的时候,我们可以把本次操作记录一下,不必直接把全部的位置反转 试想一…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 双向链表模拟题. 移动的时候,要注意它就在所需要的位置的情况.那种情况不移动. (如果已经在所需位置了,还用链表的插入方式强行移动的话,会WA到死..) [代码] #include <bits/stdc++.h> using namespace std; #define ll long long const int N = 1e5; int n, m,flag; pair <int, int> v[N+10]; v…
原题链接 题意简介: 已知分别处在 \((-\infty,-1]\) H.\((-1,0)\) .\((0,1)\) .\([1,\infty)\) 的实数的数量(下记为集合 \(A,B,C,D\) ),试问:把这些数乘起来后,答案的可能出现在那个范围中? 题解: 首先,我们不难发现,如果负数的个数为奇数,那么答案必然在 \(A\) 和 \(B\) 中,否则,将出现在 \(C\) 和 \(D\) 中. 确定了这一点后,我们将符号全部视为正,试着探索答案能否出现在 \(C\) 或 \(D\) 中.…
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际的链表对一个数字进行定位需要扫一遍,时间复杂度难以承受,因此使用数组模拟双向链表. 易错点:1.要对特殊位置进行处理,例如xy相邻的情况 2.注意链表头和尾可能在任意一个操作中变化,要进行检查 #include <bits/stdc++.h> using namespace std; ; type…
双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #include <cstring> using namespace std; ],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)…
题目链接 /* 问题 将一排盒子经过一系列的操作后,计算并输出奇数位置上的盒子标号之和 解题思路 由于数据范围很大,直接数组模拟会超时,所以采用数组模拟的链表,left[i]和right[i]分别表示i号盒子的左边是谁和右边 是谁.特别提醒,4这个操作可以采用其他的办法去标记,而不必真的去模拟交换,否则会超时. */ #include<cstdio> #include<algorithm> using namespace std; ; int n,left[maxn],right[…
题目链接:https://www.luogu.org/problemnew/show/P2952 分析: 这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque,下面来简单讲解一下deque的一些操作. clear()clear()clear():清空队列 pushpushpush_back()back()back():从尾部插入一个元素. pushpushpush_front()front()front():从头部插入一个元素. deque双端队列的先进就…
先上一波题目 https://vjudge.net/contest/338760#problem/L 这道题我们维护一个双向链表 操作1 2 3 都是双向链表的基本操作 4操作考虑到手动将链表反转时间复杂度太高 我们可以不反转序列 而反转“操作” 如反转之后其实就是将操作1和2互换 对操作三没有影响 . 在求和的时候 我们可以先按正常顺序求出奇数位置的数的和 而如果数列长度为奇数 反转前后奇数位置的数的和不变 如果数列长度为偶数 反转奇数次之后 奇偶位置改变 这个时候我们只需要将总和减去偶数位置…
题意:给一个如图坐标系,每个方形都放在下面两个中间,已知一个木块湿了那么他下面所有的都会湿,显然,不能湿两次.问,每次给出一个坐标,把他弄湿,有几个木块从干变成湿了. 思路:我们把坐标系拉直,就变成了如图,显然我们弄湿 a(0,5),那么红色部分变湿,看一眼应该已经找到计算面积的方法了.所以我们每次得到一个坐标,我们就能直接算出面积.然后我们判断,是否已经有顶点所产生的面积包含了我的顶点,是的话我湿的面积为0.没有的话我就遍历一遍所有顶点,删掉所有的已经湿了的面积,剩下的就是新湿的面积了. 然后…
Content 给定一个 \(n\) 个点.\(n\) 条边的无向图.对于所有的 \(1\leqslant i<n\),在点 \(i,i+1\) 之间连一条无向边.另外在给定两个点 \(x,y\),在点 \(x,y\) 之间连一条无向边.现请对于所有的 \(1\leqslant k<n\),求出图中最短距离为 \(k\) 的点对数. 数据范围:\(3\leqslant n\leqslant2\times 10^3\),\(1\leqslant x,y\leqslant n\),\(x+1<…
原文链接:http://www.zhangxinxu.com/wordpress/2010/01/css-float%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%B7%B1%E5%85%A5%E7%A0%94%E7%A9%B6%E3%80%81%E8%AF%A6%E8%A7%A3%E5%8F%8A%E6%8B%93%E5%B1%95%E4%B8%80/ <p>这是一行普通的文字,这里有个 <em>em</em> 标签.</p> 这段HTML代…
#include <bits/stdc++.h> using namespace std; ]; ][]; ][][]; ][][][]; ][][][][]; ][][][][][]; ][][][][][][]; struct node{ ]; int step; }; map<int,int> Hash; ],b[],c[]; bool check(int a[],int num,int step){ //检查有没有走过 ){ ]]>=) return true; ]]…
Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonline/problem/7 Description There is a strange storehouse in PKU. In this storehouse there are n slots for boxes, forming a line. In each slot you can p…
Problem UVA12657-Boxes in a Line Accept: 725  Submit: 9255 Time Limit: 1000 mSec Problem Description 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…
hihoCoder #1233 : Boxes(盒子) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 There is a strange storehouse in PKU. In this storehouse there are n slots for boxes, forming a line. In each slot you can pile up any amount of boxes. The limitation i…
题目描述 Description There is a strange storehouse in PKU. In this storehouse there are n slots for boxes, forming a line. In each slot you can pile up any amount of boxes. The limitation is that you can only pile a smaller one above a bigger one, in ord…