[ACM] POJ 2796 Feel Good (求序列中子序列的和与子序列中的最小数最大乘积)

Feel Good

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9186   Accepted: 2509
Case Time Limit: 1000MS   Special Judge

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.

Input

The first line of the input contains n - the number of days of Bill‘s life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, ... an ranging from 0 to 106 - the emotional values of the days.
Numbers are separated by spaces and/or line breaks.

Output

Print the greatest value of some period of Bill‘s life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill‘s life(inclusive) has the greatest possible value. If there are multiple periods with
the greatest possible value,then print any one of them.

Sample Input

6
3 1 6 4 5 2

Sample Output

60
3 5

Source

Northeastern Europe 2005

解题思路:

给定一序列,求一个子序列要求子序列中的和与子序列中的最小数乘积最大,输出最大乘积以及子序列的起始位置。

我们可以转化为求序列中的每一个数所属于哪个区间,保证该数在这个区间内是最小的,并且这个区间长度最大(比如  3 4 2  6,     2所属区间可以为(4,2)也可以为(4,2,6),我们要求最大的(3,4,2,6),因为要保证子序列的和最大)。

这样求出每个数的所属区间,比较一下就可以了。传统的求每个数所属区间时间复杂度高O(n2),可以采用动规的思想,也可以叫回溯,也和KMP的next数组思想有点像

求第i个数所属区间的左端点L[i]时,L[i]肯定小于等于i,所以把i从第i-1个数(i前面的)开始比较,如果第i-1个数比第i个数小,那么肯定L[i] = i+1,第i-1个数小于第i个数,肯定不会在第i个数所属的区间,如果第i-1个数比第i个数大,那么就用第i个数与  L[ i -1] -1 (第i-1个数所属区间左端点的前一个数)进行比较。

求右端点的思想和求左端点的思想是一样的,逆向求。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=100010;
int s[maxn];//存放第i个元素在某个区间内为最小值的区间左端点
int e[maxn];//存放第i个元素在某个区间内为最小值的区间右端点
int node[maxn];//存放输入元素
long long ans;//输出结果
long long sum[maxn];//存放第1到i个元素的和
int S=1,E=1;//输出所求区间的起始位置

int main()
{
    int n;cin>>n;
    sum[0]=0;//以下三行初始化为后面做准备
    node[0]=-1;
    node[n+1]=-1;
    for(int i=1;i<=n;i++)//求第1到i个元素和,并且求出第i个元素所属区间的左端点
    {
        scanf("%d",&node[i]);
        sum[i]=sum[i-1]+node[i];//求和
        for(int j=i-1;j>=0;)//第i个元素从它前面的第一个元素比较
        {
            if(node[i]>node[j])
            {
                s[i]=j+1;//肯定是j+1,因为第j个元素肯定不在第i个元素所属区间内,得保证第i个元素在所属区间内为最小值
                break;
            }
            else
                j=s[j]-1;//回溯,从第j个元素所属区间的前面第一个开始比较,如 1 5 4 2, 2<4,4所属区间为(5,4),s[j]=2,所以与第一个元素1比较
        }
    }
    for(int i=n;i>=1;i--)//逆向求第i个元素所属区间的右端点
    {
        for(int j=i+1;j<=n+1;)//从i后面的第一个元素比较。
        {
            if(node[i]>node[j])//原理同上
            {
                e[i]=j-1;
                break;
            }
            else
                j=e[j]+1;
        }
    }
    long long temp=0;
    ans=-1;
    for(int i=1;i<=n;i++)
    {
        temp=(sum[e[i]]-sum[s[i]-1])*node[i];
        if(temp>ans)
        {
            ans=temp;
            S=s[i];
            E=e[i];
        }
    }
    cout<<ans<<endl;
    cout<<S<<" "<<E<<endl;
    return 0;
}
时间: 2024-10-05 21:24:47

[ACM] POJ 2796 Feel Good (求序列中子序列的和与子序列中的最小数最大乘积)的相关文章

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33363   Accepted: 10330 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

POJ 2299 Ultra-QuickSort (求序列的逆序对数)

题意:废话了一大堆就是要你去求一个序列冒泡排序所需的交换的次数. 思路:实际上是要你去求一个序列的逆序队数 看案例: 9 1 0 5 4 9后面比它小的的数有4个 1后面有1个 0后面没有 5后面1个 4后面没有 所以结果为4+1+0+1+0=6 所以逆序对的定义如果不清楚可以自己总结了 这道题说白了就是要你用归并排序求逆序对数. 下面是搜到某牛给的逆序对数的方法: 假设回溯到某一步,后面的两部分已经排好序(就是说当前需要归并的两个部分都是分别有序的),假设这两个序列为 序列a1:2 3 5 9

[ACM] POJ 1061青蛙的约会(扩展欧几里得求模线性方程)

青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 89206   Accepted: 15926 Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置.不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

[ACM] POJ 2442 Sequence (堆的性质)

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7011   Accepted: 2262 Description Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's

[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and r

[ACM] POJ 2593 Max Sequence (动态规划,最大字段和)

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15569   Accepted: 6538 Description Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). You should output S. Input The input will consist of several test cases. Fo

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to

[ACM] POJ 1068 Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn