销售员

题目描述

阿明是一名推销员,他奉命到螺丝街推销他们公司的产品。螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户。螺丝街一共有N家住户,第i家住户到入口的距离为Si米。由于同一栋房子里可以有多家住户,所以可能有多家住户与入口的距离相等。阿明会从入口进入,依次向螺丝街的X家住户推销产品,然后再原路走出去。

阿明每走1米就会积累1点疲劳值,向第i家住户推销产品会积累Ai点疲劳值。阿明是工作狂,他想知道,对于不同的X,在不走多余的路的前提下,他最多可以积累多少点疲劳值。

输入输出格式

输入格式:

第一行有一个正整数N,表示螺丝街住户的数量。

接下来的一行有N个正整数,其中第i个整数Si表示第i家住户到入口的距离。数据保证S1≤S2≤…≤Sn<10^8。

接下来的一行有N个正整数,其中第i个整数Ai表示向第i户住户推销产品会积累的疲劳值。数据保证Ai<10^3。

输出格式:

输出N行,每行一个正整数,第i行整数表示当X=i时,阿明最多积累的疲劳值。

输入输出样例

输入样例#1:
5
1 2 3 4 5
1 2 3 4 5
输出样例#1:
15
19
22
24
25
输入样例#2:
5
1 2 2 4 5
5 4 3 4 1
输出样例#2:
12
17
21
24
27

说明

【输入输出样例1说明】

X=1:向住户5推销,往返走路的疲劳值为5+5,推销的疲劳值为5,总疲劳值为15。
X=2:向住户4、5推销,往返走路的疲劳值为5+5,推销的疲劳值为4+5,总疲劳值为5+5+4+5=19。
X=3:向住户3、4、5推销,往返走路的疲劳值为5+5,推销的疲劳值3+4+5,总疲劳值为5+5+3+4+5=22。
X=4:向住户2、3、4、5推销,往返走路的疲劳值为5+5,推销的疲劳值2+3+4+5,总疲劳值5+5+2+3+4+5=24。
X=5:向住户1、2、3、4、5推销,往返走路的疲劳值为5+5,推销的疲劳值1+2+3+4+5,总疲劳值5+5+1+2+3+4+5=25。

【输入输出样例2说明】 X=1:向住户4推销,往返走路的疲劳值为4+4,推销的疲劳值为4,总疲劳值4+4+4=12。

X=2:向住户1、4推销,往返走路的疲劳值为4+4,推销的疲劳值为5+4,总疲劳值4+4+5+4=17。

X=3:向住户1、2、4推销,往返走路的疲劳值为4+4,推销的疲劳值为5+4+4,总疲劳值4+4+5+4+4=21。

X=4:向住户1、2、3、4推销,往返走路的疲劳值为4+4,推销的疲劳值为5+4+3+4,总疲劳值4+4+5+4+3+4=24。或者向住户1、2、4、5推销,往返走路的疲劳值为5+5,推销的疲劳值为5+4+4+1,总疲劳值5+5+5+4+4+1=24。

X=5:向住户1、2、3、4、5推销,往返走路的疲劳值为5+5,推销的疲劳值为5+4+3+4+1,

总疲劳值5+5+5+4+3+4+1=27。

【数据说明】

对于20%的数据,1≤N≤20;

对于40%的数据,1≤N≤100;

对于60%的数据,1≤N≤1000;

对于100%的数据,1≤N≤100000。

【算法】

贪心,每次找权值最大的,设走到当前为止最远的人家的距离为pre

则将其他为选的人家分为两类

第一类s[i]<=pre

第二类s[i]>=pre

找每类最大的,两类取最大值

其中由于pre在不断变大,所以第二类中的人家当s[i]<=pre时要转到第一类

每类都用堆维护

思路,证明,时间复杂的与《木板》一题基本类似

【程序】

//2016-4-10
//God save princess!
//By Shui'Bing Icee
const
  maxn=200000;
var
  n,i,max,s1,l1,l2,max1,max2,pre:longint;
  a,s,heap1,heap2:array[0..maxn] of longint;
  procedure put1(x:longint);
var
 son,temp:longint;
begin
  inc(l1);
  son:=l1;
  heap1[l1]:=x;
  while (son<>1) and (a[heap1[son]]>a[heap1[son div 2]]) do
    begin
      temp:=heap1[son];
      heap1[son]:=heap1[son div 2];
      heap1[son div 2]:=temp;
      son:=son div 2;
    end;
end;
  procedure put2(x:longint);
var
 son,temp:longint;
begin
  inc(l2);
  son:=l2;
  heap2[l2]:=x;
  while (son<>1) and ((a[heap2[son]]+2*(s[heap2[son]]-pre))>(a[heap2[son div 2]]+2*(s[heap2[son div 

2]]-pre))) do
    begin
      temp:=heap2[son];
      heap2[son]:=heap2[son div 2];
      heap2[son div 2]:=temp;
      son:=son div 2;
    end;
end;

  procedure get1;
var
 fa,son,temp:longint;
begin
  heap1[1]:=heap1[l1];
  heap1[l1]:=0;
  dec(l1);
  fa:=1;
  while (fa*2<=l1) do
    begin
      if (fa*2+1>l1) or (a[heap1[fa*2]]>a[heap1[fa*2+1]])
      then son:=fa*2
      else son:=fa*2+1;
      if a[heap1[fa]]<a[heap1[son]]
      then begin
            temp:=heap1[fa];
            heap1[fa]:=heap1[son];
            heap1[son]:=temp;
            fa:=son;
           end
      else break;
    end;
end;

 procedure get2;
var
  fa,son,temp:longint;
begin
  heap2[1]:=heap2[l2];
  heap2[l2]:=0;
  dec(l2);
  fa:=1;
  while (fa*2<=l2) do
    begin
      if (fa*2+1>l2) or ((a[heap2[fa*2]]+2*(s[heap2[fa*2]]-pre))>(a[heap2[fa*2+1]]+2*(s[heap2

[fa*2+1]]-pre)))
      then son:=fa*2
      else son:=fa*2+1;
      if (a[heap2[fa]]+2*(s[heap2[fa]]-pre))<(a[heap2[son]]+2*(s[heap2[son]]-pre))
      then begin
            temp:=heap2[fa];
            heap2[fa]:=heap2[son];
            heap2[son]:=temp;
            fa:=son;
           end
      else break;
    end;
end;

begin
  read(n);
  for i:=1 to n do
    begin
      read(s[i]);
    end;
  for i:=1 to n do
    begin
      read(a[i]);
    end;
  max:=0;
  s1:=0;
  for i:=1 to n do
    begin
      if s[i]*2+a[i]>max
      then begin max:=s[i]*2+a[i];max1:=i;end;
    end;
  pre:=s[max1];
  s1:=s1+s[max1]*2+a[max1];
  writeln(s1);
  l1:=0;
  l2:=0;
  for i:=1 to n do
    begin
      if i=max1
      then continue;
      if s[i]>pre
      then put2(i)
      else put1(i);
    end;
  for i:=2 to n  do
    begin
      while (s[heap2[1]]<pre) and (l2>0) do
        begin
          put1(heap2[1]);
          get2;
        end;
      max2:=heap2[1];
      max1:=heap1[1];
      if (a[max1])>(a[max2]+(s[max2]-pre)*2)
      then begin
             s1:=s1+a[max1];
             get1;
           end
      else if (a[max1])<(a[max2]+(s[max2]-pre)*2)
           then begin
                  s1:=s1+a[max2]+(s[max2]-pre)*2;
                  get2;
                end
           else begin
                  s1:=s1+a[max1];
                  if max1<max2
                  then get1
                  else get2;
                end;
      writeln(s1);
    end;
end.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值