2020牛客暑期多校训练营(第八场)K.Kabaleo Lite
题目描述
Tired of boring WFH (work from home), Apollo decided to open a fast food restaurant, called
Kabaleo
Lite
\textbf{Kabaleo Lite}
Kabaleo Lite.
The restaurant serves n kinds of food, numbered from 1 to n. The profit for the i-th kind of food is
a
i
a_i
ai. Profit may be negative because it uses expensive ingredients. On the first day, Apollo prepared
b
i
b_i
bi dishes of the i-th kind of food.
The peculiarity of Apollo’s restaurant is the procedure of ordering food. For each visitor Apollo himself chooses a set of dishes that this visitor will receive. When doing so, Apollo is guided by the following rules:
every visitor should receive at least one dish.
each visitor should receive continuous kinds of food started from the first food. And the visitor will receive exactly 1 dish for each kind of food. For example, a visitor may receive 1 dish of the 1st kind of food, 1 dish of the 2nd kind of food, 1 dish of the 3rd kind of food.
What’s the maximum number of visitors Apollo can feed? And he wants to know the maximum possible profits he can earn to have the maximum of visitors.
输入描述:
The first line of the input gives the number of test case,
T
(
1
≤
T
≤
10
)
\mathbf{T} (1 \leq \mathbf{T} \leq 10)
T(1≤T≤10).
T
\mathbf{T}
T test cases follow.
Each test case begins with a line containing one integers
n
(
1
≤
n
≤
1
0
5
)
n (1 \le n \le 10^5)
n(1≤n≤105), representing the number of different kinds of food.
The second line contains n space-separated numbers
a
i
(
−
1
0
9
≤
a
i
≤
1
0
9
)
a_i (-10^9 \le a_i \le 10^9)
ai(−109≤ai≤109), where
a
i
a_i
ai denotes the profit of one dish of the i-th kind.
The third line contains n space-separated numbers
b
i
(
1
≤
b
i
≤
1
0
5
)
b_i (1 \le b_i \le 10^5)
bi(1≤bi≤105), where
b
i
b_i
bi denotes the number of the i-th kind of dishes.
输出描述:
For each test case, output one line containing “Case #x: y z”
, where x is the test case number (starting from 1), y is the maximum number of visitors, and z is the maximum possible profits.
示例1
输入
2
3
2 -1 3
3 2 1
4
3 -2 3 -1
4 2 1 2
输出
Case #1: 3 8
Case #2: 4 13
题意简单,实际上算法也简单,就是贪心取最大前缀和就行,但是细心点你就会发现,题目的最大数据能达到
1
e
19
1e19
1e19,所以会爆
l
o
n
g
l
o
n
g
longlong
longlong,看了题解不禁感叹自己的菜,好像人均会
i
n
t
128
int128
int128,用
p
y
py
py 的我在角落瑟瑟发抖,简单讲述贪心策略:
1.首先令
a
n
s
=
a
[
0
]
∗
b
[
0
]
ans=a[0]*b[0]
ans=a[0]∗b[0],设这几个变量,
m
n
mn
mn 记录当前能购买的最大数量,
s
u
m
sum
sum 记录当前前缀和,
m
x
mx
mx 记录遍历过程中的最大前缀和
2.当
s
u
m
>
m
x
sum>mx
sum>mx 时,
a
n
s
+
=
(
s
u
m
−
m
x
)
∗
m
n
,
m
x
=
s
u
m
ans+=(sum-mx)*mn,mx=sum
ans+=(sum−mx)∗mn,mx=sum 即可,AC代码如下:
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
mn=b[0]
ans=a[0]*b[0]
sum=mx=a[0]
for j in range(1,n):
mn=min(mn,b[j])
sum+=a[j]
if sum>mx:
ans+=(sum-mx)*mn
mx=sum
print('Case #{}: {} {}'.format(i+1,b[0],ans))