Codeforces 846C Four Segments【思维+预处理+前缀和枚举】

本文介绍了一种算法,用于解决将整数序列通过三个断点分为四个子序列的问题,目标是找到一种划分方式使计算得出的总和最大。文章详细阐述了如何使用预处理方法确定最佳断点位置,并通过双重循环实现高效求解。

C. Four Segments
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of n integer numbers. Let sum(l, r) be the sum of all numbers on positions from l to r non-inclusive (l-th element is counted, r-th element is not counted). For indices l and r holds 0 ≤ l ≤ r ≤ n. Indices in array are numbered from 0.

For example, if a = [ - 5, 3, 9, 4], then sum(0, 1) =  - 5sum(0, 2) =  - 2sum(1, 4) = 16 and sum(i, i) = 0 for each i from 0 to 4.

Choose the indices of three delimiters delim0delim1delim2 (0 ≤ delim0 ≤ delim1 ≤ delim2 ≤ n) and divide the array in such a way that the value of res = sum(0, delim0) - sum(delim0, delim1) + sum(delim1, delim2) - sum(delim2, n) is maximal.

Note that some of the expressions sum(l, r) can correspond to empty segments (if l = r for some segment).

Input

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

The second line contains n numbers a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109).

Output

Choose three indices so that the value of res is maximal. If there are multiple answers, print any of them.

Examples
input
3
-1 2 3
output
0 1 3
input
4
0 0 -1 0
output
0 0 0
input
1
10000
output
1 1 1

题目大意:


用三个断点将整个序列分成四个子序列,使得其计算和最大,输出三个断点的位子。


思路:

设定D【i】【j】表示以i作为起点,以j作为一个断点,将区间【i,n】分成两部分的计算和。

那么我们通过上述预处理,可以得到best【i】,D【i】【j】的最大值,同时维护一个数组pos【i】,表示best【i】条件下的最优解的j的位子。


那么我们可以O(n^2)去枚举前两个断点 ,然后通过得到pos【i】数组来得到第三个位子,整体维护一个最大值取最优即可。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll d[5005][5005];
ll best[5005];
ll pos[5005];
ll a[5005];
ll sum[5005];
int main()
{
    ll n;
    while(~scanf("%I64d",&n))
    {
        memset(sum,0,sizeof(sum));
        for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(ll i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
        for(ll i=1;i<=n+1;i++)
        {
            best[i]=-1000000000000000000;
            for(ll j=i;j<=n+1;j++)
            {
                d[i][j]=(sum[j-1]-sum[i-1])-(sum[n]-sum[j-1]);
                if(d[i][j]>best[i])
                {
                    best[i]=d[i][j];
                    pos[i]=j;
                }
            }
        }
        ll maxn=-1000000000000000000;
        ll ans1,ans2,ans3;
        for(ll i=1;i<=n+1;i++)
        {
            for(ll j=i;j<=n+1;j++)
            {
                if(sum[i-1]-(sum[j-1]-sum[i-1])+best[j]>maxn)
                {
                    maxn=sum[i-1]-(sum[j-1]-sum[i-1])+best[j];
                    ans1=i-1,ans2=j-1,ans3=pos[j]-1;
                }
            }
        }
        printf("%I64d %I64d %I64d\n",ans1,ans2,ans3);
    }
}










评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值