ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)
时间限制: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北京网赛)的更多相关文章
- ACM学习历程—Hihocoder 1139 二分·二分答案(bfs)
http://hihocoder.com/problemset/problem/1139 这题提示上写的是二分,但是感觉不二分应该也可以,至少题目是AC的... 二分的思想就是二分答案的值,看能不能在 ...
- ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)
http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...
- ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))
http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- ACM学习历程—Hihocoder 1288 Font Size(暴力 || 二分)
http://hihocoder.com/problemset/problem/1288 这题是这次微软笔试的第一题,关键的是s的上限是min(w, h),这样s的范围只有到1000,这样就可以直接暴 ...
- ACM学习历程—Hihocoder [Offer收割]编程练习赛1
比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...
- ACM学习历程—Hihocoder 1164 随机斐波那契(数学递推)
时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 大家对斐波那契数列想必都很熟悉: a0 = 1, a1 = 1, ai = ai-1 + ai-2,(i > 1). ...
- ACM学习历程—Hihocoder 1178 计数(位运算 && set容器)(hihoCoder挑战赛12)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物,鱼丸. 魔法n能召 ...
- ACM学习历程—Hihocoder 1177 顺子(模拟 && 排序 && gcd)(hihoCoder挑战赛12)
时间限制:6000ms 单点时限:1000ms 内存限制:256MB 描述 你在赌场里玩梭哈,已经被发了4张牌,现在你想要知道发下一张牌后你得到顺子的概率是多少? 假定赌场使用的是一副牌,四种 ...
随机推荐
- 关于function的一种常用用法
关于function的一种常用用法 void Share::InitAcrossManager() { GsMgrEvent gsMgrEvents;//保存function的结构体 gsMgrEve ...
- Jmeter 05 JMeter元件详解
1. JMeter 逻辑控制器 Switch条件控制器.While条件控制器.交替控制器.仅一次控制器.随机控制器.随机顺序控制器.条件控制器(如果(if)).循环控制器.录制控制器.ForEach控 ...
- 【BZOJ3488】[ONTAK2010]Highways 扫描线+树状数组
[BZOJ3488][ONTAK2010]Highways Description 给一棵n个点的树以及m条额外的双向边q次询问,统计满足以下条件的u到v的路径:恰经过一条额外的边不经过树上u到v的路 ...
- 【BZOJ3769】spoj 8549 BST again DP(记忆化搜索?)
[BZOJ3769]spoj 8549 BST again Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第 ...
- SpringMVC结合REST
一.什么是REST REST即 Representational State Transfer,也就是(资源)表现层状态转化.资源是指网络上的一个实体或者说是网络上的一个具体信息. 每种资源对应一个特 ...
- Action获取表单数据的三种方式
1.使用ActionContext类获取 示例 获取用户提交的用户名和密码 jsp页面 action中的java代码 2.使用ServletActionContext类获取 jsp页面 Java代码 ...
- Jquery点击事件出发顺序
鼠标点击触发事件执行顺序: mouse down -> mouse up -> click 键盘点击出发事件执行顺序: 点击后马上抬起:key down -> key press - ...
- 读取ByteBuffer有效的数据
转:https://zhidao.baidu.com/question/427134449349230532.html说道 ByteBuffer的缓冲区,就需要知道缓冲区的的三个状态1)capacit ...
- Java并发之ArrayBlockingQueue
ArrayBlockingQueue是一个由数组支持的有界阻塞队列.此队列按 FIFO(先进先出)原则对元素进行排序.队列的头部是在队列中存在时间最长的元素.队列的尾部是在队列中存在时间最短的元素.新 ...
- Data Structure Stack: Infix to Postfix
http://geeksquiz.com/stack-set-2-infix-to-postfix/ #include <iostream> #include <vector> ...