题意

给一个长度为\(n\)的数组,数组中的数互不相同,你可以有两种操作

  • 将某一个数放置在数组开头
  • 将某一个数放置在数组结尾

问最小操作多少次可以得到一个递增数列

分析

因为数组中的数很大,我们可以将其离散化然后操作,这样我们可以得到一个长度为\(n\)的排列,目的是得到一个从\(1\)到\(n\)的顺序排列

每个数最多操作一次,否则第一次可以不操作,那么我们就要找最多的不需要操作的数,如果不需要操作,则元素的位置不变,如果有这么一组不需要操作的数,我们可以发现,中间的数字是不能插进去的,所以这组数是相邻的数,那么问题就转换为找到数组内最长的相差为\(1\)的子序列,考虑用\(DP\),\(dp[i]\)表示以数字\(i\)为结尾的最长子序列长度,则转移方程为

\[dp[a[i]]=dp[a[i]-1]+1
\]

如果\(a[i]-1\)出现了,则这个数是\(a[i]-1\)的后缀,否则这个数是开头,即为\(1\)

#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
//#define int ll
#define ls st<<1
#define rs st<<1|1
#define pii pair<int,int>
#define rep(z, x, y) for(int z=x;z<=y;++z)
#define com bool operator<(const node &b)
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int maxn = (ll) 3e3 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int T = 1;
int a[maxn], b[maxn];
int dp[maxn]; void solve() {
int n;
cin >> n;
map<int, int> mp;
rep(i, 1, n)cin >> a[i], b[i] = a[i], mp[a[i]] = i, dp[i] = 0;
sort(b + 1, b + n + 1);
rep(i, 1, n)a[mp[b[i]]] = i;
int maxx = 1;
rep(i, 1, n) {
dp[a[i]] = dp[a[i] - 1] + 1;
maxx = max(maxx, dp[a[i]]);
}
cout << n - maxx << '\n';
} signed main() {
start;
cin >> T;
while (T--)
solve();
return 0;
}

CodeForces 1367F1 Flying Sort (Easy Version)的更多相关文章

  1. Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)

    题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...

  2. codeforces Equalizing by Division (easy version)

    output standard output The only difference between easy and hard versions is the number of elements ...

  3. Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)

    <题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...

  4. Codeforces 1296E1 - String Coloring (easy version)

    题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你只有两种颜色可以用来涂 问是否 ...

  5. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  6. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  7. Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)

    F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...

  8. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

  9. Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题

    B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...

  10. Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)

    D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...

随机推荐

  1. L2-004 这是二叉搜索树吗? (25 分)

    1.题目描述: 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的键值: 其左右子树都是二叉搜索树 ...

  2. Android Create New Module 提示Project needs to be converted to androidx.* dependencies

    最近在一个flutter项目中创建一个android plugin module提示androidx依赖问题 finish始终无法激活,最后参照flutter官方https://flutter.dev ...

  3. Python潮流周刊#5:并发一百万个任务要用多少内存?

    你好,我是猫哥.这里记录每周值得分享的 Python 及通用技术内容,部分为英文,已在小标题注明.(标题取自其中一则分享,不代表全部内容都是该主题,特此声明.) 博客原文:https://python ...

  4. AI 协助办公 |记一次用 GPT-4 写一个消息同步 App

    GPT-4 最近风头正劲,作为 NebulaGraph 的研发人员的我自然是跟进新技术步伐.恰好,现在有一个将 Slack channel 消息同步到其他 IM 的需求,看看 GPT-4 能不能帮我完 ...

  5. 采集douban

    # -*- coding: utf-8 -*-"""Created on Thu Oct 31 16:14:02 2019 @author: DELL"&quo ...

  6. RabbitMQ基本配置

    1.用户角色配置 自带的guest/guest 超级管理员五中不同角色配置: 普通管理者(management):仅可登陆管理控制台,无法看到节点信息,也无法对策略进行管理. 策略制定者(policy ...

  7. 实时阴影技术(Real-time Shadows)

    目录 Shadow Mapping 基本实现 Shadow Bias Peter Panning 问题 & 简单 Trick Slope Scale Based Depth Bias Casc ...

  8. 【Springboot】SpringBoot-Admin 服务监控+告警通知

    SpringBoot-Admin 服务监控 简单介绍 Spring Boot Actuator 是 Spring Boot 自带的一个功能模块, 提供了一组已经开箱即用的生产环境下常用的特性和服务,比 ...

  9. 即构SDK8月迭代:新增下行网络测速、切换房间、预览首帧回调等多项功能

    即构SDK8月上新,网络探测模块新增下行网络测速.媒体播放器新增缓存到文件结尾的回调.新增切换房间功能.新增预览首帧回调等.本月SDK迭代新上线的功能非常丰富,详细内容请看下文. LiveRoom 新 ...

  10. base64详解

    base64详解 前置知识 位与字节 二进制系统中,每个0或1就是一个位(bit,比特),也叫存储单元,位是数据存储的最小单位. 其中8bit就称为一个字节(Byte). 1B=8位 位运算 与运算: ...