CF1662I Ice Cream Shop

Ice Cream Shop

题目描述

On a beach there are $ n $ huts in a perfect line, hut $ 1 $ being at the left and hut $ i+1 $ being $ 100 $ meters to the right of hut $ i $ , for all $ 1 \le i \le n - 1 $ . In hut $ i $ there are $ p_i $ people.

There are $ m $ ice cream sellers, also aligned in a perfect line with all the huts. The $ i $ -th ice cream seller has their shop $ x_i $ meters to the right of the first hut. All ice cream shops are at distinct locations, but they may be at the same location as a hut.

You want to open a new ice cream shop and you wonder what the best location for your shop is. You can place your ice cream shop anywhere on the beach (not necessarily at an integer distance from the first hut) as long as it is aligned with the huts and the other ice cream shops, even if there is already another ice cream shop or a hut at that location. You know that people would come to your shop only if it is strictly closer to their hut than any other ice cream shop.

If every person living in the huts wants to buy exactly one ice cream, what is the maximum number of ice creams that you can sell if you place the shop optimally?

输入格式

The first line contains two integers $ n $ and $ m $ ( $ 2 \le n \le 200,000 $ , $ 1 \le m \le 200,000 $ ) — the number of huts and the number of ice cream sellers.

The second line contains $ n $ integers $ p_1, p_2, \ldots, p_n $ ( $ 1 \le p_i \le 10^9 $ ) — the number of people in each hut.

The third line contains $ m $ integers $ x_1, x_2, \ldots, x_m $ ( $ 0 \le x_i \le 10^9 $ , $ x_i \ne x_j $ for $ i \ne j $ ) — the location of each ice cream shop.

输出格式

Print the maximum number of ice creams that can be sold by choosing optimally the location of the new shop.

中文题意

沙滩上有 \(n\) 个小屋在一条直线上,第 \(1\) 个小屋在所有点的最左端,第 \(i+1\) 个小屋在第 \(i\) 个小屋的右侧 \(100m\) 处。(\(1\le i\le n-1\))每座小屋里有 \(p_i\) 个人。

这条直线上另有 \(m\) 个冰淇淋店,第 \(i\) 个冰淇淋店在第一座小屋右侧 \(x_i\) 米处。所有冰淇淋店的位置各不相同,但他们可能和小屋重合。

你将要开一家新的冰淇淋店(可以与其它冰淇淋点或小屋重合,位置可以为浮点数),一个小屋的人会到你的冰淇淋店买东西当且仅当这个小屋到你的距离严格小于到其他所有店的距离,问如何最优选址使得来你的店买冰淇淋的人最大化

思路

发现开一家店是在相邻两家店之间抢顾客,然后考虑如果在 \(x_i\)\(x_{i+1}\) 之间开店,长度为 \(\frac{x_{i+1}-x_i}{2}\) 的区间能取到,由于题目要求严格小于,边界处理较为麻烦

#include <algorithm>
#include <cstdio>
using namespace std;
using ll = long long;
const int maxn = 2e5 + 10;
int n,m,p[maxn],x[maxn];
ll pre[maxn];
int main() {
	scanf("%d%d",&n,&m);
	for (int i = 1;i <= n;i++) {
		scanf("%d",&p[i]);
		pre[i] = pre[i-1]+p[i];
	}
	for (int i = 1;i <= m;i++) scanf("%d",&x[i]);
	sort(x+1,x+m+1);
	ll ans = pre[n]-pre[min(x[m]/100+1,n)];
	if (x[1]) ans = max(ans,pre[min((x[1]-1)/100+1,n)]);
	for (int i = 1;i < m;i++) {
		int l = x[i]/100+2,r = min((x[i+1]-1)/100+1,n),k = (x[i+1]-x[i]-1)/200+1;
		for (int j = l;j <= r;j++)
			ans = max(ans,pre[min(j+k-1,r)]-pre[j-1]);
	}
	printf("%lld",ans);
	return 0;
}
posted @   lrj124  阅读(5)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示