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张牌,现在你想要知道发下一张牌后你得到顺子的概率是多少? 假定赌场使用的是一副牌,四种 ...
随机推荐
- 合理的布局,绚丽的样式,谈谈Winform程序的界面设计
转载,不错的学习文章 阅读后,起初不太明白,试验了几次后明白了dev的强大.从事Winform开发很多年了,由于项目的需要,设计过各种各样的界面效果.一般来说,运用传统的界面控件元素,合理设计布局,能 ...
- 洛谷P2402 奶牛隐藏
洛谷P2402 奶牛隐藏 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛混乱的原因看题目描述) 题目描述 在一个农场里有n块田地. ...
- Linux安装virtualenvwrapper详细步骤
1.[root@localhost ~]# pip install virtualenvwrapper 2.[root@localhost ~]# pip list [root@localhost ~ ...
- 如何从统计中批量获取BD搜索关键词及对应的入口页面?
前面我们介绍了通过cnzz的访问明细获取到搜索关键词及对应的入口页面,但是从BD搜索进来的关键词无法完整显示,只能呈现一些bd图片搜索的关键词,这是因为百度宣布从去年5月开始逐渐取消了referer关 ...
- Bootstrap学习4--Table样式(转载:https://blog.csdn.net/Fanbin168/article/details/53208869)
备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html 将<table>标签添加class=‘tab ...
- 免费好用的Diff和Merge工具大总结
总结:比较下来:diffmerge和P4merge最好用,kdiff比较专业些,支持自动merge. 一 csdiff 下载:http://www.componentsoftware.com/Prod ...
- pinpoint改造支持查询
原架构 改造后架构
- php基本语法与函数
1.标记与注释 <?php 代码 ?> 用/* */注释一段代码, 用 // 注释一行代码 /** */文档注释 注意:若php下面只有php代码没有别的代码,那么最好不要加 ...
- Facial landmark detection - 人脸关键点检测
Facial landmark detection (Facial keypoints detection) OpenSourceLibrary: DLib Project Home: http: ...
- spring-boot4
1.1.1. Starter pom 除了官方也有其他第三方提供的starter Websocket是服务端推数据到客户端.长连接. 1.1.1.Xml 配置文件 有些时候必须使用xml配置. 1.1 ...