CF605A Sorting Railway Cars(递推)

本文介绍了一个列车排序问题,通过最少的操作次数将列车从任意排列变为升序排列。文章提供了详细的解析思路,并附带实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?

输入输出格式

输入格式:

The first line of the input contains integer n ( 1<=n<=100000 ) — the number of cars in the train.

The second line contains n integers pi  ( 1<=pi<=n  pi≠pj  if i≠j  ) — the sequence of the numbers of the cars in the train.

输出格式:

Print a single integer — the minimum number of actions needed to sort the railway cars.

题意翻译

题意

给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数

如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 5
操作2:将1 2 3和 4换一下变成 1 2 3 4 5 在此后即完成要求。所以最小操作次数为2。

输入:

第一行 为长度 nnn ; 第二行为原来的顺序两个数之间有空格。

输出:

最小操作次数

范围

1<=n<=100000

1=<p[i]<=n

 p[i]!=p[j] 当 i!=j

思路:

代码极短,思路可不简单

每次往队首或往队末放一辆车,且要求最后车都按顺序排列

那么这个问题首先就要考虑无效操作

如果你的当前这辆车编号比当前位置小,

那你显然应该往前放

如果你往后放那你将来要不把前面的往后放

要么再把这个放回去

明显不会更优

所以这个问题最后转化成了在这个序列里最多有哪些车可以不用改变位置

哪些车不用变位置呢?

因为最后的排列是连续且递增的

那么明显,能剩下来的,就是以前最长的已经排列好的子序列

也就是最长连续上升子序列

有人会说:这下就简单了,我会导弹拦截!

。。。有这么麻烦吗?

我们知道这个要求的是一个连续的序列

所以就简单了

比如说7这个数

到他为止的最长连续上升序列的长度f[7]一定是f[6]+1

所以就好办喽

代码:

#include<iostream>
#include<cstdio>
#define rii register int i
using namespace std;
int n,dp[100005],ans;
int main()
{
    int ltt;
    scanf("%d",&n);
    for(rii=1;i<=n;i++)
    {
        scanf("%d",&ltt);
        dp[ltt]=dp[ltt-1]+1;
        ans=max(ans,dp[ltt]);
    }
    cout<<n-ans;
}

 

转载于:https://www.cnblogs.com/ztz11/p/9323963.html

+ buildah bud --tls-verify=false --format=docker -t 11.11.157.164:31104/a6-uat/5715opss-front:v648_8195af64ad18fa4c4e5c3565358eac5fb13e461e -f Dockerfile . STEP 1: FROM 11.11.174.85:28086/cnpcrd/node:14.19.1-pnpm-nodesass AS builder Getting image source signatures Copying blob sha256:c398d8bd2d496fc1ba9d4658870242ce78aefe3041899476238df6cd427638f3 Copying blob sha256:7d66b83ec869a899bc8364af9c9eb0f1a5ba6907f699ef52f3182e19e2598924 Copying blob sha256:22360a9558f73f04bb5e4dbe6dbe1584cb913040ae66388a8db66fc2ed131002 Copying blob sha256:d88439e7b50a5f3923f67f432b6863c1e11adf4e45bf9740515d2cc01fd8e155 Copying blob sha256:f260dee23bc81622ef145aff36ad9816cca1c98bfa9361ad1bdf03c8975b104e Copying blob sha256:5f327ea23de9da0941be6aa9b1b0106716beba2b2221ccc2cf867db344fdc38b Copying blob sha256:c510cb256549cba034277b7ba3d45f821098be40e2d1ab716ebcbb2c82ad58e8 Copying blob sha256:39a6cf4e71acef62f9dc6133d8e2798afd190aa5d98df68c2c6a0853348c2a0b Copying blob sha256:5b88e0b6e02ac27e25195d6ef326172311c848a055cbe847de5e20547c526e55 Copying blob sha256:50868247d2ce4b9e47ed00d5c40c55746315be92ed203131e2139644a0cf22eb Copying blob sha256:e5e5c5e3f6d1226900e6c2d01182178f233df381354c7b91a233220d5d20d76a Copying blob sha256:db8034a611f27836dfac1f420b062134399233963dce16661cff75b190d7fa03 Copying blob sha256:88bc12f9a5d94abd68dd9f201555592838d5de85ab0b5cc8248199aa3f484a29 Copying blob sha256:80fbe3e8702a17df69ca8074656be9f7bd7fed87fd2d31dc7ec875b7249ec454 Copying blob sha256:8c1f72c02d3e7d7e3db33be2b133937124963fd2ac77215bd703129b4bae4c95 Copying blob sha256:009593723405a46968929d8221545f13495e3ff038367157a3597c2b8614d5ca Copying config sha256:2cb9f3e953292fdbce9015df8721a9f2336b49313ca036d96b52898a5d7af190 Writing manifest to image destination Storing signatures STEP 2: ARG profile STEP 3: ENV profile=${profile:-build} STEP 4: WORKDIR /usr/src/app STEP 5: COPY . ./ STEP 6: FROM 11.11.174.85:28086/cnpcrd/nginx-curl:v1.18.0 Getting image source signatures Copying blob sha256:d4e560a3091589fb95278a386add48626fbbe5bea3220576d6c4a838b4c65b24 Copying blob sha256:81bf9dd9cea603de3f08a37fb6f9156372709d0036479e6950b08b8b08dcf225 Copying blob sha256:47927ca6802f832d4000bfcde5939632a8ff52d6e4149187f198834556ee16e9 Copying blob sha256:4ede7ada96f6d9d2bc1f6ef734c74b5c0bc60a2f64e8c8d368055152d6dfe689 Copying blob sha256:f11c1adaa26e078479ccdd45312ea3b88476441b91be0ec898a7e07bfd05badc Copying blob sha256:9ac6875eca605a2e2f0a434d6a2f461356f32dae78fb3d4b9be23c2d02d46ac3 Copying blob sha256:e63758897ccc963cadd6f56a001bfcf5dc41dacce6a4e42ca1b3870f1cb91d14 Copying blob sha256:434abd9d8a2b02240f4daaeca44eb14e422ff394d4e1628d6d3208faece25d7c Copying blob sha256:bf49aa545a9f82a3131959a5232f51eee2b047fecaf6ff5a90410692be25b6e1 Copying blob sha256:d93ade5d972d5ede15f5d0fbc912f6f358b805c78c6ffd996a00e3e006aadd81 Copying blob sha256:c7bc6a87af944df6f159be206ce20093a771768e10f648de02b5aaae55bef62b Copying config sha256:75919dd59525e8fe57a70a5499ea69eee8c1eddaa61bac53e3c7212e5a232b82 Writing manifest to image destination Storing signatures STEP 7: COPY --from=builder /usr/src/app/dist /usr/share/nginx/html error building at STEP "COPY --from=builder /usr/src/app/dist /usr/share/nginx/html": no files found matching "/var/lib/containers/storage/overlay/bc9580993deabf5cc10eafbd76345ba0501fadfa1f733cf2fe0be5f44b80696d/merged/usr/src/app/dist": no such file or directory
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值