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 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.

  1. PKU里有个奇怪的仓库。仓库里有n个连成一线的盒子槽。每个槽上可以叠堆人员数量的盒子。为了保持平衡,唯一的限制是:只能把小盒子放在大盒子上面。槽的编号为从1n。最左边的槽为1
  2.  
  3. 开始时,每个槽都有一个盒子。槽i中盒子的体积为vi。作为处女座,你决定通过移动盒子来对其进行排序。每次移动时你都能选择一个槽,把顶部的盒子移动到相邻的槽(当然你还是只能放在顶部)。你需要确保移动后依然满足上述的限制。排序过后,每个槽都应有一个盒子,并且对于任意一对相邻的槽,左边的盒子要小于右边的盒子。
  4.  
  5. 你的任务就是计算完成盒子排序的最少移动次数。

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

  1. 第一行有一个整数T,表示测试用例的数量。随后有2T行的测试用例。
  2.  
  3. 对于每组测试用例,第一行有一个整数n,表示槽的数量。第二行有n个整数 v1v2 vn,表示盒子的体积。保证一组用例中所有的vi都是不同的。
  4.  
  5. 请注意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.

  1. 对于每组测试用例,输出一行整数答案。如果无法完成盒子排序,输出-。

CN

Sample Input - 样例输入

  1. 4
  2. 3
  3. 2 1 3
  4. 2
  5. 7 8
  6. 2
  7. 10000 1000
  8. 3
  9. 97 96 95

Sample Output - 样例输出

  1. 4
  2. 0
  3. -1
  4. 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++

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <queue>
  5. int n, ed, bit[] = { }, map[];
  6. unsigned int opt[ << | ], data[];
  7. std::queue<int> q;
  8.  
  9. int red(){
  10. int now, tmp[], i;
  11. scanf("%d", &n);
  12. for (i = ; i < n; ++i) scanf("%d", data + i);
  13. memcpy(tmp, data, sizeof data);
  14. std::sort(tmp, tmp + n);
  15. for (i = ; i < n; ++i) map[tmp[i]] = i;
  16. for (i = now = ; i < n; ++i) now ^= bit[map[data[i]]] * (i + );
  17. return now;
  18. }
  19.  
  20. void setData(int d){
  21. int i, j;
  22. memset(data, -, sizeof data);
  23. for (i = ; i <= n; ++i, d >>= ){
  24. if (~data[j = (d & ) - ]) continue;
  25. data[j] = i;
  26. }
  27. }
  28. void BFS(){
  29. int now, nxt, i;
  30. while (!q.empty()){
  31. setData(now = q.front()); q.pop();
  32. for (i = ; i < n; ++i){
  33. if (data[i] == -) continue;
  34. if (i < n - ){
  35. if (data[i] < data[i + ]){
  36. nxt = now + bit[data[i] - ];
  37. if (opt[now] + < opt[nxt]) opt[nxt] = opt[now] + , q.push(nxt);
  38. }
  39. }
  40. if (i > ){
  41. if (data[i] < data[i - ]){
  42. nxt = now - bit[data[i] - ];
  43. if (opt[now] + < opt[nxt]) opt[nxt] = opt[now] + , q.push(nxt);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. void rdy(){
  50. memset(opt, -, sizeof opt);
  51. int now, i;
  52. for (i = ; i < ; ++i) bit[i] = bit[i - ] << ;
  53. for (i = now = ; i < ; n = ++i, BFS()){
  54. q.push(now += bit[i] * (i + )); opt[now] = ;
  55. }
  56. }
  57.  
  58. int main(){
  59. rdy();
  60. int t;
  61. for (scanf("%d", &t); t--; printf("%d\n", opt[red()]));
  62. return ;
  63. }

hihoCoder 1233 : Boxes(盒子)的更多相关文章

  1. ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)

    hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB   描述 There is a strange storehouse in PKU. In this ...

  2. hihocoder 1233 Boxes

    题意:类汉诺塔的一个东西……移动规则与汉诺塔一样,但初始状态为题目中给出的每根棍上一个盘子,目标状态为盘子在棍上按大小顺序排列,盘子只能在相邻的棍儿上移动. 解法:广搜并打表记录从目标状态到所有可能的 ...

  3. Linux有趣命令

    通外网 下载使用阿里云镜像源:wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.re ...

  4. HDU 5810 Balls and Boxes(盒子与球)

     Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  5. [Swift]LeetCode546. 移除盒子 | Remove Boxes

    Given several boxes with different colors represented by different positive numbers. You may experie ...

  6. 546 Remove Boxes 移除盒子

    给定一些不同颜色的盒子,以不同的正整数表示.消去连续相同颜色的盒子,直到全部消除完毕为止.每一次消去可以得到k * k分(k为消去盒子的个数, k  >= 1).计算可以得到的最大得分.注意:盒 ...

  7. 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 ...

  8. [LeetCode] Remove Boxes 移除盒子

    Given several boxes with different colors represented by different positive numbers. You may experie ...

  9. 2015北京网络赛 G题 Boxes bfs

    Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...

随机推荐

  1. wingide 远程调试

    1.首先你应该在本地安装wingide 6.1版本 2.大多数电脑.py文件都不能以wingide的形式打开(异常苦逼),无论是从“属性”或者是“设置”里面都不可以,无奈之下只能通过修改注册表的方式进 ...

  2. CentOS下Yum的$releasever和$basearch的取值

    CentOS下Yum源配置文件中如CentOS-Base.repo的$releasever和$basearch的取值 $releasever的值,这个表示当前系统的发行版本,可以通过如下命令查看: r ...

  3. 转:C# 使用资源文件 Resource.resx 的方法

    在写程序时, 可以把用到的 图标,图片,声音等外部资源,放在一个  .resx (资源文件)中. 这样的好处是不用考虑什么路径的问题.而且还对资源有保护的做用. 1.创建一个 ResourceFile ...

  4. Chrome表单自动填充如何取消(暂时可行的解决办法)

    做项目时一直遇到一个问题,那就是用chrome测试的时候页面上的表单一直会自动填充,并且伴有黄色的背景颜色,有时候感觉很方便,有时候又很想去掉. 之前也多次寻找过方法,但是网上的方法都差不多,很多都是 ...

  5. FAQ About WOYO PDR007 Dent Removal Heat Induction System

    WOYO PDR 007 is a dent repair tool for auto maintence. WOYO PDR007 Auto Body Paintless Dent Repair K ...

  6. 前端框架VUE----表单输入绑定

    vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...

  7. 利用vue写filter时 当传入是一个对象时注意

    vue或angular 写filter时,传入的是对象时一定注意,不能直接改变对象的值,因为在使用该filter的页面上,若传入的对象其他值发生变化,该filter也会重新运行,这样可能会报错,如下例 ...

  8. maven项目报错xxx cannot be resolved to a type

    同一个maven项目下的不同模块,无法导入其他模块的类,其他模块的所有类都报xxx cannot be resolved to a type 解决方案(参考): 项目Build Path --> ...

  9. 将GPS获得的UTC时间转换成本地时间的方法(转)

    源: 将GPS获得的UTC时间转换成本地时间的方法 参考: gps时转换为utc时间方法

  10. Python概念-上下文管理协议中的__enter__和__exit__

    所谓上下文管理协议,就是咱们打开文件时常用的一种方法:with __enter__(self):当with开始运行的时候触发此方法的运行 __exit__(self, exc_type, exc_va ...