ZOJ - 4104 Sequence in the Pocket(思维+元素移至列首排序)
Sequence in the Pocket
Time Limit: 1 Second Memory Limit: 65536 KB
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.
题意:给定一个序列,每次可以把一个元素移到列首(最左边),求最少移几次使其有序(非降序)
(之前做过类似的题,可以把元素移到首或尾,思路相似)
思路:因为左移,可以肯定移动的都是较小值,若要保证操作次数最少,最大值一定不需要移动。
所以先排好序,确定之间的相对大小,然后找到最大值位置(因为有相同元素so从右往左找)
再从最大值往左找次大值,因为除最大值外,次大值就是当前最大值,所以同样不需要移动。
以此类推,直到找到最左边结束,我们找到的值都是不需要移动的,那么用总个数n减不需移动的个数ans即为需要移动的个数,解保证最小。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int a[],b[]; int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int ma=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+);
int ans=;
for(i=n;i>=;i--){
if(a[i]==b[n]){
int c=n;
for(j=i;j>=;j--){
if(a[j]==b[c]){
c--;
ans++;
}
}
break;
}
}
printf("%d\n",n-ans);
}
return ;
}
ZOJ - 4104 Sequence in the Pocket(思维+元素移至列首排序)的更多相关文章
- Sequence in the Pocket【思维+规律】
Sequence in the Pocket 题目链接(点击) DreamGrid has just found an integer sequence in his right pocket. A ...
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 怎么实现元素ol的降序排序显示
首先介绍一下什么是ol元素.这里直接引用MDN里面的定义:The HTML <ol> Element (or HTML Ordered List Element) represents a ...
- 用Jquery控制元素的上下移动 实现排序功能
在页面上,控制元素上下移动,进行排序是我们比较常用的功能,今天我用jQuery 写个 简单方便,功能齐全的实现方式. 话不多说,直接上代码,下面是基础的引入jq和html元素部分: <scrip ...
- 如何使用 Java 对 List 中每个对象元素按时间顺序进行排序
如何使用 Java 对 List 中每个对象元素按时间顺序进行排序 Java 实现 import java.text.SimpleDateFormat; import java.util.ArrayL ...
- The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored E.Sequence in the Pocket(思维题)
传送门 题意: 给出一个序列,你可以将任意一个数移到最前面: 求最少需要移动多少次,可以是此序列变成非递减序列: 思路: 定义 (ai,aj) 为逆序对 ( i < j , ai > aj ...
- Sequence(组合数学,集合不同元素的个数)
Sequence [组合数学] 时间限制: 3 Sec 内存限制: 128 MB 提交: 138 解决: 52 [提交][状态][讨论版] 题目描述 在某个夜黑月高的晚上,!!!,原谅我编不下去了 ...
- ZOJ 4110 Strings in the Pocket (马拉车+回文串)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110 题目: BaoBao has just found two s ...
- ZOJ 3955:Saddle Point(思维)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3955 题意:给出一个n*m的矩阵,定义矩阵中的特殊点Aij当且仅当Aij是 ...
随机推荐
- 使用@Scheduled注解编写spring定时任务
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframewor ...
- 修改linux的hostname (修改linux系统的IP和hostname)
# vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=yourname //在这修改hostnameNISDOMAIN=eng-cn.platform.c ...
- VS2013 自动添加头部注释 -C#开发
在团队开发中,头部注释是必不可少的.但在开发每次新建一个类都要复制一个注释模块也很不爽,所以得想个办法让开发工具自动生成我们所需要的模板.....操作方法如下: 方法/步骤 1 找你的vs安装目录, ...
- java的Access restriction错误
问 :import sun.management.ManagementFactory,我在rt包下已经找到sun.management.ManagementFactory,但就是有错,请问怎么回事. ...
- 九度OJ 1053:互换最大最小数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6613 解决:2676 题目描述: 输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后输出. 输入: 测试数据有多组 ...
- MethodDispatcher—Cherrypy对REST的支持
前言 CherryPy是Python的一个Web框架,通过MethodDispatcher内建了对REST的支持,而且使用非常方便. 示例 首先,我们需要有一个符合REST风格的资源(Resource ...
- 我的Android进阶之旅------>Ubuntu下不能识别Android设备的解决方法
Bus 001 Device 006: ID 1b20:0c81 MStar Semiconductor, Inc. 今天不知道Ubuntu发了什么疯,昨天还用的好好的,今天就突然不能识别我 ...
- 一起来学linux:日志文件
在管理系统当中,经常会遇到各种各样的错误和异常.要找到这些错误和异常,就需要各种日志来帮助定位问题了.linux的日志都是存放在/var/log这个文件夹下面,常见的日志文件有如下几种;/var/lo ...
- ThreadLocalMap里Entry声明为WeakReference
Java里,每个线程都有自己的ThreadLocalMap,里边存着自己私有的对象.Map的Entry里,key为ThreadLocal对象,value即为私有对象T.在spring MVC中,常用T ...
- 利用framebuffer,命令行显示图片
上代码 import fcntl import struct import mmap import contextlib import os import time import numpy as n ...