【2018牛客多校round2】 G transform

题意

White Cloud placed n containers in sequence on a axes. The i-th container is located at x[i] and there are a[i] number of products in it.
White Rabbit wants to buy some products. The products which are required to be sold must be placed in the same container.
The cost of moving a product from container u to container v is 2*abs(x[u]-x[v]).
White Cloud wants to know the maximum number of products it can sell. The total cost can't exceed T.

思路

如果能够在T费用内将X个商品集中到同一集装箱,那么小于X的答案也是可行的,所以考虑二分答案
问题转化为,判断是否能在T费用内,将X个商品集中到同一集装箱
对于一个固定的X
从左到右枚举箱子,用五个变量维护当前的选择

  • now :当前选择的X个货物,需要搬运到的集装箱下标

  • lll :已选择的最左边的货物位于的集装箱下标

  • rr :右边未选择的离now最近货物所在的下标

  • l :lll集装箱选了多少个(因为可能没选完,所以要记录一下)

  • r: rr集装箱有多少个没有选

如果rr离now的距离,比lll离now的距离近,我们就扔掉一些lll的箱子,选择一些rr的箱子,更新lll和rr,直到最优
如果在这个过程中,费用小鱼T了,返回true,否则返回false

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5+5;
ll n,T;
ll x[N],a[N],ps_a[N],ps_ax[N];
void _move(ll &lll,ll &rr,ll &now,ll &l,ll &r,ll &res)
{
        while(x[rr]-x[now]<=x[now]-x[lll] && lll<now && rr<=n )
    {
        if(l>r){
        res+=(x[rr] - x[now])*r;
        res-=(x[now] - x[lll])*r;
        l-=r;
        rr++;r = a[rr];
        }
        else if(l<r){
        res+=(x[rr]-x[now])*l;
        res-=(x[now] - x[lll])*l;
        r-=l;
        lll++;l = a[lll];
        }
        else{
        res+=(x[rr]-x[now])*l;
        res-=(x[now] - x[lll])*l;
        lll++;l = a[lll];
        rr++;r = a[rr];
        }
    }
}
ll check(ll X)
{
   ll  now = lower_bound(ps_a+1,ps_a+n+1,X/2)-ps_a;
   ll  lll = 1,rr = upper_bound(ps_a+1,ps_a+n+1,X)-ps_a,l,r;
    if(lll == rr) l = X,r = a[rr]-X;
    else l = a[lll],r = a[rr] - (X - ps_a[rr-1]);
   ll res = x[now]*ps_a[now-1] - ps_ax[now-1];
   if(rr > now)
   {
       res+=(ps_ax[rr-1]-ps_ax[now])-x[now]*(ps_a[rr-1] - ps_a[now]);
       res+=(a[rr]-r)*(x[rr] - x[now]);
   }
   _move(lll,rr,now,l,r,res);
   if(res <=T) return 1;
    while(now < n)
   {
    now++;
    res += (ps_a[now-1]-ps_a[lll]+l)*(x[now]-x[now-1]);
    if(now<=rr)
    res -= (ps_a[rr-1]-ps_a[now-1]+(a[rr]-r))*(x[now] - x[now-1]);
    _move(lll,rr,now,l,r,res);
    if(res<=T) return 1;
   } 
   return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>T;
    T>>=1;
    for(ll i = 1;i<=n;i++)
    cin>>x[i];
    for(ll i = 1;i<=n;i++)
    {
    cin>>a[i];
    ps_a[i] = ps_a[i-1]+a[i];
    ps_ax[i] = ps_ax[i-1]+a[i]*x[i];
    }
    ll l = 0,r = ps_a[n],ans = 0;
   while(l<=r)
   {
    ll mid = (l+r)>>1;
    if(check(mid)) ans = mid,l = mid+1;
    else r = mid-1;
   } 
   cout<<ans<<endl;
}   

转载于:https://www.cnblogs.com/greenty1208/p/9359564.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值