Codeforce Educational Codeforces Round 108 (Rated for Div. 2)

这篇博客探讨了一种动态规划的解决方案,用于在两个整数数组中找到最佳子数组翻转策略,以最大化两个数组对应元素乘积之和。通过分析问题,可以将时间复杂度降低到O(n^2),并处理了数组长度为奇数或偶数的情况。博客提供了AC代码示例,展示了如何在不超过一次翻转的情况下找到最大乘积和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Maximum Sum of Products

time limit per test2 seconds
memory limit per test256 megabytes
input standard input
outpu tstandard output

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

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

Your task is to reverse such a subarray that the sum ∑i=1nai⋅bi is maximized.

Input
The first line contains one integer n (1≤n≤5000).

The second line contains n integers a1,a2,…,an (1≤ai≤107).

The third line contains n integers b1,b2,…,bn (1≤bi≤107).

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

Examples
input
5
2 3 2 1 3
1 3 2 4 2
output
29

input
2
13 37
2 4
output
174

input
6
1 8 7 6 3 6
5 9 6 8 8 6
output
235
Note
In the first example, you can reverse the subarray [4,5]. Then a=[2,3,2,3,1] and 2⋅1+3⋅3+2⋅2+3⋅4+1⋅2=29.

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

In the third example, you can reverse the subarray [3,5]. Then a=[1,8,3,6,7,6] and 1⋅5+8⋅9+3⋅6+6⋅8+7⋅8+6⋅6=235.

思路:
在这里插入图片描述
也就是说,从以5为中心反转区间时,新的和可以从上一步的和转变过来,如此复杂度就降到了O(n^2),还要注意 数组的长度是 奇数 还是 偶数。

AC代码:

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <functional>
#include <time.h>
using namespace std;
#define ll long long //__int64 __int128
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define mst(a, b) memset(a, b, sizeof(a))
#define max(a, b) (a>b? a:b)
#define min(a, b) (a<b? a:b) //a&((a-b)>>31)|b&(~(a-b)>>31)
#define closeSync std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mCase int T; scanf("%d", &T); for(int Case=1;Case<=T;++Case)
#define P pair<ll, ll>
#define maxn 200005
const int INF=0x3f3f3f3f; //0x7f7f7f7f
const long long LINF=0x3f3f3f3f3f3f3f3f; //0x7f7f7f7f7f7f7f7f
const double PI=acos(-1.0);
const double eps=1e-6;
const int mod=1000000007; // 998244353
//clock_t start=clock(); clock_t end=clock(); printf("time=%f\n",(double)(end-start)/CLOCKS_PER_SEC);

//gcd(a,b)*lcm(a,b)=a*b
ll gcd(ll a, ll b){ return b?gcd(b, a-a/b*b):a;}
ll lcm(ll a, ll b){ return a*b/gcd(a, b);}
ll qmul(ll a, ll b){ ll r=0; while(b){ if(b&1) r=(r+a)%mod; b>>=1; a=(a+a)%mod;} return r;}
ll Kpow(ll x, ll n){ ll re=1; for(;n;n>>=1) { if(n&1) { re*=x; re=re-re/mod*mod;} x*=x; x=x-x/mod*mod;} return re;}
ll Kpow(ll x, ll n, ll p){ ll re=1; for(;n;n>>=1) { if(n&1) { re*=x; re=re-re/p*p;} x*=x; x=x-x/p*p;} return re;}

int P_num[700005], P_cnt; //664579 in 1e7, 5761455 in 1e8
bool isP[10000007];

void GetPrime(int Pn){
    memset(isP, 1, sizeof(isP)), isP[1]=P_cnt=0;
    for(int i=2;i<=Pn;++i){
        if(isP[i]) P_num[++P_cnt]=i;
        for(int j=1;j<=P_cnt && i*P_num[j]<=Pn;++j){
            isP[i*P_num[j]]=0;
            if(i%P_num[j]==0) break;
        }
    }
}

ll n, l, r;
ll s[200005];
ll sum[200005];
ll t[200005];

void work(){ //主程序 其余不用管
	cin>>n;
	for(int i=1;i<=n;++i) cin>>s[i];
	for(int i=1;i<=n;++i) cin>>t[i];
	for(int i=1;i<=n;++i) sum[i]=sum[i-1]+s[i]*t[i]; // 前缀和
	ll re=sum[n]; // 默认最大值是没有区间翻转的时候
	for(int i=1;i<=n;++i){
		ll nw=s[i]*t[i];
		for(int l=i-1, r=i+1; l>0 && r<=n; --l, ++r){
			nw+=s[l]*t[r]+s[r]*t[l];
			re=max(re, sum[l-1]+sum[n]-sum[r]+nw);
		}
		nw=0;
		for(int l=i, r=i+1; l>0 && r<=n; --l, ++r){
			nw+=s[l]*t[r]+s[r]*t[l];
			re=max(re, sum[l-1]+sum[n]-sum[r]+nw);
		}
	}
	cout<<re<<endl;
}

int main()
{
	//closeSync;
	work();

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值