您的位置:首页 > Web前端

POJ2796 Feel Good(单调栈)

2015-09-03 00:32 369 查看
题目:

Feel Good

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 11415Accepted: 3148
Case Time Limit: 1000MSSpecial 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

题意:给一个序列,求出一个区间,这个区间的最小值乘以这个区间的元素之和最大。

思路:我们可以枚举每一个元素作为最小值时所能得到的最大区间之和,然后再求他们乘积的最大值。求一个元素作为最小值时的最大区间之和就是要尽可能地向左和向右扩展,直到遇到比它更小的元素。所以我们可以维护一个单调栈,每个元素入栈时,使得栈顶大于等于它的元素出栈,并记录下出栈的元素之和,直到遇到比它更小的元素,这就是它作为最小值时向左扩展的距离,以后的元素再入栈时都不可能再改变这个值。等到它出栈时说明有一个比它更小的元素把它弹出了,我们就可以记录下在它出栈之前的元素之和,这就是它向右扩展的距离,两个和加起来就得到了它作为最小值时得到的最大区间之和。所以栈里的每个元素有四个属性,一是它本身的和,二是它的区间的和,三是它的区间起点,四是它的区间的元素个数,这些信息都需要在入栈或出栈时更新。要注意一点,当一个元素把另一个元素弹出栈时,应该加上那个元素所在区间的和而不是本身的值,并且要加上它所在区间的元素个数。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/

int a[1000000+5];
struct node
{
    int val;
    long long int sum;
    int start;
    int count;
};
node s[1000000+5];
int main()
{
int n;
RI(n);
long long int ans=0;
REP(i,0,n)
{RI(a[i]);
}
 int top=0;
int st=1,ed=1;

REP(i,0,n)
{
    long long int tmp=0;
    int cnt=0;
    while(top>0&&s[top-1].val>=a[i])
    {

        s[top-1].count+=cnt;
        if(ans<(long long )(s[top-1].sum+tmp)*s[top-1].val)
        {
            ans=(s[top-1].sum+tmp)*s[top-1].val;
            st=s[top-1].start;
            ed=s[top-1].start+s[top-1].count-1;
        }
        cnt=s[top-1].cou27nt;
        tmp+=s[top-1].sum;
        top--;

    }
    s[top].val=a[i];
    s[top].sum=tmp+a[i];
    s[top].start=i-cnt+1;
    s[top].count=cnt+1;
    top++;
}
long long int tmp=0;
int cnt=0;
while(top>0)
{

        s[top-1].count+=cnt;
        if(ans<(long long )(s[top-1].sum+tmp)*s[top-1].val)
        {
            ans=(long long )(s[top-1].sum+tmp)*s[top-1].val;
            st=s[top-1].start;
            ed=s[top-1].start+s[top-1].count-1;
        }
        cnt=s[top-1].count;
        tmp+=s[top-1].sum;
        top--;
}
printf("%I64d\n",ans);
printf("%d %d\n",st,ed);

        return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: