【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;
}   
全部评论

相关推荐

身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
小浪_coder:深圳除了一些计算机,UI设计,金融类等一些可以月薪过万的工作之外, 认识很多朋友做运营,营销,文员的工作, 月薪基本都在4-6K左右,还有大把人在干
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
吴offer选手:HR:我KPI到手了就行,合不合适关我什么事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务