CF2041J Bottle Arrangement 题解

· · 题解

$\rm Difficulty:2700

题意

给出长度为 n 的两个序列 a,b,要求给 b 序列中的若干个数减一并重排,设重排后的序列为 b',要求 b' 满足:

求减一次数的最小值,或报告无解。

1\le n\le5\times10^5 1\le a_i,b_i\le10^9

做法

先将 b 序列从小到大排序,接下来的 b 序列指从小到大排序后的 b 序列。

考虑扣出序列最小值 mn 的所有位置 p_1,p_2,...,p_k。

我们可以枚举单峰序列的峰在哪个 [p_i,p_{i+1}] 中(或者是 [1,p_1] 或 [p_k,n]),设 len=p_{i+1}-p_i-1,则我们可以贪心的将 b 序列的前 n-len 个数放在 [1,p_i],[p_{i+1},n] 中,所需减 1 的次数就是 b 序列前 n-len 个数中 mn 的个数,不过如果 b_{n-len} 已经大于 mn 那么说明这种操作是不合法的,必须选择其他的 [p_i,p_{i+1}]。

于是我们只需解决在 [p_i+1,p_{i+1}-1] 中放入 b 序列的后 len 个数的子问题,做法与上面类似,只需不断递归分治即可。

分治时间复杂度为 O(n),但是由于要找 p 序列,需要用一些数据结构维护一下,所以时间复杂度为 O(n\log n)。

代码

(本人大码量体质,实际上代码写得很清晰)

#include<bits/stdc++.h>
#define cint const int
#define iint inline int
#define uint unsigned int
#define iuint inline unsigned int
#define cuint const unsigned int
#define ll long long
#define cll const long long
#define ill inline long long
#define ull unsigned long long
#define iull inline unsigned long long
#define cull const unsigned long long
#define sh short
#define csh const short
#define ish inline short
#define ush unsigned short
#define iush inline unsigned short
#define cush const unsigned short
using namespace std;
iint read()
{
    int x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=getchar();
    }
    return x;
}
cint inf=1e9;
int n;
int a[500001],b[500001];
namespace T{//segment tree
    struct node{
        int l,r,mn;
    }t[2000001];
    void Build(cint p,cint l,cint r)
    {
        t[p].l=l;
        t[p].r=r;
        if(l==r)
        {
            t[p].mn=a[l];
            return;
        }
        cint mid=l+r>>1;
        Build(p<<1,l,mid);
        Build(p<<1|1,mid+1,r);
        t[p].mn=min(t[p<<1].mn,t[p<<1|1].mn);
    }
    void build()
    {
        Build(1,1,n);
    }
    int Ask(cint p,cint l,cint r)
    {
        if(t[p].l>r||t[p].r<l)return inf;
        if(t[p].l>=l&&t[p].r<=r)return t[p].mn;
        return min(Ask(p<<1,l,r),Ask(p<<1|1,l,r));
    }
    int ask(cint l,cint r)
    {
        return Ask(1,l,r);
    }
    int Find(cint p,cint l,cint r,cint x)
    {
        if(t[p].l>r||t[p].r<l)return -1;
        if(t[p].l>=l&&t[p].r<=r&&t[p].mn!=x)return -1;
        if(t[p].l==t[p].r)return t[p].l;
        int L=Find(p<<1,l,r,x);
        if(L!=-1)return L;
        return Find(p<<1|1,l,r,x);
    }
    int find(cint l,cint r,cint x)
    {
        return Find(1,l,r,x);
    }
}
int find(cint l,cint r,cint x)
{
    cint mid=l+r+1>>1;
    if(l>=r)return mid;
    if(b[mid]<x)return find(mid,r,x);
    return find(l,mid-1,x);
}
int solve(cint l,cint r)
{
    if(l>r)return 0;
    int mn=T::ask(l,r);
    vector<int>p;
    int w=l;
    p.push_back(l-1);
    while(1)
    {
        w=T::find(w,r,mn);
        if(w==-1)break;
        p.push_back(w);
        ++w;
    }
    int lst=find(0,n,mn);
    p.push_back(r+1);
    int ans=inf;
    for(int i=0;i<p.size()-1;++i)
    {
        cint pl=p[i]+1,pr=p[i+1]-1;
        cint len=pr-pl+1,tp=n-len;
        if(b[tp]>mn)continue;
        if(tp<=lst)
        {
            ans=min(ans,solve(pl,pr));
        }
        else
        {
            ans=min(ans,solve(pl,pr)+tp-lst);
        }
    }
    return ans;
}
int main()
{
    n=read();
    for(int i=1;i<=n;++i)
    {
        a[i]=read();
    }
    for(int i=1;i<=n;++i)
    {
        b[i]=read();
    }
    T::build();
    sort(b+1,b+n+1);
    int ans=solve(1,n);
    printf("%d",(ans==inf?-1:ans));
    return 0;
}