http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=4208
注意题目是让求趟数,所以比较原序列与排序后序列中位置相差最大的就是答案。
PS:也可以用反序表来思考。参见《计算机程序设计艺术》第三卷5.1.1
完整代码:
/*136ms,1408KB*/
#include<cstdio>
#include<algorithm>
using namespace std;
int a[10005], Map[10005];
int main()
{
int n, t, i, maxn;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
Map[a[i]] = i;
}
sort(a, a + n);
maxn = 0;
for (i = 0; i < n; ++i)
maxn = max(maxn, Map[a[i]] - i);
printf("%d\n", maxn);
}
return 0;
}

本文介绍了如何通过比较原始序列与排序后序列中元素的位置差异来找出最大差距的方法,并提供了相应的代码实现。适用于理解序列排序算法和数组操作的基本技巧。
119

被折叠的 条评论
为什么被折叠?



