• bzoj4700


    题解:

    cdq分治

    先考虑没有人被秒掉的情况

    代码:

    #include<bits/stdc++.h>
    #define y1 ____y1
    const int N=300005;  
    using namespace std;  
    typedef long long ll;  
    int bh[N],q[N],bhd[N],bha[N],A[N],d[N],P[N],T[N],n,atk;  
    ll ans,pr,K[N];  
    int comp1(const int x,const int y)
    {
        return A[x]*d[y]>A[y]*d[x];
    }  
    int fc(int x,int y,int z)
    {  
        ll x1=d[x],y1=K[x],x2=d[y],y2=K[y],x3=d[z],y3=K[z];  
        return (y1-y2)*(x2-x3)>=(y2-y3)*(x1-x2);  
    }  
    void cdq(int xl,int xr)
    {  
        if (xl==xr)return;   
        int mid=xl+xr>>1;  
        cdq(xl,mid);cdq(mid+1,xr);  
        int l=1,r=0;  
        for (int i=xl;i<=mid;i++)
         {  
            while (l<r&&fc(bhd[i],q[r],q[r-1]))r--;  
            q[++r]=bhd[i];  
         }  
        for (int i=mid+1;i<=xr;i++)
         {  
            while (l<r&&K[q[l]]-K[q[l+1]]<=(ll)A[bha[i]]*(d[q[l]]-d[q[l+1]]))l++;
            if (l<=r)ans=max(ans,K[bha[i]]+K[q[l]]-(ll)A[bha[i]]*d[q[l]]);  
         }  
        int i=xl,j=mid+1,ptr=xl;  
        for (;i<=mid&&j<=xr;)
         {  
            if (d[bhd[i]]<d[bhd[j]])bh[ptr++]=bhd[i++];  
            else bh[ptr++]=bhd[j++];  
         }  
        for (;i<=mid;)bh[ptr++]=bhd[i++];  
        for (;j<=xr;)bh[ptr++]=bhd[j++];  
        for (int i=xl;i<=xr;i++)bhd[i]=bh[i];  
        i=xl,j=mid+1,ptr=xl;  
        for (;i<=mid&&j<=xr;)
         {  
            if (d[bha[i]]<d[bha[j]])bh[ptr++]=bha[i++];  
            else bh[ptr++]=bha[j++];  
         }   
        for (;i<=mid;)bh[ptr++]=bha[i++];  
        for (;j<=xr;)bh[ptr++]=bha[j++];  
        for (int i=xl;i<=xr;i++)bha[i]=bh[i];  
    }  
    int main()
    {  
        scanf("%d%d",&n,&atk);  
        for (int i=1;i<=n;i++)bh[i]=i;  
        for (int i=1;i<=n;i++)
         {
            scanf("%d%d",&A[i],&d[i]);  
            d[i]=(d[i]-1)/atk+1;  
         }
        sort(bh+1,bh+n+1,comp1);  
        for (int i=1;i<=n;i++)
         {
            P[i]=P[i-1]+d[bh[i]];
            T[i]=T[i-1]+A[bh[i]];
            bhd[i]=bha[i]=bh[i]; 
         }
        for (int i=1;i<=n;i++)
         {  
            K[bh[i]]=(ll)A[bh[i]]*(P[i]-1)+(ll)(T[n]-T[i])*d[bh[i]];  
            pr+=(ll)A[bh[i]]*(P[i]-1);  
         }  
        cdq(1,n);  
        printf("%lld",pr-ans);  
        return 0;
    }  
  • 相关阅读:
    Eclipse 中怎样自动格式化代码?
    如何使用Jfreechart生成柱状图?
    ADT OOP
    5.2 Construction for Reuse
    Lab6实验的一些思考
    软件构造的八个多维视图
    软件构造笔记5.1 Metrics, Morphology and External Observations of Reusability
    事后诸葛亮之感谢
    (Model)针对编译错误的友好提示
    第二次结对作业
  • 原文地址:https://www.cnblogs.com/xuanyiming/p/8963865.html
Copyright © 2020-2023  润新知