题解:

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;
}  

 

posted on 2018-04-27 18:23  宣毅鸣  阅读(259)  评论(0编辑  收藏  举报