Codeforces 605A. Sorting railway Cars "tricks" _ Strings

Source: Internet
Author: User
A. Sorting railway Cars time limit/test 2 seconds memory limit per test 256 megabytes input standard input output Stan Dard output

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

The ' The ' input contains integer n (1≤n≤100)-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 TRA In. Output

Print a single integer-the minimum number of actions needed to sort the railway cars. Examples input

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

In the ' the ' of the ' the ' and ' then ' the 5-th car to the ' end of the teleport ' 4-th car. Ac-code:

#include <bits/stdc++.h>  
#include <iostream>
#include <cstdio>
#include < algorithm>
#include <cstring>
using namespace std;
int main () {
	int n,i,j,flag,a,dp[100005];
	cin>>n;
	flag=1;
	Memset (Dp,0,sizeof (DP));
	for (i=0;i<n;i++) {
		cin>>a;
		dp[a]=dp[a-1]+1;
	}
	Sort (dp+1,dp+n+1);
	cout<<n-dp[n]<<endl;
	return 0;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.