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之间, 有两个杯子, 你要把水倒到两个杯子 ...
随机推荐
- Windows Terminal安装并美化
介绍 Windows Teminal是一款新式.快速.高效.强大的终端应用程序,适用于命令行工具.命令提示符.PowerShell.WSL(Linux子系统)等等的Shell用户,主要功能包括多选项卡 ...
- poj3177 无向连通图加多少条边变成边双连通图
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15752 Accepted: 6609 ...
- 模板:list列表显示
作为视图,担当的角色就是显示数据.所以关键就是,借助JSTL的c:forEach标签遍历从CategoryServlet的list()的request.setAttribute("thecs ...
- h5+ 开发分享功能
h5+ 开发微信.QQ分享功能 此处只做微信网页分享示例代码 方式一.JS+HTML+h5Plus 1.html代码 <div class="button" onclick= ...
- Fundamental ES6 Part-I
Exercise-01 with Solution Write a JavaScript program to compare two objects to determine if the firs ...
- Java基本语法---标识符、变量、数据类型转换及进制
Java基本语法 标识符 标识符:凡事可以自己起名字的地方,都可以叫做标志符 标识符命名规则: 26个字母大小写,数字0-9,下划线_,美元符号$ 数字不能开头 不能使用关键字和保留字,但是可以包含 ...
- 微信小程序订阅
微信小程序订阅 摘要 1.基于promise封装微信小程序订阅 2.解决由于微信基础库版本低下的兼容 3.解决“总是保持以上选择,不再询问”的取消状态 主要运用API: requestSubscrib ...
- eatwhatApp开发实战(十)
android应用中,很少有一个activity的app,这次我们设置一个activity,通过listview的点击跳转并显示对应的商店信息. 首先创建类ShopInfoActivity,对应设置其 ...
- 【转】从一副扑克牌中随机抽取N张
该问题为产生不重复的随机数序列,形象点就是一副扑克牌中随机抽取N张. 摘自:不重复随机数列生成算法 改了一部分 /** * 从0-max随机选N个数出来 * **/ public static int ...
- 一篇文章看清楚 Linux 的职业发展方向
手机.汽车.甚至宇宙飞船,在今天的科技世界中,你几乎到处都能看到 Linux 的身影.前两天 SpaceX 成功将宇航员送入太空的猎鹰9号火箭与龙飞船用的也是 Linux的操作系统.身处与 Linux ...