您的位置:首页 > 其它

HDU 5500 Reorder the Books (水题)

2015-10-10 23:19 344 查看
题意:

  有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次?

思路:

  如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽出来。抽出来的书必定可以回到它最终的位置上,即每本书最多抽出来1次,可以想象抽出来后直接扔掉,在剩下的书中找到一个有序,且最后一个元素为n,且等差为1的序列{...n-2,n-1,n}。

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <deque>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define back que[rear-1]
#define INF 0x7f7f7f7f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI  = acos(-1.0);
const int N=30;

int n, a
;
int cal()
{
for(int i=n; i>0; i--)
if(a[i]==n)    n--;
return n;
}

int main()
{
//freopen("input.txt", "r", stdin);
int t;cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d", &a[i]);
cout<<cal()<<endl;
}
return 0;
}


AC代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: