CF1654A.Maximum Cake Tastiness
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n pieces of cake on a line. The i-th piece of cake has weight ai (1≤i≤n).
The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i. e., max(a1+a2,a2+a3,…,an−1+an)).
You want to maximize the tastiness of the cake. You are allowed to do the following operation at most once (doing more operations would ruin the cake):
- Choose a contiguous subsegment a[l,r] of pieces of cake (1≤l≤r≤n), and reverse it.
The subsegment a[l,r] of the array a is the sequence al,al+1,…,ar.
If you reverse it, the array will become a1,a2,…,al−2,al−1,ar,ar−1,…,al+1,al,ar+1,ar+2,…,an−1,an.
For example, if the weights are initially [5,2,1,4,7,3], you can reverse the subsegment a[2,5], getting [5,7,4,1,2,3]. The tastiness of the cake is now 5+7=12 (while before the operation the tastiness was 4+7=11).
Find the maximum tastiness of the cake after doing the operation at most once.
一条直线上有 n 块蛋糕。第 i 块蛋糕的重量为 ai(1≤i≤n)。
蛋糕的“美味度”定义为所有相邻两块蛋糕重量之和的最大值,即
max(a1+a2,a2+a3,…,an−1+an)。
你希望最大化蛋糕的美味度。你最多可执行一次如下操作(执行多次会毁掉蛋糕):
- 选择一段连续子区间 a[l,r](1≤l≤r≤n),并将该子区间反转。
数组 a 的子区间 a[l,r] 指序列 al,al+1,…,ar。
反转后,数组变为
a1,a2,…,al−2,al−1,ar,ar−1,…,al+1,al,ar+1,ar+2,…,an−1,an。
例如,若初始重量为 [5,2,1,4,7,3],你可以反转子区间 a[2,5],得到 [5,7,4,1,2,3]。此时蛋糕的美味度为 5+7=12(而操作前美味度为 4+7=11)。
求在最多执行一次上述操作后,蛋糕所能达到的最大美味度。
输入格式
The first line contains a single integer t (1≤t≤50) — the number of test cases.
The first line of each test case contains a single integer n (2≤n≤1000) — the number of pieces of cake.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — ai is the weight of the i-th piece of cake.
第一行包含一个整数 t(1≤t≤50)—— 表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤1000)—— 表示蛋糕块的数量。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 其中 ai 表示第 i 块蛋糕的重量。
输出格式
For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.
对于每个测试用例,输出一个整数:执行该操作至多一次后蛋糕的最大美味度。
输入输出样例
输入#1
5 6 5 2 1 4 7 3 3 32 78 78 3 69 54 91 8 999021 999021 999021 999021 999652 999021 999021 999021 2 1000000000 1000000000
输出#1
12 156 160 1998673 2000000000
说明/提示
In the first test case, after reversing the subsegment a[2,5], you get a cake with weights [5,7,4,1,2,3]. The tastiness of the cake is now max(5+7,7+4,4+1,1+2,2+3)=12. This is the maximum possible tastiness of the cake one can obtain by performing the operation at most once.
In the second test case, it's optimal not to do any operation. The tastiness is 78+78=156.
In the third test case, after reversing the subsegment a[1,2], you get a cake with weights [54,69,91]. The tastiness of the cake is now max(54+69,69+91)=160. There is no way to make the tastiness of the cake greater than 160 by performing at most one operation.
在第一个测试用例中,将子段 a[2,5] 反转后,得到的蛋糕重量为 [5,7,4,1,2,3]。此时蛋糕的美味度为 max(5+7,7+4,4+1,1+2,2+3)=12。这是至多执行一次操作所能达到的最大美味度。
在第二个测试用例中,最优策略是不执行任何操作。此时美味度为 78+78=156。
在第三个测试用例中,将子段 a[1,2] 反转后,得到的蛋糕重量为 [54,69,91]。此时蛋糕的美味度为 max(54+69,69+91)=160。至多执行一次操作无法使蛋糕的美味度超过 160。
输入解题思路,AI测评打分。不知道怎么写?