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. MySQL复制经常使用拓扑结构具体解释

    复制的体系结构有下面一些基本原则: (1)    每一个slave仅仅能有一个master: (2)    每一个slave仅仅能有一个唯一的serverID: (3)    每一个master能够有 ...

  2. 关于function的一种常用用法

    关于function的一种常用用法 void Share::InitAcrossManager() { GsMgrEvent gsMgrEvents;//保存function的结构体 gsMgrEve ...

  3. python 深复制与浅复制------copy模块

    模块解读: 浅复制: x = copy.copy(y)深复制: x = copy.deepcopy(y)(注:模块特有的异常,copy.Error) 深copy与浅copy的差别主要体现在当有混合对象 ...

  4. 【BZOJ3105】[cqoi2013]新Nim游戏 贪心+线性基

    [BZOJ3105][cqoi2013]新Nim游戏 Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个 ...

  5. hdu_吃糖果(思维题)

    吃糖果 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...

  6. [原创]将本地代码共享到github的操作步骤

    将本地代码共享到github的操作步骤 本地代码目录执行如下命令,初始化为git仓库. git init 到github上新建一个仓库,假设为https://github.com/sky0014/sk ...

  7. MySQL删除相同前缀的表,修改某个库的存储引擎

    MySQL5.0 之后,提供了一个新的数据库information_schema,用来记录MySQL总的元数据信息.元数据指的是 数据的数据. 比如表名.列名.列类型.索引名等表的各种属性名称.这个库 ...

  8. Eclipse +Tomcat配置-【菜鸟学JAVA】

    说起来也惭愧,以前用(Unieap)工具搞了一年多的JAVA+Oracle,现在居然在Eclipse中配置tomcat都搞了半天时间,原来一直是做.NET的. 今天开始不再用集成的环境,话不多说,开始 ...

  9. Android Resources

    Ref:Android开发最佳实践 Ref:Android高手速成--第一部分 个性化控件(View) Ref:Android高手速成--第二部分 工具库 Ref:Android高手速成--第三部分 ...

  10. JVM对象

    对象 Java虚拟机采用自动的内存管理和自适应的优化策略.但了解java虚拟机的运行机制和优化策略,写出适合java虚拟机管理的程序对性能提升是有意义的. 逃逸分析:对象的作用范围只在本线程范围,如方 ...