zoukankan      html  css  js  c++  java
  • Luogu P2672 推销员


    Luogu P2672 推销员

    解析

    • 按照向住户推销产品的疲劳值从大到小排序,然后记录前 i 家用户里最远的那家用户的距离 $ (ms[ ]) $ 以及前 i 家用户所造成的总疲劳值 $ (bef[ ]) $ ,然后处理出第 i 家用户后面贡献疲劳值最大的一家 $ (mbeh[ ]) $
    • 对于需要推销 x 户人家,最大疲劳值一定是 “前 x 户疲劳值最大的人家的疲劳值贡献之和 + 这 x 户中最远人家的距离的疲劳值贡献 $ (bef[x]+2 imes 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
    ",max(bef[i-1]+mbeh[i],bef[i]+2*ms[i]));
    	return 0;
    }
    
  • 相关阅读:
    加法问题,编程入门课
    三.Python变量,常量,注释
    二、python介绍
    一. python 安装
    Syntax behind sorted(key=lambda :)
    Java动态数组
    Package name does not correspond to the file path (IntelliJ IDEA)
    理解Java构造器中的"this"
    The Constructor with No Arguments
    160229-01、web页面常用功能js实现
  • 原文地址:https://www.cnblogs.com/Hawking-llfz/p/11598220.html
Copyright © 2011-2022 走看看