CF269B Greenhouse Effect (dp LIS)

题目吓死人系列,这一串浮点位置看哭我,后来仔细读题,发现题目问的是,最少经过多少次移动能将序列排成非严格单调递增

为什么是这样的呢,因为他想要分m个区域,并且要求每个i种类都位于i区域,那么只能把所有的排成非严格递增才行

那这道题就简单了,其实就是求LIS,之后用总数减一下就行,这里我使用的是nlogn的栈模拟,不知道n^2能不能过

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#define ull unsigned long long
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=1e5+10;
int f[5010][5010];
int a[N];
vector<int> num;
int main(){
    int n;
    int m;
    int i;
    cin>>n>>m;
    for(i=1;i<=n;i++){
        double x;
        scanf("%d%lf",&a[i],&x);
    }
    num.clear();
    num.push_back(a[1]);
    for(i=2;i<=n;i++){
        if(a[i]>=num.back())
            num.push_back(a[i]);
        else{
            int pos=upper_bound(num.begin(),num.end(),a[i])-num.begin();
            num[pos]=a[i];
        }
    }
    int ans=n-(int)num.size();
    cout<<ans<<endl;
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/ctyakwf/p/12637480.html