Luogu P2672 推销员


Luogu P2672 推销员

解析

  • 按照向住户推销产品的疲劳值从大到小排序,然后记录前 i 家用户里最远的那家用户的距离 $ (ms[ ]) $ 以及前 i 家用户所造成的总疲劳值 $ (bef[ ]) $ ,然后处理出第 i 家用户后面贡献疲劳值最大的一家 $ (mbeh[ ]) $
  • 对于需要推销 x 户人家,最大疲劳值一定是 “前 x 户疲劳值最大的人家的疲劳值贡献之和 + 这 x 户中最远人家的距离的疲劳值贡献 $ (bef[x]+2 \times ms[x]) $ ” 或 “前 x-1 户疲劳值最大的人家的疲劳值贡献之和 + 第 x 户后面贡献疲劳值最大的一家的疲劳值贡献 $ (bef[x-1]+mbeh[x]) $ ” 中较大的一个

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int N=1e5+5;
struct node
{
	LL s,v;
}c[N];
int n;
LL ms[N],bef[N],mbeh[N];
bool cmp(node a,node b)
{
	return a.v>b.v;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lld",&c[i].s);
	for(int i=1;i<=n;i++) scanf("%lld",&c[i].v);
	sort(c+1,c+n+1,cmp);
	for(int i=1;i<=n;i++) ms[i]=max(ms[i-1],c[i].s);
	for(int i=1;i<=n;i++) bef[i]=bef[i-1]+c[i].v;
	for(int i=n;i>=1;i--) mbeh[i]=max(mbeh[i+1],2*c[i].s+c[i].v);
	for(int i=1;i<=n;i++) printf("%lld\n",max(bef[i-1]+mbeh[i],bef[i]+2*ms[i]));
	return 0;
}
posted @ 2019-09-27 15:23  Hawking_llfz  阅读(87)  评论(0编辑  收藏  举报