Sequence in the Pocket【思维+规律】
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【思维+规律】的更多相关文章
- ZOJ - 4104 Sequence in the Pocket(思维+元素移至列首排序)
Sequence in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has just found an ...
- The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored E.Sequence in the Pocket(思维题)
传送门 题意: 给出一个序列,你可以将任意一个数移到最前面: 求最少需要移动多少次,可以是此序列变成非递减序列: 思路: 定义 (ai,aj) 为逆序对 ( i < j , ai > aj ...
- UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律
UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...
- 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 ...
- II play with GG(思维规律)
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 IG won the S champion ...
- ZOJ4104 Sequence in the Pocket(2019浙江省赛)
思维~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int N; int main ( ...
- cf1216E2 Numerical Sequence (hard version)(思维)
cf1216E2 Numerical Sequence (hard version) 题目大意 一个无限长的数字序列,其组成为\(1 1 2 1 2 3 1.......1 2 ... n...\), ...
- CF 1064B Equations of Mathematical Magic(思维规律)
Description Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for. ...
- HDU 5881--Tea 思维规律
感谢http://blog.csdn.net/black_miracle/article/details/52567718 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子 ...
随机推荐
- easyui API
http://www.jeasyuicn.com/api/docTtml/index.htm
- vs code插件自动压缩 min.css
我们在进行相应的项目书写的时候,有些需要把scss 和 css 进行 压缩成 min.css 便于更好的使用 在这里强调一下 scss 后来才慢慢接触到这个语言的 感觉的确实懂得明白了之后 好用而且 ...
- 使用EditPlus根据指定字符批量换行,快速填充Postman请求参数键值对
1.当某个.ext格式的文件中的重复格式内容太多时,而又想要根据某个字符进行批量换行时,那么可以使用EditPlus进行批量换行. 在开发过程中就会经常遇到这种问题,比如把Url的请求参数,快速的填写 ...
- springboot整合mybatis报错
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more ...
- Maven系列(二) -- 将开源库上传到maven仓库私服
前言 之前简单说了下Maven的搭建,现在跟大家说一下如何将自己的aar传到我们新搭建的maven仓库里面,接下来我们就从最基本的新建一个library开始讲述整个流程,话不多说,让我们把愉快的开始吧 ...
- 微信小程序初探--写个扫雷分享给你玩
闲暇里,想学一下微信小程序, 于是,用微信小程序原生做了个扫雷玩. 以下略作总结,分享给大家. 微信里下拉,输入[mini计算器], 看到这个图标的就是了: 说好的扫雷,怎么变成计算器了?原因后面解释 ...
- Unable to start services. See log file /tmp/vmware-root/vmware-6853.log for details.
debian安装vmware错误 https://github.com/AdministratorGithub/vmshell vm15.1.0解决linux安装出现Unable to start s ...
- 生成随机字符串 php
/** +---------------------------------------------------------- * 生成随机字符串 +------------------------- ...
- day07 作业
作业(必做题):#1. 使用while循环输出1 2 3 4 5 6 8 9 10count=0while count<11: if count==7: count+=1 continue pr ...
- PAT1034 有理数四则运算 (20分)
1034 有理数四则运算 (20分) 本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全 ...