zoukankan      html  css  js  c++  java
  • Codeforces 605A. Sorting Railway Cars

    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.

    开一个pos数组记录每个数的位置,求该数组里的最长连续递增子序列长度即可
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    int f[100005];
    int a[100005],pos[100005] = {0},n;
    int main(){
      // freopen("test.in","r",stdin);
      cin >> n;
      for (int i=1;i<=n;i++){
        cin >> a[i];
        pos[a[i]] = i;
      }
      int res = 0,now = 0;
      // for (int i=1;i<=n;i++){
      //   cout << pos[i] << " ";
      // }cout << endl;
      for (int i=1;i<=n;i++){
        if (pos[i] > pos[i-1]){
          now ++;
          // cout << i << i-1 << now << res << endl;
        }
        else {
          res = max(now,res);
          now = 1;
        }
      }
      res = max(now,res);
      cout << n - res;
    }
    View Code
  • 相关阅读:
    基于keepalived双主模型的高可用LVS
    ViewFlipper实现ViewPager的页面切换效果
    C++,Python,Go对照学习-01
    matlab figure 调整大小、字体、线宽
    学术研究 —— 常用结论、说法
    学术研究 —— 常用结论、说法
    OpenGL(十六) 鼠标、键盘交互响应事件
    N+1:创新点的设计
    N+1:创新点的设计
    数学中的物理、几何概念与含义
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7421045.html
Copyright © 2011-2022 走看看