#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
*/
【Codeforces Round 335 (Div 2) C】【贪心 脑洞 有趣排序】Sorting Railway Cars 全排列排序可以提前后提后的最小操作数
最新推荐文章于 2017-10-09 21:55:57 发布