Sequence in the Pocket

题目链接(点击)

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.

题意:

给出T组  每组一个序列 每次操作可以把其中的一个数移动到最前方 需要几次操作可以将序列变成从小到大

思路:

将序列从小到大排序 然后将新的序列从后往前每次枚举一个值 在原序列中查找出来num个  所以需要移动的次数是n-num

例如:

1 2 3 1 2 3 排序后是 1 1 2 2 3 3

依次枚举3 3 2 2  1 1

3 可以找到  j=n-1 时

3可以找到   j=n-4时

2可以找到   j=n-5时

在枚举第二个2的时候 就找不到了(j一直在减小)

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAX=1e5;
int main()
{
int a[MAX+5],b[MAX+5],T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b,b+n);
int j=n-1,num=0;
for(int i=n-1;i>=0;i--){ ///for+while 这种写法很好
while(b[i]!=a[j]&&j>=0){
j--;
}
if(j<0){
break;
}
else{
num++;
j--;
//printf("%d ",b[i]);
}
}
// printf("\n");
printf("%d\n",n-num);
}
return 0;
}

Sequence in the Pocket【思维+规律】的更多相关文章

  1. ZOJ - 4104 Sequence in the Pocket(思维+元素移至列首排序)

    Sequence in the Pocket Time Limit: 1 Second      Memory Limit: 65536 KB DreamGrid has just found an ...

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

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

  3. UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律

    UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...

  4. 1005:Number Sequence(hdu,数学规律题)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  5. II play with GG(思维规律)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 IG won the S champion ...

  6. ZOJ4104 Sequence in the Pocket(2019浙江省赛)

    思维~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int N; int main ( ...

  7. cf1216E2 Numerical Sequence (hard version)(思维)

    cf1216E2 Numerical Sequence (hard version) 题目大意 一个无限长的数字序列,其组成为\(1 1 2 1 2 3 1.......1 2 ... n...\), ...

  8. CF 1064B Equations of Mathematical Magic(思维规律)

    Description Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for. ...

  9. HDU 5881--Tea 思维规律

    感谢http://blog.csdn.net/black_miracle/article/details/52567718 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子 ...

随机推荐

  1. js中时间戳和时间格式之间的转换

    //时间格式转化 getNowDate(timestamp) { var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 va ...

  2. 【HTTP】Web服务器和HTTP的协作&HTTP首部

    用单台虚拟主机实现多个域名 Web服务器可以搭建多个独立域名的Web网站,也可以作为通信路径上的中转服务器提升效率. HTTP/1.1规范允许一台HTTP服务器搭建多个Web站点,提供Web托管服务的 ...

  3. 八皇后问题求解java(回溯算法)

    八皇后问题 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处 ...

  4. [COCOS2DX-LUA]0-005.cocos2dx中关于全面屏和折叠屏的适配的一些见解

    1.随着科技的发展,我们可以看到从iphoneX的刘海屏开始,引发了各种全面屏和异形屏的出现.这是科技的进步,但是对于各大的应用厂商来说,苦不堪言. 2.当然 ,吐槽归吐槽,我们还是要理智的去对待这个 ...

  5. 拨开云雾-Verilog是个大杂烩(中性)

    https://mp.weixin.qq.com/s/HKxX_79DtnXmFU1Mwt1GwA   一. 有意为之   Verilog是个大杂烩,这是有意而为之.   Verilog IEEE S ...

  6. 【Linux】将javaweb项目部署到CentOS的tomcat上

    1.将项目打包成war包 build之后war文件就生成了. 2.拷贝war文件到CentOS的tomcat的项目文件夹中 用WinSCP将文件粘帖进这个文件夹中 /wocloud/tomcat_cl ...

  7. DynamIQ扫盲文

    综述: ARM CPU的架构都基于big.LITTLE大小核技术.而再big.LITTLE的基础上,又添加了DynamIQ.单一Cluster中可以又8个core,且支持不同架构的core,以及支持不 ...

  8. Java实现 LeetCode 173 二叉搜索树迭代器

    173. 二叉搜索树迭代器 实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 示例: BSTIterator iterato ...

  9. Java实现 LeetCode 42 接雨水

    42. 接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这 ...

  10. Java GUI 鼠标事件

    import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.Mou ...