题解:P11847 [USACO25FEB] True or False Test P

· · 题解

题目传送门

思路

显然 Bessie 选完题目之后 Elsie 会选 a_i+b_i 最大的 k 道题,不妨先将所有题目按 a_i+b_i 降序排序,那么假设 Bessie 选的集合为 S,那么最终得分就是 S 编号最小的 k 道题的 b 之和的相反数加上剩下题目的 a 之和。

显然存在一个分界点 i,使得 Bessie 选择了 1 到 i 中的 k 道题和 i+1 到 n 中的一些题。

根据贪心,Bessie 一定会选择 1 到 i 之中 -b 最大的 k 道题和 i+1 到 n 的所有题。我们定义此时 Bessie 的得分为 w_{k,i}。

关键的观察:w_{k,i} 满足四边形不等式。 ::::info[证明] 欲证:\forall k_1<k_2\le i_1<i_2,w_{k_1,i_1}+w_{k_2,i_2}\ge w_{k_1,i_2}+w_{k_2,i_1}。

一个 w_{k,i} 由“a 的和”减去“b 的和”得到,不难发现不等式两边的“a 的和”是一样的。两边同时减去这个值后,只需证明 \forall k_1<k_2\le i_1<i_2,f_{k_1,i_1}+f_{k_2,i_2}\ge f_{k_1,i_2}+f_{k_2,i_1},其中 f_{k,i} 表示 1 到 i 中 -b 最大的前 k 个之和。

感性理解,要想和更大,肯定要把机会留给选择更大的那位(1 到 i_2),所以得证。

然后 w_{k,i} 显然可以主席树 O(\log n) 算,做完了。

AC code

#include<bits/stdc++.h>
using namespace std;
namespace cs{
    #define LL long long
    #define fir first
    #define sec second
    typedef pair<int,int> PII;
    const int N=2e5;
    const LL INF=1e18;
    int n,Q,dtz[N+5],dlen;
    LL ss[N+5];
    struct idk{
        int a,b;
        inline bool operator <(const idk& A)const{
            return a+b>A.a+A.b;
        }
    }p[N+5];
    int sgcnt,root[N+5];
    struct Segment{
        int ls,rs;
        int num;
        LL sum;
    }Tree[20*N+5];
    void Modify(int& I,int ori,int l,int r,int x){
        I=++sgcnt;
        int id=I,mid;
        while(1){
            Tree[id]=Tree[ori];
            Tree[id].num++;
            Tree[id].sum+=dtz[x];
            if(l==r) break;
            mid=l+r>>1;
            if(mid>=x){
                Tree[id].ls=++sgcnt;
                id=Tree[id].ls;
                ori=Tree[ori].ls;
                r=mid;
            }
            else{
                Tree[id].rs=++sgcnt;
                id=Tree[id].rs;
                ori=Tree[ori].rs;
                l=mid+1;
            }
        }
    }
    LL Query(int id,int l,int r,int x){
        LL rtn=0;
        int mid;
        while(l!=r){
            mid=l+r>>1;
            if(Tree[Tree[id].ls].num>=x){
                id=Tree[id].ls;
                r=mid;
            }
            else{
                rtn+=Tree[Tree[id].ls].sum;
                x-=Tree[Tree[id].ls].num;
                id=Tree[id].rs;
                l=mid+1;
            }
        }
        rtn+=x*1ll*dtz[l];
        return rtn;
    }
    inline LL w(int k,int i){
        if(i<k) return -INF;
        return ss[i+1]-Query(root[i],1,dlen,k);
    }
    int g[N+5];
    LL gf[N+5];
    void Solve(int l,int r,int L,int R){
        if(l>r) return;
        if(L==R){
            for(int i=l;i<=r;i++){
                g[i]=L;
                gf[i]=w(i,L);
            }
            return;
        }
        int mid=l+r>>1;
        LL temp;
        g[mid]=L;
        gf[mid]=w(mid,L);
        for(int i=L+1;i<=R;i++){
            temp=w(mid,i);
            if(temp>=gf[mid]){
                gf[mid]=temp;
                g[mid]=i;
            }
        }
        Solve(l,mid-1,L,g[mid]);
        Solve(mid+1,r,g[mid],R);
    }
    int main(){
        ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
        cin>>n>>Q;
        for(int i=1;i<=n;i++){
            cin>>p[i].a>>p[i].b;
            dtz[i]=p[i].b;
        }
        sort(p+1,p+n+1);
        sort(dtz+1,dtz+n+1);
        dlen=unique(dtz+1,dtz+n+1)-dtz-1;
        for(int i=1;i<=n;i++){
            Modify(root[i],root[i-1],1,dlen,lower_bound(dtz+1,dtz+dlen+1,p[i].b)-dtz);
        }
        for(int i=n;i>=1;i--){
            ss[i]=ss[i+1]+p[i].a;
        }
        Solve(0,n,0,n);
        int x;
        while(Q--){
            cin>>x;
            cout<<gf[x]<<"\n";
        }
        return 0;
    }
}
int main(){
    cs::main();
    return 0;
}