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之间, 有两个杯子, 你要把水倒到两个杯子 ...
随机推荐
- 使用包时,报 xxx.default is not a function
最近做了一个导出功能,代码如下 import request from 'request-promise-native'; export default class Form { // 导出 @po ...
- js时间戳转为日期格式的方法
Date.prototype.Format = function(fmt){ var o = { "M+" : this.getMonth()+1, //月份 "d+&q ...
- 腾讯云的ftp搭建
一开始配置完后 发现用xftp链接登录成功 但是列表无法列举出来 然后又用力cmd.filezilla发现都是一个主动被动问题 pasv_enable=YES.pasv_min_port=60000. ...
- HDU6097 Mindis
题目链接:https://cn.vjudge.net/problem/HDU-6097 知识点: 计算几何.圆的反演 题目大意: 已知一个圆心在原点的圆的半径,再给定 \(P, Q\) 两点坐标( \ ...
- UVALive5846
题目大意:见刘汝佳<算法竞赛入门经典——训练指南>P173. 解题思路: 如果要直接求所有单色三角形的个数似乎不简单,正难则反,先求出所有非单色三角形 cnt,answer = C(n,3 ...
- JavaScript实现单向链表结构
参考资料 一.什么是链表结构? 1.1.简介 链表和数组一样, 可以用于存储一系列的元素, 但是链表和数组的实现机制完全不同,链表中的元素在内存不是连续的空间,链表的每个元素由一个存储元素本身(数据) ...
- OPCUA+MQTT构建物联网通用框架
写在前面: 为了应对标准化和跨平台的趋势,更好的推广OPC,OPC基金会在OPCDA成功应用的基础上推出了一个新的OPC标准——OPC UA,OPCUA不再基于分布式组件对象模型(DCOM),而是以面 ...
- 特效 css3 渐变背景框
.box{ 子级 position: relative; width: 300px; height: 400px; display: flex; justify-content: center; al ...
- C#正则表达式基础
namespace ---> System.Text.RegularExpressions. static void Main(string[] args) { // if (IsInputMa ...
- Dubbo源码阅读-服务导出
Dubbo服务导出过程始于Spring容器发布刷新事件,Dubbo在接收到事件后,会立即执行服务导出逻辑.整个逻辑大致可分为三个部分,第一部分是前置工作,主要用于检查参数,组装URL.第二部分是导出服 ...