HDU 5500 Reorder the Books
2017-11-07 21:02
447 查看
题目
https://vjudge.net/problem/HDU-5500题意是说给你一个打乱顺序的区间,要求变成从小到大排列。每次移动一个数的时候都要把这个数放到区间的最前面,问最少的变成目标区间的操作次数。
题解
http://blog.csdn.net/Loi_black/article/details/78441083原题的数据范围很水,可以做到O(N*T)
考虑最糟糕的情况,我们固定最大的元素不动,把其他所有元素按照一定顺序向上丢,这样一定能得到答案。
如果有一个比最大数小的次大数在最大数的上方,显然这个次大数是不必被丢的,ans-1
,同理,次大和次次大也是这样
【困成狗,看black学长博客吧】
代码
#include<iostream> #include<cstdio> using namespace std; const int N=1000000+50; int a ,mp ; int n,nowwz,now,ans; int main(){ int t; scanf("%d",&t); while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++){ scanf("%d",&a[i]); mp[a[i]]=i; } ans=n-1; int now=n,nowwz=mp[now]; for(;;){ if(now==1) break; now--; if(nowwz>mp[now]){ ans--; nowwz=mp[now]; }else break; } printf("%d\n",ans); } }
相关文章推荐
- hdu 5500 Reorder the Books(规律题)
- Reorder the Books HDU - 5500
- Reorder the Books HDU 5500
- HDU 5500 Reorder the Books(模拟)
- HDU 5500 - Reorder the Books(模拟)
- hdu 5500 Reorder the Books(规律)
- HDU 5500 Reorder the Books (水题)
- HDU 5500 Reorder the Books
- hdu 5500 Reorder the Books
- HDU_5500 Reorder the Books
- Reorder the Books-HDU-5500-BC59div2
- HDU 5500 Reorder the Books
- Reorder the Books hdu 5500 贪心
- hdu 5500 Reorder the Books 【BestCoder Round #59 (div.2) 第二题】
- hdu 5500 Reorder the Books
- HDU 5500 Reorder the Books (规律)
- HDU 5500 Reorder the Books【水题】
- HDU 5500 Reorder the Books 贪心
- HDU 5500 Reorder the Books(贪心+思维)
- HDU 5500 Reorder the Books