Codeforce----Sorting Railway Cars(LIS/O(nlogn))

31 篇文章 0 订阅
10 篇文章 0 订阅

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?

Input

The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train.

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

Output

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

Sample test(s)
input
5
4 1 2 5 3
output
2
input
4
4 1 3 2
output
2
Note

In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.

题意:

给出n个汽车编号(1--n),乱序,将这n辆汽车移动按编号从小到大排序,每次可以将一辆汽车移除原来位置插入到尾部或首部。问最少需要移动多少次?

分析:

实际上就是求原来排列的最大上升子序列。只要保证这个最大上升子序列相对位置不变,移动剩下的汽车就是最少移动次数。

下面先贴出我的WA代码,我是想对每个a[i](第i辆汽车的编号)求出以它为起点的最大上升子序列,方法就是从它开始往后遍历,大于它时,个数加1,更新序列尾值,但是忽略了这种情况:

...4 8 5 6 7...

单就这一段而言,以4为开端的最大上升子序列理想的结果应是4 5 6 7,一共4个数,而我的方法实际上得到的是4 8,一共2个数。

#include <stdio.h>
#include <string.h>
#define maxn 100010
int a[maxn],n,b[maxn],t;
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while (~scanf("%d", &n))
    {
        for (int i = 0;i < n;i++)
            scanf("%d", &a[i]);
        for (int i = 0;i < n;i++)
        {
            t = a[i];
            b[i] = 1;
            for (int j = i+1;j < n;j++)
            {
                if (a[j] > t)//错误在这里
                {
                    b[i]++;
                    t = a[j];
                }
            }
        }
        int max = b[0];
        for (int i = 1;i < n;i++)
        {
            if (b[i] > max)
                max = b[i];
        }
        printf("%d\n", n - max);
    }
    return 0;
}

然后搜到了一种方法:

另开一个数组,记录a[i]所在位置i,即b[a[i]]=i;然后每次比较b[j]和b[j-1],如果b[j]>b[j-1]说明编号j可以插入当前上升子序列,否则的话,当前子序列结束,比较这个子序列的长度和已经计算过的子序列长度,保留较大的一个。

AC了。这种方法时间复杂度是O(n)。

#include <stdio.h>
#include <string.h>
#define maxn 100010
int a[maxn],n,b[maxn],len,ans;
int main()
{
    //freopen("in.txt", "r", stdin);
   //freopen("out.txt", "w", stdout);
    while (~scanf("%d", &n))
    {
        for (int i = 1;i <= n;i++)
        {
            scanf("%d", &a[i]);
            b[a[i]]=i;
        }
        len = 1;
        ans = 0;
        for (int i = 2;i <= n;i++)
        {
            if (b[i] > b[i - 1]) len ++ ;
            else
            {
                ans = ans > len ? ans : len;
                len = 1;
            }
        }
        ans = ans > len ? ans : len;
        printf("%d\n", n - ans);
    }
    return 0;
}

接着我又找到了一篇博客专门介绍了,最大上升子序列问题, 地址在这里

按照DP的写法写了一下,但是TLE了【因为时间复杂度是O(n^2)】,不过前八组测试数据没问题。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int  maxn=100010;
int a[maxn], dp[maxn], n;
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while (~scanf("%d", &n))
    {
        for (int i = 1;i <= n;i++)
            scanf("%d", &a[i]);
        for (int i = 1;i <= n;i++)
        {
            dp[i] = 1;
            for (int j = 1;j < i;j++)
                if (a[j] < a[i])
                    dp[i] = max(dp[i], dp[j] + 1);
        }
        sort(dp + 1, dp + n + 1);
        printf("%d\n", n-dp[n]);
    }
    return 0;
}

我把O(nlogn)的方法贴了上去,也是TLE了。看来只能用第一种方法了。囧 ~

#include <iostream>
#define SIZE 1001

using namespace std;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int i, n, top, temp;
    int stack[SIZE];
    while (cin >> n)
    {
        top = 0;
        /* 第一个元素可能为0 */
        stack[0] = -1;
        for (i = 0; i < n; i++)
        {
            cin >> temp;
            /* 比栈顶元素大数就入栈 */
            if (temp > stack[top]) stack[++top] = temp;
            else
            {
                int low = 1, high = top;
                int mid;
                /* 二分检索栈中比temp大的第一个数 */
                while (low <= high)
                {
                    mid = (low + high) / 2;
                    if (temp > stack[mid]) low = mid + 1;
                    else high = mid - 1;
                }
                /* 用temp替换 */
                stack[low] = temp;
            }
        }
        /* 最长序列数就是栈的大小 */
        int ans = n - top;
        cout << ans << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值