https://vjudge.net/problem/ZOJ-4104/origin

https://vjudge.net/contest/395635#problem/E

DreamGrid has just found an integer sequence a_1, a_2, \dots, a_na1​,a2​,…,an​ 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 TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1 \le n \le 10^51≤n≤105), indicating the length of the sequence.

The second line contains nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ (1 \le a_i \le 10^91≤ai​≤109), indicating the given sequence.

It's guaranteed that the sum of nn of all test cases will not exceed 10^6106.

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.

附未通过代码方便日后补题:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl; #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db; const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7; ll a[100005] = {0};
int main()
{
int t = 0;
cin >> t;
while(t--)
{
int n = 0;
cin >> n;
int counter = 0;
int flag = 0;
for(int i = 0;i<n;i++)
{
cin >> a[i];
}
if(a[1]>a[0] && a[1]>a[2])
{
flag = 1;
//cout <<1<< " !A\n";
}
for(int i = 2;i<n-2;i++)
{
if(a[i]>a[i-1] && a[i]>a[i+1])
{
if(flag < 2)
{
if(a[i] == a[i-2])
{
counter -= 1;
if(flag == 1)counter += 1;
a[i-1] = a[i];
flag = 2;
//cout <<i<< " !1\n";
continue;
}
if(a[i] == a[i+2])
{
if(flag == 1)counter += 1;
a[i+1] = a[i];
flag = 2;
//cout <<i<< " !2\n";
continue;
}
} if(flag < 1)
{
if(a[i-1]<a[i-2] && a[i-1]<a[i+1] && a[i+1]<a[i+2])
{
counter -= 1;
a[i] = a[i-1];
flag = 1;
//cout <<i<< " !3\n";
continue;
}
if(a[i+1]<a[i+2] && a[i+1]<a[i-1] && a[i-1]<a[i-2])
{
counter -= 1;
a[i] = a[i+1];
flag = 1;
//cout <<i<< " !4\n";
continue;
}
if(a[i-1] == a[i-2] || a[i-1] == a[i+1] || a[i+1] == a[i+2])
{
a[i] = a[i-1];
flag = 1;
//cout <<i<< " !5\n";
continue;
} }
counter += 1;
//cout <<i<< " !++\n";
}
}
if(a[n-2]>a[n-1] && a[n-2]>a[n-3] && n!=3)
{
counter += 1;
//cout <<n-2<< " !B\n";
}
cout << counter << endl;
} return 0;
}

  

Sequence in the Pocket ZOJ - 4104 (待解决的更多相关文章

  1. ZOJ - 4104 Sequence in the Pocket(思维+元素移至列首排序)

    Sequence in the Pocket Time Limit: 1 Second      Memory Limit: 65536 KB DreamGrid has just found an ...

  2. Sequence in the Pocket【思维+规律】

    Sequence in the Pocket 题目链接(点击) DreamGrid has just found an integer sequence  in his right pocket. A ...

  3. ORA-02287: sequence number not allowed here问题的解决

    当插入值需要从另外一张表中检索得到的时候,如下语法的sql语句已经不能完成该功能:insert into my_table(id, name) values ((select seq_my_table ...

  4. The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored E.Sequence in the Pocket(思维题)

    传送门 题意: 给出一个序列,你可以将任意一个数移到最前面: 求最少需要移动多少次,可以是此序列变成非递减序列: 思路: 定义 (ai,aj) 为逆序对 ( i < j , ai > aj ...

  5. ZOJ4104 Sequence in the Pocket(2019浙江省赛)

    思维~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int N; int main ( ...

  6. TypeError: sequence item 0: expected string, Tag found

    原始代码: soup = BeautifulSoup(result, 'html.parser') content_list = soup.find_all('p', attrs={"cla ...

  7. uvm的sequence

    1,每个sequence都有一个body任务.当一个sequence启动后,会自动执行sequence的body任务,所以在sequence的class中,一定要有一个名为body的task. 此外, ...

  8. 2019浙江ACM省赛——部分题目

    有一些题目过了我还没有重新写,先放一些我重新写好了的吧 签到题拿到了信心吧,9分钟写完两题,我们贼开心,我大哥说签到题有什么好开心的,如果不是我有一些地方卡了下,可能还是更快吧,还有就是测试案例多试了 ...

  9. 浙江省第十六届大学生ACM程序设计竞赛部分题解

    E .Sequence in the Pocket sol:将数组copy一份,然后sort一下,找寻后面最多多少个元素在原数组中保持有序,用总个数减去已经有序的就是我们需要移动的次数. 思维题 #i ...

  10. CocoaPods的使用及安装

    本文转自:http://www.jianshu.com/p/6e5c0f78200a 一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理.开发i ...

随机推荐

  1. 为什么阿里的dubbo注册中心要放弃zookeeper, 而用Nacos?

    首先,那么为什么说zookeeper不适合做服务注册中心呢? 从CAP角度来看 有个思考,从CAP角度考虑,服务注册中心是CP系统还是AP系统呢? 首先,服务注册中心是为了服务间调用服务的,那么绝对不 ...

  2. 8086汇编(16位汇编)学习笔记00.DEBUG命令使用解析及范例大全

    转载自:https://bpsend.net/thread-99-1-1.html 启动 Debug,它是可用于测试和调试 MS-DOS 可执行文件的程序. Debug [[drive:][path] ...

  3. Linux vim编辑器介绍

      vim是Linux中常用的文件编辑器,作用包括两个:维护文本文件内容,维护Linux系统中的各种配置信息.她是程序开发者爱不释手的一款程序开发工具.下面简简单单介绍一下vim. 安装vim编辑器 ...

  4. 如何在Mac系统上把U盘分成2个区?

    插入U盘后,使用命令行执行如下命令: diskutil partitionDisk /dev/disk8 GPT MS-DOS TESLAMUSIC 7% ExFAT TESLADRIVE 93% 第 ...

  5. Java如何实现多线程

    JAVA里面 ​ JVM(java虚拟机) 就是 一个进程 ​ 进程与进程之间是绝对互相独立 ​ 我们运行多个main方法,代表有多个JAVA进程 ​ 进程里面有线程; 一个进程里面,允许有多个线程叫 ...

  6. Grafana+Prometheus+Node_exporter监控Linux系统

    一.简单介绍 这里介绍使用prometheus+grafana+node_exporter实现linux系统的监控.下面针对这三个组件进行具体的说明 prometheus prometheus是一套开 ...

  7. 函数使用十五:BAPI_PO_RELEASE

    *&---------------------------------------------------------------------* *& Report ZBAPI_PO_ ...

  8. [书籍精读]《你不知道的JavaScript(下卷)》精读笔记分享

    写在前面 书籍介绍:JavaScript这门语言简单易用,很容易上手,但其语言机制复杂微妙,即使是经验丰富的JavaScript开发人员,如果没有认真学习的话也无法真正理解.本套书直面当前JavaSc ...

  9. 五一假期学习总结:从DevOps到SRE

    大家好,我是Edison. 五一假期,没出远门,带娃露营玩水玩沙骑平衡车,累的不亦乐乎.同时,也刷了一门极客时间的课程<SRE实战总结>,给我带来了一些新的认知,我将这些认知整理了以下,特 ...

  10. C# DataGridView 控制只能选择一行

    https://www.it1352.com/1112692.html multiselectchanged方法的值false 将DataGridView的 SelectionMode 更改为当用户点 ...