zoukankan      html  css  js  c++  java
  • codeforces605A

    Sorting Railway Cars

     CodeForces - 605A 

        一辆列车有N节车厢,编号为1...N(每节车厢编号都不同),并且他们的排列是混乱的。李老湿想要把这N节车厢重新排列为升序排列,每次调整他可以挑出一节车厢放在车头或者车尾。可是李老湿太懒了,所以他想知道最少需要调整多少次就可以完成重新排列!为了解决这个问题,李老湿决定求助学弟们。

    Input

    第一行:一个整数N( 1<=N<=100000) 表示列车的车厢数

    第二行:N个整数pi( 1<=pi<=n,   pi ≠ pj if i ≠ j)表示给定的车厢排列

    Output

    输出一个整数:最少的操作。

    Examples

    Input
    5
    4 1 2 5 3
    Output
    2
    Input
    4
    4 1 3 2
    Output
    2

    sol:好无趣的结论题啊,一开始想错了,以为是找到最长上升子序列,然后被这组数据hack了
    input
    7
    1 3 5 7 2 4 6
    output
    5

    然后发现是要找到最长的连续上升子序列。。。

    
    
    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0');    return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=100005;
    int n,a[N],dp[N];
    int main()
    {
        int i,ans=0;
        R(n);
        for(i=1;i<=n;i++) R(a[i]);
        for(i=1;i<=n;i++)
        {
            dp[a[i]]=dp[a[i]-1]+1;
            ans=max(ans,dp[a[i]]);
        }
        Wl(n-ans);
        return 0;
    }
    /*
    Input
    5
    4 1 2 5 3
    Output
    2
    
    Input
    4
    4 1 3 2
    Output
    2
    
    input
    7
    1 3 5 7 2 4 6
    output
    5
    */
    View Code
    
    
    
     
  • 相关阅读:
    AcWing 276. I-区域
    学习笔记:可持久化线段树(主席树):静态 + 动态
    NOIP2016提高组 天天爱跑步
    AcWing 195. 骑士精神
    标准文档流
    css 盒模型
    css 继承性和层叠性
    css 选择器
    css 引入方式
    html body中的标签2
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10731337.html
Copyright © 2011-2022 走看看