UVA 1619 Feel Good(DP)

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 day was. 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 input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.

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

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

On the first line of the output file print the greatest value of some period of Bill‘s life.

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 the shortest one. If there are still several possibilities, print the one that occurs first..

Sample Input

6
3 1 6 4 5 2

Sample Output

60 

3 5

题意:选取连续的几天,求区间中最小的值*区间值的和的最大值:

ps:大家去POJ交吧。UVA这道题坑死人
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1000010;
int l[maxn],r[maxn];
LL sum[maxn],dp[maxn];
int main()
{
   int n;
   int cas=0;
   while(~scanf("%d",&n))
   {
       memset(sum,0,sizeof(sum));
       if(cas)
          puts("");
       cas++;
       for(int i=1;i<=n;i++)
       {
           scanf("%lld",&dp[i]);
           sum[i]=sum[i-1]+dp[i];
           l[i]=r[i]=i;
       }
//       cout<<"23333  "<<endl;
       for(int i=1;i<=n;i++)
       {
           if(dp[i]>0)
           {
               while(dp[l[i]-1]>=dp[i])
                  l[i]=l[l[i]-1];
           }
       }
//       cout<<"hahahah  "<<endl;
       for(int i=n;i>=1;i--)
       {
           if(dp[i]>0)
           {
               while(dp[r[i]+1]>=dp[i])
                  r[i]=r[r[i]+1];
           }
       }
//       cout<<"111  "<<endl;
       LL ans=0,temp;
       int ll=1,rr=1;
       for(int i=1;i<=n;i++)
       {
//           cout<<"fuck  "<<endl;
           if(dp[i]>0)
           {
               temp=dp[i]*(sum[r[i]]-sum[l[i]-1]);
               if(temp>ans)
               {
                   ans=temp;
                   ll=l[i];
                   rr=r[i];
               }
               else  if(temp==ans)
               {
                   if(rr-ll==r[i]-l[i]&&l[i]<ll)
                   {
                       ll=l[i];
                       rr=r[i];
                   }
               }
           }
       }
       printf("%lld\n%d %d\n",ans,ll,rr);
   }
   return 0;
}

时间: 2024-10-03 23:53:38

UVA 1619 Feel Good(DP)的相关文章

UVA 10641 - Barisal Stadium(DP + 几何)

题目链接:10641 - Barisal Stadium 题意:逆时针给定n个点,在给m个灯,每个灯有一个花费,要求最小花费使得所有边能被灯照到 思路:用向量叉积判断向量的顺逆时针关系,从而预处理出每个灯能照到的边,然后由于n个点是环的,所以可以直接扩大两倍,dp时候去枚举起点即可 状态为dp[i]表示现在照到i条边之前的边全部照亮需要的最小花费 代码: #include <stdio.h> #include <string.h> const double eps = 1e-6;

UVA 580 - Critical Mass(DP)

题目链接:580 - Critical Mass 题意:一个栈,里面可以放L和U,有三个连续的U就是不安全的,问共有几种不安全的情况 思路:dp,dp[i][j][k],表示放到第i个,最后两个状态为j,k表示有没有出现不安全.然后去记忆化搜索一下就可以了 然后还有一种做法是,先考虑安全的情况,在用总情况(1<<n 种)减去安全的情况,安全的情况的递推方式很容易dp[i]表示放第i个, dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]. 不过这题都没给数据范围

UVA 11578 - Situp Benches(dp)

题目链接:11578 - Situp Benches 题意:健♂身♂房有两个仰卧起坐坐垫,每次调整角度要花费10元/10度,每次使用要花费15,现在给定n个人的时间顺序,和所希望的角度,求最少花费 思路:dp,dp[i][j][k]表示第i个人,一个角度为j,另一个为k的最小花费,一个人用和两个人用的情况分开讨论,然后记录dp状态转移路径.这个输出路径让这题变得麻烦了不少.不过机智的我还是把它搞♂出♂来♂了. 代码: #include <stdio.h> #include <string

uva 10163 Storage Keepers (DP)

uva 10163 Storage Keepers 题目大意:有N个仓库,M个管理员,M个管理员每个人的工资都不一样,工资与他们的能力值(P)相同.一个管理员可以看管多个(n)仓库,但是仓库的安全值就会变为P / n.现在要是的最小的安全值最大,并且还要求出该状况下的最小花费. 解题思路:两次DP,第一次dp求出最大的最小安全值ans,第二次dp根据第一次dp求出的ans求出最小的花费. #include <cstdio> #include <cstring> #include &

uva 10003 Cutting Sticks (DP)

uva 10003 Cutting Sticks Description 你的任务是替一家叫Analog Cutting Machinery (ACM)的公司切割木棍.切割木棍的成本是根据木棍的长度而定.而且切割木棍的时候每次只切一段.很显然的,不同切割的顺序会有不同的成本.例如:有一根长10公尺的木棍必须在第2.4.7公尺的地方切割.这个时候就有几种选择了.你可以选择先切2公尺的地方,然后切4公尺的地方,最后切7公尺的地方.这样的选择其成本为:10+8+6=24.因为第一次切时木棍长10公尺,

uva 10617 Again Palindrome (DP)

uva 10617 Again Palindrome 题目大意:给出一段字符串,可进行删除操作,可以删除任意位置任意个数(可以是0)的字符.问,进行删除操作使原本字符串变成回文字符串,有几种方式. 解题思路: dp[i][j] = 1 (i == j),单独一个字符也是回文字符串 s[i] != s[j]时, dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1], dp[i + 1][j] 和 dp[i][j + 1]的公共部分dp[

uva 116 Unidirectional TSP (DP)

uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Traveling Sa

UVa 12186 Another Crisis (DP)

题意:有一个老板和n个员工,除了老板每个员工都有唯一的上司,老板编号为0,员工们为1-n,工人(没有下属的员工),要交一份请愿书, 但是不能跨级,当一个不是工人的员工接受到直系下属不少于T%的签字时,自己也会签字,并交给上级,问你最少有多少工人签字,才能让老板收到请愿书. 析:题意比较简单,也好理解,很明显是一个动态规划的题目,d(u)表示u给上级要发信至少需要多少工人签字.假设u有k个结点,那么至少要 c = (kT-1)/100 + 1个工人,然后把每个结点排序,找出最少的工人即可,挺简单的

uva 10069 Distinct Subsequences (dp + 大数)

uva 10069 Distinct Subsequences 题目大意:给出两个字符串A和B,找出A中所有与B相同的子字符串. 解题思路:if(A[j?1]==B[i?1]){ dp[i][j]=dp[i][j]+dp[i?1][j?1]; } import java.math.BigInteger; import java.util.Scanner; /** * Created by spzn on 15-3-30. */ public class Main { public static