【Codeforces Round 335 (Div 2) C】【贪心 脑洞 有趣排序】Sorting Railway Cars 全排列排序可以提前后提后的最小操作数

84 篇文章 5 订阅
48 篇文章 0 订阅
C. Sorting Railway Cars
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n,x;
int e[N];
int main()
{
	while(~scanf("%d",&n))
	{
		MS(e,0);
		int ans=0;
		for(int i=1;i<=n;++i)
		{
			scanf("%d",&x);
			e[x]=e[x-1]+1;
			gmax(ans,e[x]);
		}
		printf("%d\n",n-ans);
	}
	return 0;
}
/*
【trick&&吐槽】
TwT 一上来就知道题LIS要跪。
然而还没想到,LIS竟然还能过初测。
太高估初测强度和大家的严谨性了,没有提早lock 去hack 好难过。
被一个红名汪抢了5分钟的800分hack分,好难过啊呜呜呜!

下次要当机立断!怎么可能这么快就过这么多人2333

【题意】
给你一个长度为n(1e5)的全排列。
我们可以对任意数字做任意次数的操作。
每次操作可以把一个数字提到最前或者提到最后。

【类型】
脑洞 贪心

【分析】
这题LIS是错误的。
比如4 2 1 3 5,LIS是3,然而我们却至少要操作3个数。而不是5-3==2个数

我们发现,每个数,如果操作,最多只会操作一次。
于是,我们希望不操作的数尽可能多。
而不操作的数,除了要求有LIS的关系外,因为我们是没办法在其中做数的插入的。
所以,这个LIS要求数字之间必须保证数值存在严格的连续性。

于是,我们从前向后扫描所有数,e[x]表示一个数向前延展的数值连续的子序列的长度
那么显然e[x]=e[x-1]+1;
也就是LIS O(n^2)的前驱固定的弱化版。
于是输出答案就可以AC啦

【时间复杂度&&优化】
O(n)

【数据】
input
5
4 2 1 3 5
output
3

*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值