HDU Reorder the Books 找规律

来源:互联网 发布:数组tostring 编辑:程序博客网 时间:2024/05/24 23:12

题目:
http://acm.hdu.edu.cn/showproblem.php?pid=5500

这道题与之前做过的一道题很像,但比那道题简单;

思路:对于一个序列,我们最多用n-1不就可以将它排好序;
而最大值一定在最下面,所以我们先找到最大值的位置;
找出从它开头向上找出连续的子序列tot,这个子序列上的数可以不用移动;

ans=ans-tot

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int m,n;int a[1000001];int pos[1000001];void solve(){    scanf("%d",&n);    for(int i=1;i<=n;i++) a[i]=pos[i]=0;    for(int i=1;i<=n;i++) scanf("%d",&a[i]),pos[a[i]]=i;    int ans=n-1;    int i=pos[n],t=n-1;    while(true)    {        i--;        if(!i) break;        if(a[i]==t)             ans--,t--;    }    cout<<ans<<'\n';    return;}int main(){    cin>>m;    while(m--) solve();    return 0;}
原创粉丝点击