传送门

分析题目发现如果把某个数 $x$ 往左移,那么之后所有小于 $x$ 的数也都要往左移

如果把 $x$ 往右移,那么之后所有大于 $x$ 的数也都要往右移

考虑我们首先一定有一个操作 $n$ 次的合法方案

但是发现其实有些数可以不用操作,只要把比它小的和比它大的搞成合法就行了

发现其实不用操作的数的排名一定是连续的一段,证明可以这样考虑

假设排名不是连续的一段,那么两段中间一定有一个数 $x$ 要操作,那么所有大于 $x$ 数或者小于 $x$ 的数一定要操作

那么矛盾,证明完成

那么现在只考虑排名连续的数最长不用操作的值的数量

显然只要这些数出现的最左边和最右边的区间互不相交那么一定可以不用操作

并且一旦相交就必须操作,所以直接从左到右枚举即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=3e5+;
int Q,n,a[N],L[N],R[N];
int main()
{
Q=read();
while(Q--)
{
n=read();
for(int i=;i<=n;i++) L[i]=N,R[i]=;
for(int i=;i<=n;i++)
a[i]=read(),L[a[i]]=min(L[a[i]],i),R[a[i]]=max(R[a[i]],i);
int cnt=,now=,las=,mx=;
for(int i=;i<=n;i++)
{
if(L[i]>n) continue;
cnt++;
now= (!now) ? : (L[i]>las ? now+ : );
las=R[i]; mx=max(mx,now);
}
printf("%d\n",cnt-mx);
}
return ;
}

Codeforces 1240B. Sequence Sorting的更多相关文章

  1. D. Sequence Sorting dp

    D. Sequence Sorting 题目大意:给你一个序列,有一种操作就是对所有相同的数可以挪到最前面,也可以挪到最后面,问最少操作次数. 首先,对于很多个相同的数,可以缩成两个位置,一个是就是这 ...

  2. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) D. Sequence Sorting

    链接: https://codeforces.com/contest/1241/problem/D 题意: You are given a sequence a1,a2,-,an, consistin ...

  3. codeforces 876 D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...

  4. Codeforces 606-C:Sorting Railway Cars(LIS)

    C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. Codeforces 830B - Cards Sorting 树状数组

    B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Codeforces Round #335 Sorting Railway Cars 动态规划

    题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...

  7. codeforces hungry sequence 水题

    题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...

  8. Codeforces 13C Sequence

    http://codeforces.com/contest/13/problem/C 题目大意 给定一个含有N个数的序列,要求你对一些数减掉或者加上某个值,使得序列变为非递减的,问你加减的值的总和最少 ...

  9. Codeforces 327B-Hungry Sequence(素数筛)

    B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. Centos镜像下载地址

    https://blog.csdn.net/weixin_42430824/article/details/81019039

  2. decimal模块 --数字的精度、保留小数位数、取整问题

    开始之前需要注意一点是:精度值为数字的总位数,如:1.23, 精度值为3: 0.123,精度值也为3 1.更改默认精度值后,直接进行计算即可保留对应精度值 from decimal import ge ...

  3. 将mysql数据库的数据导出做成excl表格通过邮件发送附件发给指定人

    mysql数据库的数据导出成excl表 方法一: mysql -uroot -p123456 -e “select *.* from 表 into outfile ‘文件路径.xls’into out ...

  4. openstack基于卷快照恢复卷

    openstack基于卷快照恢复卷 基于P版本,对卷基于快照进行恢复的源码分析 1.特性描述 在pike版本中,openstack官网增加了一个新特性,Cinder volume revert to ...

  5. substr 字符串截取

    <!DOCTYPE html> <html> <body> <?php echo substr("Hello world",0,10).& ...

  6. linux文件结构---转发

    linux各文件夹的作用---转载   linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配 ...

  7. 3、inform (通知、告诉、报告) - perform (表现)

  8. centos6 安装tensorflow

    1.升级python2.6.6 至 python2.7.12+ 升级时./configure --prefix=/usr/local/python27 --enable-unicode=ucs4 2. ...

  9. Spring-Kafka —— KafkaListener禁止自启动

    应用服务启动时,KafkaListener默认会自动启动进行消费,如果想不自动消费,可以设置AutoStartup属性值为false @Override @KafkaListener(id = Con ...

  10. 给php安装openssl扩展

    转自 http://blog.csdn.net/sinat_23678421/article/details/42217971