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之间, 有两个杯子, 你要把水倒到两个杯子 ...
随机推荐
- Canvas 画圆
原文地址:http://hi.baidu.com/lj2tj/item/557d8d1a65adfa721009b58b --------------------------------------- ...
- Request.params,Request.form,Request.querystring区别(转)
自己总结:---大致意思是: 1.传参的方式分为:get方式传参,post方式传参,request.cookies和request.servervariables 2.这几种获取参数方式获取的都是一个 ...
- redis学习——day01_redis简介与安装
一.Redis 简介 1.1 Redis是什么 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redi ...
- 四、HTML属性—— HTML 元素提供的附加信息
HTML属性 (1)属性一般描述于开始标签 (2)属性总是以名称/值对的形式出现,比如:name="value" (3)使用小写属性 HTML属性值 应该始终被包括在引号内. —— ...
- VMware虚拟机,vps忘记密码修改密码
因为业务经常会用到虚拟机,然后过不了多久,登录密码就用不了. 解决问题有两种办法,其中一种是通过Dos命令窗:还有一个就是通过制作pe启动盘来操作,进行密码重置,删除等等. 制作pe启动盘的方式有 ...
- Mybatis配置-简单的使用
导包 基本配置 配置mybatis.config.xml文档 <?xml version="1.0" encoding="UTF-8" ?> < ...
- 微软:正式发布针对 .NET Core的 Winform 设计器
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://devblogs.microsoft.com/dotnet/windows-for ...
- [JavaWeb基础] 005.Hibernate的配置和简单使用
1.Hibernate简介 Hibernate是一个基于Java的开源的持久化中间件,对JDBC做了轻量的封装.采用ORM映射机制,负责实现Java对象和关系数据库之间的映射,把sql语句传给数据库, ...
- Python--numpy中的tile()函数
首先是官方给的定以(我是用的VsCode,鼠标放置在tile上出现的),建议直接看后面的示例. def tile(A, reps) Construct an array by repeating ...
- xxd十六进制编辑器的安装
一.背景:在vi中使用命令:%!xxd无法进行十六进制编辑,为缺少xxd命令所致 二.yum直接安装xxd无法成功[root@ELK ~]# yum install xxd已加载插件:fastestm ...