hihocoder #1233 : Boxes 北京赛区(2015)网络赛(状态压缩+bfs)
2016-09-24 09:48
288 查看
#1233 : Boxes
时间限制: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 abovea 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个卡槽,放有体积不同的n个空盒子,每次你可以移动一个空盒子到相邻卡槽,但前提是相邻卡槽若已经有空盒子,那么要移动的空盒子体积必须小于已有的空盒子,问要移动多少步才能使得从左到右,每个卡槽空盒子的体积递增。
现用①表示1号卡槽,②表示2号卡槽,依次类推……
样例1的移动方式:1->③,2->②,1->②,1->①
样例3的移动方式:95->②,95->①,96->③,95->②,95->③,97->②,95->②,95->①,96->②,95->②,95->③,96->①,95->②,95->①,97->③,95->②,95->③,96->②,95->②,95->①
题解:状态压缩,状态表示每个盒子所在的位置,每个盒子的位置用三位二进制表示,总共的状态为2^21,因为每次盒子只能向左放或者向右放,那么我们从结束的位置开始搜索,bfs预处理出目标状态到所有状态所需要的最小步数,然后对于每个询问O(1)回答即可。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5000000;
int n;
int a[10], b[10];
int vis[MAXN];
bool flag[10];
void bfs(int n)
{
queue<int> q;
int tmp = 0;
for(int i = 0; i < n; i++)
{
tmp += (i+1) * (1 << (3*i));
}//比如说n=7时,tem = 7 * 3^6 + 6 *3^5 + 5 * 3^4 + 4 * 3^3 + 3 * 3^2 + 2 * 3^1 + 1 * 3^0
q.push(tmp);
vis[tmp] = 0;
while(!q.empty())
{
int s = q.front();
q.pop();
memset(flag, 0, sizeof(flag));
for(int i = 0; i < n; i++) //i+1在哪个位置
{
int pos = (s >> (3*i)) % 8; //取第i+1个位置的盒子
if(flag[pos]) continue; //判断该盒子是否已经移动过
flag[pos] = 1;
if(pos>1 && !flag[pos-1])
{
int t = s - (1<<(3*i));//盒子左移
if(vis[t]==-1)
{
q.push(t);
vis[t] = vis[s] + 1;
}
}
if(pos<n && !flag[pos+1])
{
int t = s + (1<<(3*i));//盒子右移
if(vis[t]==-1)
{
q.push(t);
vis[t] = vis[s] + 1;
}
}
}
}
}
int main() {
int t;
cin >> t;
memset(vis, -1, sizeof(vis));
for(int i = 1; i < 8; i++){
bfs(i);
}
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]), b[i] = a[i];
}
sort(b+1, b+n+1);
for(int i = 1; i <= n; i++){
a[i] = lower_bound(b+1, b+n+1, a[i]) - b - 1;//因为移动的步数仅跟各盒子的大小关系有关,与具体体积大小无关,故记为1~n
}
int s = 0;
for(int i = 1; i <= n; i++)
{
s += i*(1<<(3*a[i]));//将各个盒子的位置表示成一个三位二进制表示数
}
printf("%d\n", vis[s]);
}
return 0;
}
相关文章推荐
- hihoCoder 1233 Boxes(状态压缩 + bfs)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 1007 Boxs(状态压缩+搜索)
- hiho 1233 Boxes(状态压缩+BFS)
- hihocoder #1234 : Fractal 北京赛区(2015)网络赛 (找规律)
- 2015北京赛区网络赛 Boxes
- hihoCoder 1231 Border Length(ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 E)
- HDU 4284 Travel 第37届ACM/ICPC 天津赛区网络赛1007题 (状态压缩DP)
- hihoCoder 1227 The Cats' Feeding Spots 2015北京赛区网络赛
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛A题
- 2015 ACM-ICPC国际大学生程序设计竞赛北京赛区网络赛 1002 Mission Impossible 6
- 北京赛区(2015)网络赛The Cats' Feeding Spots
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛H题2015-9-20
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 B Mission Impossible 6
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛A题2015-9-20
- hihoCoder 1227 The Cats' Feeding Spots && ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛
- hihoCoder 1231 Border Length(北京赛区(2015)网络赛 E)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 二分查找交点个数
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛 E题 hihoCoder 1231 Border Length
- 2015网络赛北京赛区G题(bfs+预处理)