Educational Codeforces Round 108 (Rated for Div. 2)-D. Maximum Sum of Products-题解

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 108 (Rated for Div. 2)-D. Maximum Sum of Products-题解相关的知识,希望对你有一定的参考价值。

Educational Codeforces Round 108 (Rated for Div. 2)-D. Maximum Sum of Products

传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes

Problem Description

You are given two integer arrays a a a and b b b of length n n n.

You can reverse at most one subarray (continuous subsegment) of the array a a a.

Your task is to reverse such a subarray that the sum ∑ i = 1 n a i ⋅ b i \\sum\\limits_{i=1}^n a_i \\cdot b_i i=1naibi is maximized.

Input

The first line contains one integer n n n ( 1 ≤ n ≤ 5000 1 \\le n \\le 5000 1n5000).

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \\dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 7 1 \\le a_i \\le 10^7 1ai107).

The third line contains n n n integers b 1 , b 2 , … , b n b_1, b_2, \\dots, b_n b1,b2,,bn ( 1 ≤ b i ≤ 1 0 7 1 \\le b_i \\le 10^7 1bi107).

Output

Print single integer — maximum possible sum after reversing at most one subarray (continuous subsegment) of a a a.

Sample Input

5
2 3 2 1 3
1 3 2 4 2

Sample Onput

29

Note

In the first example, you can reverse the subarray [ 4 , 5 ] [4, 5] [4,5]. Then a = [ 2 , 3 , 2 , 3 , 1 ] a = [2, 3, 2, 3, 1] a=[2,3,2,3,1] and 2 ⋅ 1 + 3 ⋅ 3 + 2 ⋅ 2 + 3 ⋅ 4 + 1 ⋅ 2 = 29 2 \\cdot 1 + 3 \\cdot 3 + 2 \\cdot 2 + 3 \\cdot 4 + 1 \\cdot 2 = 29 21+33+22+34+12=29.

In the second example, you don’t need to use the reverse operation. 13 ⋅ 2 + 37 ⋅ 4 = 174 13 \\cdot 2 + 37 \\cdot 4 = 174 132+374=174.

In the third example, you can reverse the subarray [ 3 , 5 ] [3, 5] [3,5]. Then a = [ 1 , 8 , 3 , 6 , 7 , 6 ] a = [1, 8, 3, 6, 7, 6] a=[1,8,3,6,7,6] and 1 ⋅ 5 + 8 ⋅ 9 + 3 ⋅ 6 + 6 ⋅ 8 + 7 ⋅ 8 + 6 ⋅ 6 = 235 1 \\cdot 5 + 8 \\cdot 9 + 3 \\cdot 6 + 6 \\cdot 8 + 7 \\cdot 8 + 6 \\cdot 6 = 235 15+89+36+68+78+66=235.


题目大意

给你两个数组 a a a b b b以及一个整数 n n n,每个数组中有 n n n个正整数。你可以选择把 a a a的一个子串翻转,使得 ∑ a i ∗ b i \\sum a_i*b_i aibi最大。


解题思路

字串必须是连续的!

首先来想最朴素的算法:

i 从 1 到 n:
    j 从 i 到 n:
        k 从 1 到 n:
            如果 i ≤ k ≤ j:
                ans += a[k] * b[j - k + i]
            否则:
                ans += a[k] * b[k]

这样的话复杂度为 O ( n 3 ) O(n^3) O(n3),然而 n n n的范围是 1 1 1~ 5000 5000 5000,会超时。


简单优化一下:
因为 i i i ~ j j j外面的 a [ k ] a[k] a[k] b [ k ] b[k] b[k]是一一对应的,没有翻转,故可以用一个前缀数组 q i a n Z u i qianZui qianZui,其中 q i a n Z u i [ i ] qianZui[i] qianZui[i]表示 ∑ k = 1 i a [ k ] ∗ b [ k ] \\sum_{k=1}^ia[k]*b[k] k=1ia[k]b[k]。这样的话处于 l l l ~ r r r外面的所有 a [ i ] ∗ b [ i ] a[i]*b[i] a[i]b[i]的和就可以用 O ( 1 ) O(1) O(1)的时间快速得出,为 q i a n Z u i [ n ] − q i a n Z u i [ r ] + q i a n Z u i [ l − 1 ] qianZui[n]-qianZui[r]+qianZui[l-1] qianZui[n]qianZui[r]+qianZui[l1]


但是, l l l ~ r r r 中的元素还是需要一个一个计算,这就使得总体复杂度仍为 O ( n 3 ) O(n^3) O(n3)


继续优化:
既然第三层复杂度的来源是重新计算了 l l l ~ r r r 中每个 a a a b b b相乘,那么能不能找到一种方法,使得后面的 l l l ~ r r r(翻转部分)的计算基于前面的 l l l ~ r r r的计算结果之上呢?


于是我们想到,如果已知 l l l ~ r r r 的翻转后的 a a a b b b相乘的值 s 1 s_1 s1,那么就能很快求出 l − 1 l-1 l1 ~ r + 1 r+1 r+1 的翻转后的 a a a b b b相乘的值 s 2 s_2 s2,其中 s 2 = s 1 + a [ l − 1 ] ∗ b [ r + 1 ] + a [ r + 1 ] ∗ b [ l − 1 ] s_2=s_1+a[l-1]*b[r+1]+a[

以上是关于Educational Codeforces Round 108 (Rated for Div. 2)-D. Maximum Sum of Products-题解的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27