Codeforces Round 108(Problem - D Maximum Sum of Products)

Posted 偶尔爆零的蒟蒻

tags:

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

题意:给定长度为 n n n的数组 a , b a,b a,b,你可以通过反转 a a a的一段连续子序列,来求得 m a x ∑ i = 1 n a i ⋅ b i max\\sum_i=1^na_i·b_i maxi=1naibi

区间DP
注意:三重循环会TLE
优化:计算小区间的反转与未反转的差,每次都利用中间的部分,左右端点另算,减少重复计算。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[5005],b[5005];
ll dp[5005][5005];
int main()
    ios_base::sync_with_stdio(false);cin.tie(0),cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    ll sum=0;
    for(int i=1;i<=n;i++)
        cin>>b[i];
        sum+=1ll*a[i]*b[i];
    
    ll maxx=0;
    for(int len=1;len<n;len++)
        for(int i=1;i+len<=n;i++)
            int j=i+len;
            //重复利用小区间的反转与未反转差值
            //内侧左右端的差
            //去掉枚举分割点的循环语句,减小到n方复杂度
            //cha=a[i]b[j]+a[j]b[i]-a[i]b[i]-a[j]b[j]
            dp[i][j]=dp[i+1][j-1]+1ll*(a[j]-a[i])*(b[i]-b[j]);
            maxx=max(maxx,dp[i][j]);
        
    
    //dp[i][j]表示反转i~j,能增加多少
    cout<<sum+maxx<<endl;


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

Educational Codeforces Round 108 (Rated for Div. 2)-A. Red and Blue Beans-题解

Codeforces Beta Round #108 (Div2)

Codeforces Round 108 (C. Berland Regional)

Educational Codeforces Round 108 (Rated for Div. 2)Codeforces-1519ABCD

Educational Codeforces Round 108 (Rated for Div. 2)-B. The Cake Is a Lie-题解

Educational Codeforces Round 108 (Rated for Div. 2) ABCD题解