题目大意:
给出N个植物,每个植物都属于一个品种,共计m个品种,分落在不同的位子上(在一个数轴上,而且数轴是无限长度的),保证读入的位子是按照升序读入的。
现在我们可以进行一个操作:取任意一个位子上的植物,移动到任意一个没有植物的位子上去。
问我们最少进行多少次操作,能够使得从左到右,是按照品种升序排列的(1~m),而且每种植物都相邻;
思路:
①仔细读题之后发现,位子是没有用的,那么我们的任务就是挪动最少次数,使得序列品种升序。
②再仔细思考一下,我们挪动到的位子是不限的,那么我们其实问题就是确定最多多少个植物不挪动相对位子,而挪动其他植物即可,
那么问题就是求一个LIS,答案就是n-Lis
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[150000];
int have[150000];
int ned[150000];
int dp[150000];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(have,0,sizeof(have));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
double pos;
scanf("%d%lf",&a[i],&pos);
}
int maxn=0;
for(int i=1;i<=n;i++)
{
dp[i]=1;
for(int j=i-1;j>=1;j--)
{
if(a[i]>=a[j])
{
dp[i]=max(dp[i],dp[j]+1);
}
}
maxn=max(maxn,dp[i]);
}
printf("%d\n",n-maxn);
}
}