https://codeforces.com/problemset/problem/269/B
思路:在n^2的数据范围下两个都可以做。
法一:我们看这一串中有多少个已经是在其对应的逻辑位置上的,贪心来说我们要让在应该在的位置上最多,拿总数减这个最多就是答案。最多就是找LIS的过程。
法二:我们可以通过sort构造出答案序列,然后在两个序列中找出最长公共的就是最多相同的不用换的,总数减去就好。
所以n^2两个都可以,但是如果改成1e5呢?那就只能上贪心二分的LIS版本
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e3+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],dp[maxn];///LIS版本
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n,m;cin>>n>>m;
for(LL i=1;i<=n;i++){
cin>>a[i];double x;cin>>x;
}
for(LL i=1;i<=n;i++) dp[i]=1;
LL ans=n;
for(LL i=1;i<=n;i++){
for(LL j=1;j<i;j++){
if(a[i]>=a[j]){
dp[i]=max(dp[j]+1,dp[i]);
}
}
}
LL res=1;
for(LL i=1;i<=n;i++){
res=max(res,dp[i]);
}
cout<<ans-res<<"\n";
return 0;
}
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e3+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
///最长公共子序列
LL a[maxn],b[maxn],dp[maxn][maxn];
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n,m;cin>>n>>m;
for(LL i=1;i<=n;i++){
cin>>a[i];double x;cin>>x;
b[i]=a[i];
}
sort(b+1,b+1+n);
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++){
if(a[i]==b[j]){
dp[i][j]=max(dp[i-1][j-1]+1,dp[i][j]);
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
LL ans=n;
cout<<ans-dp[n][n]<<"\n";
return 0;
}