Codeforces 1240B. Sequence Sorting
分析题目发现如果把某个数 $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的更多相关文章
- D. Sequence Sorting dp
D. Sequence Sorting 题目大意:给你一个序列,有一种操作就是对所有相同的数可以挪到最前面,也可以挪到最后面,问最少操作次数. 首先,对于很多个相同的数,可以缩成两个位置,一个是就是这 ...
- 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 ...
- codeforces 876 D. Sorting the Coins
http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...
- 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 ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #335 Sorting Railway Cars 动态规划
题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...
- codeforces hungry sequence 水题
题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...
- Codeforces 13C Sequence
http://codeforces.com/contest/13/problem/C 题目大意 给定一个含有N个数的序列,要求你对一些数减掉或者加上某个值,使得序列变为非递减的,问你加减的值的总和最少 ...
- Codeforces 327B-Hungry Sequence(素数筛)
B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- 如何知道,当前redis实例是处于阻塞状态?
随便get一个key,然后卡着不动就行,简单粗暴.优雅一点是看latency的延迟,blocked_clients的数量,rejected_connections的数量等 或者 方法一:登录 Redi ...
- JAVA之自动内存管理机制
一.内存分配 1.JVM体系结构 2.运行时数据区域 3.内存分配二.内存回收 1.垃圾收集算法 2.垃圾收集器三.相关参考一.内存分配JVM体系结构 在了解自动内存管理的内存分配之前,我们先看下JV ...
- JS高级_数据类型
1.分类: a.基本(值)类型: * String :任意字符串 * Number :任意数字 * boolean : true/false * undefined :undefined * null ...
- MySQL 中<=>用法(长知识)
https://www.runoob.com/mysql/mysql-operator.html MySQL 运算符 本章节我们主要介绍 MySQL 的运算符及运算符的优先级. MySQL 主要有以下 ...
- Go语言学习之介绍与环境搭建
Go语言第一课 一.Go语言介绍 1.什么是Go语言? Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pik ...
- 在业务控制方法中写入Request,Response等传统web参数
可以在业务控制方法中书写传统web参数,这种方式我们不提倡,耦合了 @Controller @RequestMapping(value="/user") public class ...
- Hibernate3核心API-SessionFactory接口
Configuration对象根据当前的配置信息生成 SessionFactory 对象.SessionFactory 对象一旦构造完毕,即被赋予特定的配置信息(SessionFactory 对象中保 ...
- selenium webdriver常用函数
from selenium import webdriver driver = webdriver.Ie(executable_path = "e:\\IEDriverServer" ...
- Selenium 2自动化测试实战34(编写Web测试用例)
编写Web测试用例 1.介绍了unittest单元测试框架,其主要是来运行Web自动化测试脚本.简单的规划一下测试目录:web_demo1/------test_case/------------te ...
- jQuery上传文件显示进度条
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script sr ...