B. Greenhouse Effect (Thinking+LIS/LCS)

https://codeforces.com/problemset/problem/269/B


Idea: Both can be done under the data range of n^2.

Method 1: Let's see how many of the strings are already in their corresponding logical positions. For greed, we want to be the most in the position that should be. The answer is to subtract the total from the most. The most is the process of finding LIS.

Method 2: We can construct the answer sequence through sort, and then find the longest common among the two sequences, which is the most identical without changing, just subtract the total.

So n^2 can be both, but what if it is changed to 1e5? Then you can only use the LIS version of the greedy bisection

#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;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/114898621