hihocoder 1233 Boxes
2015-09-22 12:19
246 查看
题意:给出n个不同的数,每次可以把一个较小的那个数移到相邻的较大的那个数的上面,问最少需要移动多少次使得这n个数是升序的。
做法:从升序状态bfs出到其它状态的最小步数即可,状态s:从左到右,第i个数在第j个位置,例如
序列312表示成231,因为1在第2个位置,2在第3个位置,3在第1个位置。
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int *dp[8];
int a[8],fac[8];
void bfs(int n)
{
int x=0;
for(int i=1;i<=n;i++)
x=x*10+i;
queue<int>qq;
qq.push(x);
dp
[x]=0;
while(qq.size())
{
int from=qq.front();
qq.pop();
memset(a,0,sizeof(a));
x=from;
for(int i=n;i>0;i--,x/=10)
a[x%10]=i;
for(int i=1;i<=n;i++)
if(a[i]!=0)
{
if(i>1&&(a[i]<a[i-1]||a[i-1]==0))
{
int to=from-fac[n-a[i]];
if(dp
[to]==-1)
{
qq.push(to);
dp
[to]=dp
[from]+1;
}
}
if(i<n&&(a[i]<a[i+1]||a[i+1]==0))
{
int to=from+fac[n-a[i]];
if(dp
[to]==-1)
{
qq.push(to);
dp
[to]=dp
[from]+1;
}
}
}
}
}
int main()
{
fac[0]=1;
for(int i=1;i<=7;i++)
fac[i]=fac[i-1]*10;
int x=0;
for(int i=1;i<=7;i++)
{
x=x*10+8;
dp[i]=new int[x+1];
memset(dp[i],-1,sizeof(int)*x);
bfs(i);
}
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
x=0;
for(int i=1;i<=n;i++)
{
int sum=0;
for(int j=1;j<=n;j++)
if(a[i]>=a[j])
sum++;
x+=i*fac[n-sum];
}
printf("%d\n",dp
[x]);
}
}
时间限制: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.
样例输入
样例输出
做法:从升序状态bfs出到其它状态的最小步数即可,状态s:从左到右,第i个数在第j个位置,例如
序列312表示成231,因为1在第2个位置,2在第3个位置,3在第1个位置。
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int *dp[8];
int a[8],fac[8];
void bfs(int n)
{
int x=0;
for(int i=1;i<=n;i++)
x=x*10+i;
queue<int>qq;
qq.push(x);
dp
[x]=0;
while(qq.size())
{
int from=qq.front();
qq.pop();
memset(a,0,sizeof(a));
x=from;
for(int i=n;i>0;i--,x/=10)
a[x%10]=i;
for(int i=1;i<=n;i++)
if(a[i]!=0)
{
if(i>1&&(a[i]<a[i-1]||a[i-1]==0))
{
int to=from-fac[n-a[i]];
if(dp
[to]==-1)
{
qq.push(to);
dp
[to]=dp
[from]+1;
}
}
if(i<n&&(a[i]<a[i+1]||a[i+1]==0))
{
int to=from+fac[n-a[i]];
if(dp
[to]==-1)
{
qq.push(to);
dp
[to]=dp
[from]+1;
}
}
}
}
}
int main()
{
fac[0]=1;
for(int i=1;i<=7;i++)
fac[i]=fac[i-1]*10;
int x=0;
for(int i=1;i<=7;i++)
{
x=x*10+8;
dp[i]=new int[x+1];
memset(dp[i],-1,sizeof(int)*x);
bfs(i);
}
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
x=0;
for(int i=1;i<=n;i++)
{
int sum=0;
for(int j=1;j<=n;j++)
if(a[i]>=a[j])
sum++;
x+=i*fac[n-sum];
}
printf("%d\n",dp
[x]);
}
}
时间限制: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
相关文章推荐
- css初始化代码
- IOS视图旋转可放大缩小
- 简单随机密码生成
- javascript删除提示弹出况确认
- oracle left join优化
- 机器学习算法基础概念学习总结
- Android中home键和back键区别实例分析
- 日经春秋 20150922
- 新博客地址
- 线段树详解及模板 (转载)
- 有移动规则2
- buffer和cache有什么本质区别
- (25)JAVA.IO--使用Java创建指定大小的空文件
- Hadoop与Spark等数据处理系统哪个是最好的?
- 自定义ImageView实现圆角矩形
- 天声人語 20150922
- Android 自定义View 实现垂直滑动页
- Unity图片变灰的方式
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- 关于tomcat startup.bat启动后一闪而过的问题(转)