hihoCoder挑战赛12

时间限制:1000ms

单点时限:1000ms

内存限制:256MB

 

描述

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.

输入

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

输出

For each test case, print a line containing one integer indicating the answer. If it’s impossible to sort the boxes, print -1.

样例输入

4
3
2 1 3
2
7 8
2
10000 1000
3
97 96 95

样例输出

4
0
-1
20

题目看起来跟汉诺塔很像,但是条件更苛刻。

不过由于n < 8,所以状态数最多是1!+2!+3!+…7!。

但是如果想模拟移动的过程,在移动过程中箱子会重叠,这样的话,所有状态的数目应该是1^1+2^2+3^3…7^7。

这个状态数目不是特别大。

而且对于1,12,123,1234,12345,123456,1234567这种状态的值都为0.

于是,就可以从这些为0的状态出发,去模拟箱子移动的过程,然后bfs求得所有状态的解。相当于一个倒推的过程。

首先肯定需要把所有数处理成连续的数,比如123456这样。这个处理方法有很多。

但是状态的存储是一个问题。

可以用一个结构体来存,那么里面会有一个长度为7的数组index[i],表示大小为i的箱子所在的位置。这样的话需要map来存结果,但是需要重载小于号。

用string存的话可以不用重载小于号,但是也需要map来保存结果。

但是这里map的效率是logn,经测时间很长。。

也就是说这个logn不能乘上去,虽然理论上计算复杂度应该够,但是实际上1S内无法处理。

也是说状态跟结果的映射关系需要在常数时间内完成。

由于状态最大是7777777,也就是说直接用十进制int来存状态的话是可以用一维数组来完成的。

网上也有用二进制状压的,每三位表示一个箱子的位置,这样最多21位就能表示所有箱子的状态,也就是2097152。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; struct ARG
{
int val;
int id;
}arg[]; bool cmpARG(ARG x, ARG y)
{
return x.val < y.val;
} int n;
int s[];
int ans[]; void bfs(int cnt)
{
int k[], tmp, state, mi;
queue<int> q;
state = ;
for (int i = ; i <= cnt; ++i)
state = *state+i;
ans[state] = ;
q.push(state);
while (!q.empty())
{
state = q.front();
q.pop();
tmp = state;
for (int i = ; i < cnt; ++i)
{
k[cnt--i] = tmp%;
tmp /= ;
}
for (int i = ; i < cnt; ++i)
{
mi = -;
for (int j = ; j < i; ++j)
{
if (k[j] == k[i])
{
mi = j;
break;
}
}
if (mi != -)
continue;
if (k[i] > )
{
k[i]--;
mi = -;
for (int j = ; j < i; ++j)
{
if (k[j] == k[i])
{
mi = j;
break;
}
}
tmp = ;
for (int i = ; i < cnt; ++i)
tmp = *tmp+k[i];
if (ans[tmp] == - && mi == -)
{
ans[tmp] = ans[state]+;
q.push(tmp);
}
k[i]++;
}
if (k[i] < cnt)
{
k[i]++;
mi = -;
for (int j = ; j < i; ++j)
{
if (k[j] == k[i])
{
mi = j;
break;
}
}
tmp = ;
for (int i = ; i < cnt; ++i)
tmp = *tmp+k[i];
if (ans[tmp] == - && mi == -)
{
ans[tmp] = ans[state]+;
q.push(tmp);
}
k[i]--;
}
}
}
} void init()
{
memset(ans, -, sizeof(ans));
for (int i = ; i < ; ++i)
bfs(i);
} void input()
{
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
scanf("%d", &arg[i].val);
arg[i].id = i;
}
sort(arg, arg+n, cmpARG);
for (int i = ; i < n; ++i)
s[i]=arg[i].id+;
} void work()
{
if (n == )
return;
int k = ;
for (int i = ; i < n; ++i)
k = *k+s[i];
printf("%d\n", ans[k]);
} int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int i = ; i < T; ++i)
{
input();
work();
}
return ;
}

ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)的更多相关文章

  1. ACM学习历程—Hihocoder 1139 二分·二分答案(bfs)

    http://hihocoder.com/problemset/problem/1139 这题提示上写的是二分,但是感觉不二分应该也可以,至少题目是AC的... 二分的思想就是二分答案的值,看能不能在 ...

  2. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  3. ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))

    http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...

  4. ACM学习历程—Hihocoder 1290 Demo Day(动态规划)

    http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...

  5. ACM学习历程—Hihocoder 1288 Font Size(暴力 || 二分)

    http://hihocoder.com/problemset/problem/1288 这题是这次微软笔试的第一题,关键的是s的上限是min(w, h),这样s的范围只有到1000,这样就可以直接暴 ...

  6. ACM学习历程—Hihocoder [Offer收割]编程练习赛1

    比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...

  7. ACM学习历程—Hihocoder 1164 随机斐波那契(数学递推)

    时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 大家对斐波那契数列想必都很熟悉: a0 = 1, a1 = 1, ai = ai-1 + ai-2,(i > 1). ...

  8. ACM学习历程—Hihocoder 1178 计数(位运算 && set容器)(hihoCoder挑战赛12)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB   描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物,鱼丸. 魔法n能召 ...

  9. ACM学习历程—Hihocoder 1177 顺子(模拟 && 排序 && gcd)(hihoCoder挑战赛12)

      时间限制:6000ms 单点时限:1000ms 内存限制:256MB   描述 你在赌场里玩梭哈,已经被发了4张牌,现在你想要知道发下一张牌后你得到顺子的概率是多少? 假定赌场使用的是一副牌,四种 ...

随机推荐

  1. swift中的?和!理解

    本文转载至 http://www.cnblogs.com/dugulong/p/3770367.html 首先贴cocoachina上某位大大的帖子:     Swift语言使用var定义变量,但和别 ...

  2. 【BZOJ3707】圈地 几何

    [BZOJ3707]圈地 Description 2维平面上有n个木桩,黄学长有一次圈地的机会并得到圈到的土地,为了体现他的高风亮节,他要使他圈到的土地面积尽量小.圈地需要圈一个至少3个点的多边形,多 ...

  3. 【BZOJ3744】Gty的妹子序列 分块+树状数组

    [BZOJ3744]Gty的妹子序列 Description 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzo ...

  4. EasyDSS流媒体服务器灵活地帮助用户实现摄像机RTSP转RTMP直播功能

    简要描述 今天突然接到国内某上市公司同事打来的技术咨询电话,经过简单的沟通,大概所描述的需求是: 1.目前现场有非常多的摄像机资源需要接入: 2.需要将摄像机的RTSP流转成RTMP流接入到微信小程序 ...

  5. Cloneable 和clone的区别和联系

    设计模式----原型模式时候,涉及到的复制克隆, Cloneable 接口,内部是没有任何方法的, 这个接口其实是一个标记性的接口,和Serializable是一样的,都是标记使用, 在类实现了这个C ...

  6. 我的Android进阶之旅------>Android嵌入图像InsetDrawable的用法

    面试题:为一个充满整个屏幕的LinearLayout布局指定背景图,是否可以让背景图不充满屏幕?请用代码描述实现过程. 解决此题,可以使用嵌入(Inset)图像资源来指定图像,然后像使用普通图像资源一 ...

  7. 6.让ORM映射执行的时候打印SQL语句

    配置Django日志:\hello_django\hello_django\settings.py 文件中的 LOGGING 加入如下配置: LOGGING = { 'version': 1, 'di ...

  8. Failed to decode response: zlib_decode(): data error Retrying with degraded;

    composer update的时候出现: Failed to decode response: zlib_decode(): data error Retrying with degraded: 执 ...

  9. pom.xml配置文件详解(转发)

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  10. 【整理学习Hadoop】Hadoop学习基础之一:服务器集群技术

            服务器集群就是指将很多服务器集中起来一起进行同一种服务,在客户端看来就像是只有一个服务器.集群可以利用多个计算机进行并行计算从而获得很高的计算速度,也可以用多个计算机做备份,从而使得任 ...