Sequence in the Pocket


Time Limit: 1 Second      Memory Limit: 65536 KB

DreamGrid has just found an integer sequence  in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move it to the beginning of the sequence.

What's the minimum number of operations needed to make the sequence non-decreasing?

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of the sequence.

The second line contains  integers  (), indicating the given sequence.

It's guaranteed that the sum of  of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

2
4
1 3 2 4
5
2 3 3 5 5

Sample Output

2
0

Hint

For the first sample test case, move the 3rd element to the front (so the sequence become {2, 1, 3, 4}), then move the 2nd element to the front (so the sequence become {1, 2, 3, 4}). Now the sequence is non-decreasing.

For the second sample test case, as the sequence is already sorted, no operation is needed.

题意:给定一个序列,每次可以把一个元素移到列首(最左边),求最少移几次使其有序(非降序)

(之前做过类似的题,可以把元素移到首或尾,思路相似)

思路:因为左移,可以肯定移动的都是较小值,若要保证操作次数最少,最大值一定不需要移动。

所以先排好序,确定之间的相对大小,然后找到最大值位置(因为有相同元素so从右往左找)

再从最大值往左找次大值,因为除最大值外,次大值就是当前最大值,所以同样不需要移动。

以此类推,直到找到最左边结束,我们找到的值都是不需要移动的,那么用总个数n减不需移动的个数ans即为需要移动的个数,解保证最小。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int a[],b[]; int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int ma=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+);
int ans=;
for(i=n;i>=;i--){
if(a[i]==b[n]){
int c=n;
for(j=i;j>=;j--){
if(a[j]==b[c]){
c--;
ans++;
}
}
break;
}
}
printf("%d\n",n-ans);
}
return ;
}

ZOJ - 4104 Sequence in the Pocket(思维+元素移至列首排序)的更多相关文章

  1. Sequence in the Pocket【思维+规律】

    Sequence in the Pocket 题目链接(点击) DreamGrid has just found an integer sequence  in his right pocket. A ...

  2. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  3. 怎么实现元素ol的降序排序显示

    首先介绍一下什么是ol元素.这里直接引用MDN里面的定义:The HTML <ol> Element (or HTML Ordered List Element) represents a ...

  4. 用Jquery控制元素的上下移动 实现排序功能

    在页面上,控制元素上下移动,进行排序是我们比较常用的功能,今天我用jQuery 写个 简单方便,功能齐全的实现方式. 话不多说,直接上代码,下面是基础的引入jq和html元素部分: <scrip ...

  5. 如何使用 Java 对 List 中每个对象元素按时间顺序进行排序

    如何使用 Java 对 List 中每个对象元素按时间顺序进行排序 Java 实现 import java.text.SimpleDateFormat; import java.util.ArrayL ...

  6. The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored E.Sequence in the Pocket(思维题)

    传送门 题意: 给出一个序列,你可以将任意一个数移到最前面: 求最少需要移动多少次,可以是此序列变成非递减序列: 思路: 定义 (ai,aj) 为逆序对 ( i < j , ai > aj ...

  7. Sequence(组合数学,集合不同元素的个数)

    Sequence [组合数学] 时间限制: 3 Sec  内存限制: 128 MB 提交: 138  解决: 52 [提交][状态][讨论版] 题目描述 在某个夜黑月高的晚上,!!!,原谅我编不下去了 ...

  8. ZOJ 4110 Strings in the Pocket (马拉车+回文串)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110 题目: BaoBao has just found two s ...

  9. ZOJ 3955:Saddle Point(思维)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3955 题意:给出一个n*m的矩阵,定义矩阵中的特殊点Aij当且仅当Aij是 ...

随机推荐

  1. 关于eclipse 插件的挂载

    学习java的时候,不喜欢myeclipse 这种插件,什么都准备好了,自己动手就少了,不利于自己学习,现在我就diy 自己选几个插件来用,基本上就是 eclipse 加上我自己要用的插件,插件的安装 ...

  2. 九度OJ 1011:最大连续子序列 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5615 解决:2668 题目描述:     给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, N ...

  3. 九度OJ 1054:字符串内排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7949 解决:4343 题目描述: 输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串. 输入: 测试数据有多组,输 ...

  4. Swift语言概览

                     Swift语言概览 关于 这篇文章简要介绍了苹果于WWDC 2014公布的编程语言--Swift. 前言 在这里我觉得有必要提一下Brec Victor的Invent ...

  5. Git如何强制拉取一个远程分支到本地分支(转载)

    有时候,我们在使用git pull指令想把一个远程分支拉取到本地分支的时候,老是会拉取失败,这一般是因为某种原因,本地分支和远程分支的内容差异无法被git成功识别出来,所以git pull指令什么都不 ...

  6. touchweb手机网站图片加载方法(canvas加载和延迟加载)

    一.canvas图片加载 关于canvas加载,我的方法是,将文章中所有用到图片的地方,都用canvas代替,给canvas一个data-src,里面存放img的路径,通过canvas方法渲染图片.因 ...

  7. AppStore App申请审核加速

    容芳志大牛一直是我学习的榜样 分类: iOS开发经验技巧2014-11-12 09:40 409人阅读 评论(0) 收藏 举报 有没有遇到上线后发现很严重的bug这种情况,修复bug后提交审核又是漫长 ...

  8. 吴恩达机器学习笔记(二) —— Logistic回归

    主要内容: 一.回归与分类 二.Logistic模型即sigmoid function 三.decision boundary 决策边界 四.cost function 代价函数 五.梯度下降 六.自 ...

  9. SQL Server 2008R2 代理服务-开启

    ,点击开始菜单-所有程序-SQLServer2008R2-配置工具-SQLServer配置管理器 2,选择SQLServer服务 3,找到SqlServer代理(MSSQLSERVER),双击或右键选 ...

  10. 时尚创意VI矢量设计模板

    时尚创意VI矢量设计模板 创意VI VI设计 企业VI 时尚背景 信封设计 封面设计 杯子 桌旗 帽子 EPS矢量素材下载 http://www.huiyi8.com/vi/