題目描述
阿明是一名推銷員,他奉命到螺絲街推銷他們公司的產品。螺絲街是一條死衚衕,出口與入口是同一個,街道的一側是圍牆,另一側是住戶。螺絲街一共有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時,阿明最多積累的疲勞值。
輸入輸出樣例
5 1 2 3 4 5 1 2 3 4 5
15 19 22 24 25
5 1 2 2 4 5 5 4 3 4 1
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.