• 【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;
    }	
    
    
  • 相关阅读:
    是什么意思
    Hql查询
    java的LINQ :Linq4j简明介绍
    Sqlite内存数据库
    ACE中的Proactor介绍和应用实例
    mysql的锁表问题
    消息:'null'为空或不是对象
    FusionChart中引入图类型和数据源方法
    实现FusionChart动态获取数据(二)
    JSP页面解决文件路径方法
  • 原文地址:https://www.cnblogs.com/greenty1208/p/9359564.html
Copyright © 2020-2023  润新知