程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj2796 Feel Good

poj2796 Feel Good

編輯:C++入門知識

poj2796 Feel Good


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

這題是用單調棧,題意是給你一段區間,求出(在這段區間之類的最小值*這段區間所有元素之和)的最大值。可以維護一個單調遞增隊列,記錄棧內的sum(區間所有元素的和),minmum(區間最小值),begin(區間的最前面一個數的位置),count(區間的長度)。每讀入一個數,把棧內所有大於等於它的數都刪掉,這裡刪掉的時候注意要把刪掉的數所對應的區間的長度以及和傳到棧內下面的一個數所對應的區間,因為這個數要比下面的數大,所以對下面那個數來說,刪掉的數所對應的區間也可以包含進來。當所有的數都循環完後,再從棧頂開始向下刪除數,同時把這個數的信息傳到下一個數,可以發現兩個操作在這裡是一樣的。

 

#include
#include
#include
#include
#include
using namespace std;
#define maxn 100060
#define ll long long 
struct node{
	ll sum,minmum,begin,count;
	
}stack[maxn];
int main()
{
	//int n,m,i,j,top;
	int n,m,i,j;
	ll ans,sum1,tmp,count,a,ansbegin,ansend,top;
	while(scanf(%d,&n)!=EOF)
	{
		ansbegin=ansend=1;
		top=0;ans=0;
		for(i=1;i<=n;i++){
			scanf(%lld,&a);
			sum1=0;count=tmp=0;
			while(top>0 && stack[top].minmum>=a){
				stack[top].count+=count;
				count=stack[top].count;
				stack[top].sum+=tmp;
				tmp=stack[top].sum;
				sum1=stack[top].sum*stack[top].minmum;
				if(ans0){
				stack[top].count+=count;
				count=stack[top].count;
				stack[top].sum+=tmp;
				tmp=stack[top].sum;
				sum1=stack[top].sum*stack[top].minmum;
				if(ans

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved