D. Maximum Sum of Products

链接

https://codeforces.com/problemset/problem/1519/D

题目

分析

总的来说不算难的一道题,主要是敢写就行,控制在O(n^2),枚举中心点,分成两类:一类是奇数,一类是偶数对称就行。

代码

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#define IOS ios::sync_with_stdio(false), cin.tie(0) ,cout.tie(0)
using namespace std;
#define int long long
const int N = 5e3 + 10;
int a[N], b[N];
int ans = 0;
signed main()
{
	IOS;
	int n; cin >> n;
	for (int i = 1; i <= n; i++)cin >> a[i];
	for (int i = 1; i <= n; i++)cin >> b[i];
	for (int i = 1; i <= n; i++)ans += a[i] * b[i];
	int sum = ans;
	for (int i = 1; i <= n; i++)
	{
		int ansh = sum;
		//中心点扩散
		for (int j = 1; i - j > 0 and i + j <= n; j++)
		{
			ansh += (a[i + j] - a[i - j]) * (b[i - j] - b[i + j]);
			ans = max(ans, ansh);
		}
	}
	for (int i = 1; i <= n; i++)
	{
		int ansh = sum;
		//两边扩散
		for (int j = 0; i - j > 0 and i + j + 1 <= n; j++)
		{
			ansh += (a[i + 1 + j] - a[i - j]) * (b[i - j] - b[i + 1 + j]);
			ans = max(ans, ansh);
		}
	}
	cout << ans;



	return 0;
}

posted on 2024-07-19 09:52  WHUStar  阅读(3)  评论(0编辑  收藏  举报