I - Inquiry I(简单前缀和应用)

本文探讨了一个竞赛编程问题,目标是找到给定数组中特定乘积表达式的最大值。通过使用前缀和技巧,避免了时间限制内的暴力求解,实现了高效的算法解决方案。

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

题目
The Bureau for Artificial Problems in Competitions wants you to solve the following problem: Given n positive integers a1, . . . , an, what is the maximal value of
在这里插入图片描述
forma.jpg

Input:

• A single line containing an integer 2 ≤ n ≤ 10^6.

• Then follow n lines, the ith of which contains the integer 1 ≤ ai ≤ 100.

Output:

Output the maximal value of the given expression.

样例输入1
5
2
1
4
3
5
样例输出1
168
样例输入2
2
1
1
样例输出2
1
样例输入3
10
8
5
10
9
1
4
12
6
3
13
样例输出3
10530

题意:给出a[1]……a[n]的数组序列,求(a1a1+……akak)*(a(k+1)a(k+1)+……+anan)最大值。

思路:1s的时间限制,n为1e6暴力不可做,用前缀和。wa的原因是数组的数据类型为int,连续累加应l,r数组应为long long。

AC代码

#include <bits/stdc++.h>

using namespace std;
int a[1000010];
long long l[1000010],r[1000010];
int main()
{
    int n;
    scanf("%d",&n);
    l[0]=0;
    r[0]=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        l[i]=l[i-1]+a[i]*a[i];
        r[i]=r[i-1]+a[i];
    }
    long long ans=-1;
    for(int i=1; i<=n; i++)
    {
        if((l[i]*(r[n]-r[i]))>ans)
        {
            ans=l[i]*(r[n]-r[i]);
        }
    }
    printf("%lld\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值