transform

链接:https://www.nowcoder.com/acm/contest/140/G
 

题目描述

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.

 

 

输入描述:

The first line of input contains 2 integers n and T(n <= 500000,T <= 1000000000000000000)
In the next line there are n increasing numbers in range [0,1000000000] denoting x[1..n]
In the next line there are n numbers in range[0,10000] denoting a[1..n]

输出描述:

Print an integer denoting the answer.

示例1

输入

复制

2 3
1 2
2 3

输出

复制

4

题意:

有N个集装箱,位置为x[i],有a[i]件东西,移动的花费为2*abs(x[u]-x[v]),问在满足花费小于T的情况下的最大移动数量

分析:

二分+贪心,二分查找可以移动的东西的个数,从前往后依次判断,能往右移就尽量右移,终点的位置依次右移

代码:

#include<bits/stdc++.h>

#define gs(c) (c < '0' || c > '9')
#define gc(c) c = getchar()
#define getint() ({ int w = 0; char gc(c); while (gs(c)) gc(c); while (!gs(c)) w = w*10+c-'0', gc(c); w; })
#define d(i, j) (int64)(x[j] - x[i])

using namespace std;

const int N = 500050;

typedef long long int64;

int n, x[N], a[N];
int64 T, s[N];

int64 sum(int l, int lc, int r, int rc) //集装箱l的第lc个物品到集装箱r的第rc-1个物品一共有多少物品
{
	return l == r? rc - lc: s[r - 1] - s[l] + a[l] - lc + rc;
}
bool check(int64 need) {
	int l = 1, lc = 1, r = n + 1, rc = 1;
	//从集装箱l的lc个物品开始转移,need个物品,到及集装箱r的rc-1,位置,rc位置已将是第need+1个物品了,不移。
	int64 cur = 0, sa = 0;//从第一个集装箱第一个物品开始移都移到第一个集装箱(l=1;lc=1)查找{r,rc},并计算这时最多移的个数
	for (int i = 1; i <= n; ++i) {
		if (sa + a[i] <= need) sa += a[i], cur += d(1, i) * a[i];
		else { r = i, rc = need - sa + 1, cur += d(1, i) * (rc - 1); break; }
	}
	if (cur <= T) return 1;

	for (int i = 2; i <= n; ++i)
	 {
		cur += d(i - 1, i) * (sum(l, lc, i, 1) - sum(i, 1, r, rc));//从1点的第一个物品开始的need个物品移到集装箱i的花费,集装箱i左面的从i-1移到i,集装箱i及右面的要原路返回到i。
		while (r <= n && d(l, i) > d(i, r))//能往右移
            {
			int z = min(a[l] - lc + 1, a[r] - rc + 1);//集装箱l选的移到r的剩余空间,最多移的个数
			cur += (d(i, r) - d(l, i)) * z;//移完后的花费
			if (lc += z, lc > a[l]) ++l, lc = 1;//l选的小,则l集装箱一个都没选,转到下一个集装箱
			if (rc += z, rc > a[r]) ++r, rc = 1;//r剩余的空间小,r集装箱选满,转到下一个集装箱
            }
		if (cur <= T) return 1;
	}
	return 0;
}
int main() {
	cin>>n>>T;
	assert(1<=n&&n<=500000);
	assert(1<=T&&T<=1e18);
	T /= 2;
	for (int i = 1; i <= n; ++i) x[i] = getint(),assert(1<=n&&n<=n),assert(i==1||x[i]>x[i-1]);
	for (int i = 1; i <= n; ++i) a[i] = getint(),assert(0<=a[i]&&a[i]<=1e4), s[i] = s[i - 1] + a[i];

	int64 ll = 0, rr = s[n];//二分答案,做多能转移的货物
	while (ll < rr) {
		int64 mm = (ll + rr + 1) >> 1;
		if (check(mm)) ll = mm;
		else rr = mm - 1;
	}
	cout<<ll<<endl;
}
/*
4 13
1 9 11 13
3 100 2 1
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值