您的位置:首页 > 其它

Reorder the Books HDU - 5500

2016-12-17 20:53 435 查看
题意:

书号从上到下,从大到小。每次只能移动一本书。问最少移动几次可以还原

思路:

拿到最大的书开始往上找是否存在次大的。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 200005
using namespace std ;
int a[maxn];
int main()
{
int T;
cin>>T;
for(int Case=1;Case<=T;Case++)
{
int n,pos;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==n)
{
pos=i;
}
}
int ans=0;
ans=n-pos;
int need=n-1;
for(int i=pos-1;i>=1;i--)
{
if(a[i]==need)
{
need--;
}
else
{
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: