题解:CF2006D Iris and Adjacent Products

· · 题解

前言

有个小丑找了半天 TLE 的原因结果竟然是初始化的时候用了二分。

思路

首先我们观察一下性质。

这些是最容易观察出来的东西,然后我们考虑把数分类一下,我们发现对于 \leq \sqrt{k} 的任意两个数一定可以放在一起,那么我们就又有了一个性质,就是我们修改一定不会修改到 \leq \sqrt{k} 的数。然后我们考虑 \leq \sqrt{k} 的每一个数能得到一个能匹配的最大的数 \lfloor\frac{k}{i}\rfloor 然后我们发现对于一个合法序列一定要有 \forall i\in[1,\sqrt{k}]\sum_{j=1}^{i} cnt_{j}\geq \sum_{j=\frac{k}{i}}^{k} cnt_j 然后我们发现对于大于 \sqrt{k} 的每一个数都可以找到一个最接近它且大于等于它的 \frac{k}{i} 然后把 cnt 加到这个数上面即可,这样就能使得 cnt 有值的只有 \sqrt{k} 个,然后我们考虑最少修改的数怎么算,我们答案为 \frac{\max_{i=1}^{\sqrt{k}}(\sum_{j=\frac{k}{i}}^{k}cnt_j-\sum_{j=1}^{i}cnt_j)}{2} 然后只需要维护出来这一段区间的 cnt 然后就可以 \sqrt{k} 算答案了,如何得到这个 cnt 的话也很简单,直接用莫队跑就行了。

注意这个题没有保证 \sum k 所以不能每次 1\sim k 的清空。

还有一些边界情况就留给读者思考了,代码里面也有。

代码

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define ll long long
#define fire signed
#define il inline
template<class T> il void print(T x) {
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
template<class T> il void in(T &x) {
    x = 0; char ch = getchar();
    int f = 1;
    while (ch < '0' || ch > '9') {if(ch=='-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
    x *= f;
}
int T=1;
int n,q,k;
const int N=1e5+10,K=1e6+10;
int arr[N];
int ans[N],len;
struct node{
    int l,r,id;
    friend bool operator<(const node&a,const node&b) {
        int d=a.l/len;
        return (a.l/len==b.l/len)?(d&1?a.r<b.r:a.r>b.r):a.l<b.l;
    }
}s[N];
int cnt[N],tot,a[N];
int dui[N],mp[K];
int ct,B;
void add(int x) {
    cnt[mp[x]]++;
    if(x>B) ct++;
}
void del(int x) {
    cnt[mp[x]]--;
    if(x>B) ct--;
}
int ss[N],s1[N];
void solve() {
    tot=0;
    in(n),in(q),in(k);
    len=sqrt(n);
    rep(i,1,n) in(a[i]);
    rep(i,1,q) in(s[i].l),in(s[i].r),s[i].id=i;
    sort(s+1,s+1+q);
    B=sqrt(k);
    rep(i,1,B*2) ss[i]=s1[i]=false;
    rep(j,1,B) arr[++tot]=j,arr[++tot]=k/j;
    sort(arr+1,arr+1+tot);
    int mm=unique(arr+1,arr+1+tot)-arr-1;
    rep(i,1,n) mp[a[i]]=lower_bound(arr+1,arr+1+mm,a[i])-arr;
    int l=mm;
    rep(i,1,mm) {
        while(l>=1&&arr[l]>=k/arr[i]) l--;
        dui[i]=l+1;
    }
    int L=1,R=0;
    ct=0;
    rep(i,1,q) {
        while(L>s[i].l) add(a[--L]);
        while(L<s[i].l) del(a[L++]);
        while(R<s[i].r) add(a[++R]);
        while(R>s[i].r) del(a[R--]);
        s1[mm+1]=ss[0]=false;
        rep(j,1,mm) ss[j]=ss[j-1]+cnt[j];//求出前后缀和
        rep1(j,mm,1) s1[j]=s1[j+1]+cnt[j];
        int mx=false;
        rep(j,1,mm) {
            if(arr[j]<=B) mx=max(s1[dui[j]]-ss[j]-(s1[dui[j]]+ss[j]>=R-L+1),mx);//边界情况
            else break;
        }
        ans[s[i].id]=(mx+1)/2;
    }
    rep(i,1,n) cnt[mp[a[i]]]=false;
    rep(i,1,q) printf("%lld ",ans[i]);
    puts("");
}
fire main() {
    in(T);
    while(T--) {
        solve();
    }
    return false;
}