B.温室効果(思考+ LIS / LCS)

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


アイデア:どちらもn ^ 2のデータ範囲で実行できます。

方法1:すでに対応する論理位置にある文字列の数を見てみましょう。貪欲の場合、本来あるべき位置に最も多くなりたいのです。答えは、最も多いものから合計を引くことです。最も多いのは、LISを見つけるプロセスです。

方法2:並べ替えによって回答シーケンスを作成し、2つのシーケンスの中で最も長い共通点が変更せずに最も同一であることを見つけ、合計を差し引くだけです。

したがって、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;
}

 

おすすめ

転載: blog.csdn.net/zstuyyyyccccbbbb/article/details/114898621