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是 ...
随机推荐
- SQL Server里的 ISNULL 与 NULLIF(转)
SQL Server 中有两个参数,语法: ISNULL(check_expression, replacement_value) check_expression 与 replacement ...
- gridcontrol 之标题 GroupPanel设置 (标题设置,屏蔽右键)
GroupPanel设置 例如gridcontrol显示标题:“gridcontrol小例子” gridView1.GroupPanelText="gridcontrol小例子"; ...
- Azkaban_Oozie_action
http://azkaban.github.io/azkaban/docs/2.5/ There is no reason why MySQL was chosen except that it is ...
- Hadoop实战-Flume之自定义Sink(十九)
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...
- swift中反向循环
First of all, protocol extensions change how reverse is used: for i in (1...5).reverse() { print(i) ...
- leetcode 890. Possible Bipartition
Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ...
- stm32f429I discovery试用
从网上了解到stm32f429I discovery可以跑单片机程序也可以跑uclinux,而我恰好喜爱嵌入式开发相关内容, 性价比也较高,所以新入手了这款开发板.开发板官方网页:http://www ...
- 主成分分析(PCA)与SVD奇异值分解
主要参考:https://www.zhihu.com/question/38417101/answer/94338598 http://blog.jobbole.com/88208/ 先说下PCA ...
- php设计模式课程---9、桥接模式是什么
php设计模式课程---9.桥接模式是什么 一.总结 一句话总结: 一个类没干完,另外一个类接着给它干完 实质是类的拼接,也就是用类的组合代替了类的继承,因为类的组合可以有很多种方式,所以桥接就是类的 ...
- 分享知识-快乐自己:Oracle SQL语法汇总
--删除重复值-保留重复值最大的编号 delete from emp where rowid in( select rowid from emp where rowid not in( select ...