poj 2796 Feel Good

本文介绍了一种计算人类生活期间情绪价值的方法,通过将每个日子分配一个非负整数值来量化其情绪价值,并通过特定公式计算出一段时间内的总情绪价值。文章提供了一个C++实现示例,用于找出具有最大情绪价值的生活阶段。

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

Description

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. 

A new idea Bill has recently developed assigns a non-negative integer value to each day of human life. 

Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day. 

Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.


题意是给出一串数,求给定的范围内最小值乘该范围内所有值的和的最大值

方法和poj 2559相同,仍然是假设某一点的数值是最小的,求左右两端可到达的最远的位置

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX_N 100000
using namespace std;
int n,a[MAX_N+5],l[MAX_N+5],r[MAX_N+5];
int main()
{
    while(~scanf("%d",&n))
    {
        long long sum[MAX_N+5];
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        l[0]=0,r[n+1]=0;
        for(int i=1;i<=n;i++)
        {
            int k=i-1;
            while(k>0&&a[i]<=a[k]) k=l[k]-1;
            l[i]=++k;
        }
        for(int i=n;i>=1;i--)
        {
            int k=i+1;
            while(k<n+1&&a[i]<=a[k]) k=r[k]+1;
            r[i]=--k;
        }
        sum[0]=0,sum[1]=a[1];
        for(int i=2;i<=n;i++)
            sum[i]=sum[i-1]+a[i];
        long long maxn=-1;
        int maxi;
        for(int i=1;i<=n;i++)
        {
            long long q=sum[r[i]]-sum[l[i]-1];
            q=a[i]*q;
            if(q>maxn) { maxn=q; maxi=i;}
        }
        printf("%lld\n",maxn);
        printf("%d %d\n",l[maxi],r[maxi]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值