hihoCoder 1233 : Boxes(盒子)
hihoCoder #1233 : Boxes(盒子)
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 order to keep balance. The slots are numbered from 1 to n. The leftmost one is slot 1.
At first there is exactly one box in each slot. The volume of the box in slot i is vi. As a virgo, you decide to sort these boxes by moving some of them. In each move you can choose a slot and move the top box in this slot to an adjacent slot (of course you can only put it on the top). You should ensure that the limitation mentioned above is still satisfied after this move. After the sort operation, there should be exactly one box in each slot, and for each pair of adjacent slots, the box in the left one should be smaller than the box in the right one.
Your task is to calculate the minimum number of moves you need to sort the boxes.
PKU里有个奇怪的仓库。仓库里有n个连成一线的盒子槽。每个槽上可以叠堆人员数量的盒子。为了保持平衡,唯一的限制是:只能把小盒子放在大盒子上面。槽的编号为从1到n。最左边的槽为1。 开始时,每个槽都有一个盒子。槽i中盒子的体积为vi。作为处女座,你决定通过移动盒子来对其进行排序。每次移动时你都能选择一个槽,把顶部的盒子移动到相邻的槽(当然你还是只能放在顶部)。你需要确保移动后依然满足上述的限制。排序过后,每个槽都应有一个盒子,并且对于任意一对相邻的槽,左边的盒子要小于右边的盒子。 你的任务就是计算完成盒子排序的最少移动次数。
CN
Input - 输入
In the first line there’s an integer T(T≤6000), indicating the number of test cases. The following 2T lines describe the test cases.
In each test case, the first line contains an integer n, indicating the number of slots. The second line contains n integers v1,v2…vn, indicating the volume of the boxes. It is guaranteed that all vi in a test case are different.
Please note that n<8,0≤vi≤104
第一行有一个整数T,表示测试用例的数量。随后有2T行的测试用例。 对于每组测试用例,第一行有一个整数n,表示槽的数量。第二行有n个整数 v1,v2 … vn,表示盒子的体积。保证一组用例中所有的vi都是不同的。 请注意n<,≤vi≤^
CN
Output - 输出
For each test case, print a line containing one integer indicating the answer. If it’s impossible to sort the boxes, print -1.
对于每组测试用例,输出一行整数答案。如果无法完成盒子排序,输出-。
CN
Sample Input - 样例输入
4
3
2 1 3
2
7 8
2
10000 1000
3
97 96 95
Sample Output - 样例输出
4
0
-1
20
题解
开始时脑袋一抽冒了个2^(8*8)的状态表示,看到2^21后恍然大悟。 写完时没注意T,弄了个在线查询超时了……默默地改成打表
压缩状态:
[1--] [2--] [3--] [4--] [5--] [6--] [7--]
最多7个数,把数字离散化成位置。
用3位二进制标记第i小的数存放的位置,[0, 6]或[1, 7]的方式都差不多。 状态数2^(3*7)。
(但是想了想为什么不把3位全用上,估计是因为2^(3*8)……)
后面就是BFS打表,枚举所有情况。
后记:
打表刚刚写完的时候在本地的VS跑了2.5s的初始化,结果hihoCoder上居然A了?!哈?还只花了0.1s
吓得我以为hihoCoder用的是80GHz的CPU……
后面经过对比估计是VS和G++ STL的区别,VS不知道为什么这次queue特别低效。
经过稍微优化后也只能达到1.3s的初始化速度,G++基本为0.2s内初始化完成。
估计还是queue的锅,不知这速度差距是不是算得上BUG了。
代码 C++
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
int n, ed, bit[] = { }, map[];
unsigned int opt[ << | ], data[];
std::queue<int> q; int red(){
int now, tmp[], i;
scanf("%d", &n);
for (i = ; i < n; ++i) scanf("%d", data + i);
memcpy(tmp, data, sizeof data);
std::sort(tmp, tmp + n);
for (i = ; i < n; ++i) map[tmp[i]] = i;
for (i = now = ; i < n; ++i) now ^= bit[map[data[i]]] * (i + );
return now;
} void setData(int d){
int i, j;
memset(data, -, sizeof data);
for (i = ; i <= n; ++i, d >>= ){
if (~data[j = (d & ) - ]) continue;
data[j] = i;
}
}
void BFS(){
int now, nxt, i;
while (!q.empty()){
setData(now = q.front()); q.pop();
for (i = ; i < n; ++i){
if (data[i] == -) continue;
if (i < n - ){
if (data[i] < data[i + ]){
nxt = now + bit[data[i] - ];
if (opt[now] + < opt[nxt]) opt[nxt] = opt[now] + , q.push(nxt);
}
}
if (i > ){
if (data[i] < data[i - ]){
nxt = now - bit[data[i] - ];
if (opt[now] + < opt[nxt]) opt[nxt] = opt[now] + , q.push(nxt);
}
}
}
}
}
void rdy(){
memset(opt, -, sizeof opt);
int now, i;
for (i = ; i < ; ++i) bit[i] = bit[i - ] << ;
for (i = now = ; i < ; n = ++i, BFS()){
q.push(now += bit[i] * (i + )); opt[now] = ;
}
} int main(){
rdy();
int t;
for (scanf("%d", &t); t--; printf("%d\n", opt[red()]));
return ;
}
hihoCoder 1233 : Boxes(盒子)的更多相关文章
- ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)
hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There is a strange storehouse in PKU. In this ...
- hihocoder 1233 Boxes
题意:类汉诺塔的一个东西……移动规则与汉诺塔一样,但初始状态为题目中给出的每根棍上一个盘子,目标状态为盘子在棍上按大小顺序排列,盘子只能在相邻的棍儿上移动. 解法:广搜并打表记录从目标状态到所有可能的 ...
- Linux有趣命令
通外网 下载使用阿里云镜像源:wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.re ...
- HDU 5810 Balls and Boxes(盒子与球)
Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- [Swift]LeetCode546. 移除盒子 | Remove Boxes
Given several boxes with different colors represented by different positive numbers. You may experie ...
- 546 Remove Boxes 移除盒子
给定一些不同颜色的盒子,以不同的正整数表示.消去连续相同颜色的盒子,直到全部消除完毕为止.每一次消去可以得到k * k分(k为消去盒子的个数, k >= 1).计算可以得到的最大得分.注意:盒 ...
- 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 ...
- [LeetCode] Remove Boxes 移除盒子
Given several boxes with different colors represented by different positive numbers. You may experie ...
- 2015北京网络赛 G题 Boxes bfs
Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...
随机推荐
- 使用Python中的config配置
Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置),所以可以自己写一个函数,实现读取config配置. config文件的写法比较简单,[sect ...
- JAVA基础2---深度解析A++和++A的区别
我们都知道JAVA中A++和++A在用法上的区别,都是自增,A++是先取值再自增,++A是先自增再取值,那么为什么会是这样的呢? 1.关于A++和++A的区别,下面的来看个例子: public cla ...
- ESB(Enterprise Service Bus)企业服务总线介绍
ESB(Enterprise Service Bus)企业服务总线介绍 ESB全称为Enterprise Service Bus,即企业服务总线.它是传统中间件技术与XML.Web服务等技术结合的产物 ...
- 外网上传到NAS速度很慢是什么情况?上行1M都不到,但是测试有4M
外网上传到NAS速度很慢是什么情况?上行1M都不到,但是测试有4M NAS可以将自己的影片,图片,音乐都放在NAS中.在家中就能无线共享了.在其他地方要下载自己nas里的影片,下载速度主要取决于家里宽 ...
- 51Nod 1072 威佐夫游戏
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1072 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆 ...
- SharePoint 解决管理员密码修改后各种问题的来袭
问题描述:本人用的是Win10自带虚拟机Hyper-V.Windows Service 2012R2.SQL2014和SharePoint2016,由于有一段时间没有登录虚拟机,在此登录的时候提示密码 ...
- echarts遇到的问题
X轴无偏移: axisTick: { alignWithLabel: true }, x轴显示所有数据项且避免拥挤在xAxis设置: axisLabel: { interval: 0, rotate: ...
- Django后端项目----restful framework 认证源码流程
一.请求到来之后,都要先执行dispatch方法,dispatch方法方法根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 ...
- Golang面向对象_继承
package main import "fmt" type Person struct { name string //名字 sex byte //性别 age int //年龄 ...
- bzoj1227 P2154 [SDOI2009]虔诚的墓主人
P2154 [SDOI2009]虔诚的墓主人 组合数学+离散化+树状数组 先看题,结合样例分析,易得每个墓地的虔诚度=C(正左几棵,k)*C(正右几棵,k)*C(正上几棵,k)*C(正下几棵,k),如 ...