UVA 1619 Feel Good 感觉不错 (扫描法)

Feel Good

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

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<tex2html_verbatim_mark> - the number of days of Bill‘s life he is planning to investigate (1n100000)<tex2html_verbatim_mark> . The rest of the file contains n<tex2html_verbatim_mark> integer numbers a1, a2,..., an<tex2html_verbatim_mark> ranging from 0 to 106<tex2html_verbatim_mark> - 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<tex2html_verbatim_mark> and r<tex2html_verbatim_mark> such that the period from l<tex2html_verbatim_mark> -th to r<tex2html_verbatim_mark> -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

对于某个最小值ai来说,所选的区间应该尽量大,直到再选就不能保证ai是最小值的时候停止。

在扫描过程中维护一个向前延伸的最大位置,扩展的时候注意传递性,如果前面一个元素比它小,那么前面一个元素能延伸到的位置,

当前元素也可以延伸到,然后类似链表往前找的同时延伸链即可。向后找的时候类似。区间和用一个前缀和来处理。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
typedef long long ll;
int a[maxn],l[maxn],r[maxn],n;
ll sum[maxn];

int main()
{
    int T = 0; sum[0] = 0; a[0] = -1;
    while(~scanf("%d",&n)){
        if(T++) putchar(‘\n‘);
        a[n+1] = -1;
        for(int i = 1; i <= n; i++)
            scanf("%d",a+i),sum[i] = sum[i-1]+a[i],l[i]=r[i]=i;

        for(int i = 1; i <= n; i++){
            while(a[i]<=a[l[i]-1]) l[i] = l[l[i]-1];
        }

        for(int i = n; i >= 1; i--){
           while(a[i]<=a[r[i]+1]) r[i] = r[r[i]+1];
        }
        ll Max = (ll)a[1]*a[1];
        int L = 1,R = 1;
        for(int i = 1; i <= n; i++){
            ll tmp = (sum[r[i]]-sum[l[i]-1])*a[i];
            if(tmp > Max || (tmp == Max && R - L > r[i] - l[i] )){
                Max = tmp;
                L = l[i]; R = r[i];
            }
        }
        printf("%lld\n%d %d\n",Max,L,R);
    }
    return 0;
}
时间: 2024-10-15 01:52:06

UVA 1619 Feel Good 感觉不错 (扫描法)的相关文章

UVA - 1619 Feel Good(扫描法)

题目: 思路: 预处理出a[i]在哪个范围区间内是最小的,然后直接遍历a数组求答案就可以了. 这个预处理的技巧巧妙的用了之前的处理结果.(大佬tql) 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1e3 #define FRE() freopen("in.txt","r",stdin) #define FRO() freopen("out.txt",&

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-negat

uva 1619 - Feel Good 一个技巧

1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human emotions. His recent investigations are dedi- cated to studying how good or bad days in uent people's memories about some period of life. A new idea Bil

看到一篇写的关于block和delegate放在一起来方便大家理解的文章,感觉不错,就推荐给大家来看一下。

代理设计模式对于iOS开发的人来说肯定很熟悉了,代理delegate就是委托另一个对象来帮忙完成一件事情,为什么要委托别人来做呢,这其实是MVC设计模式中的模块分工问题,例如View对象它只负责显示界面,而不需要进行数据的管理,数据的管理和逻辑是Controller的责任,所以此时View就应该将这个功能委托给Controller去实现,当然你作为码农强行让View处理数据逻辑的任务,也不是不行,只是这就违背了MVC设计模式,项目小还好,随着功能的扩展,我们就会发现越写越难写:还有一种情况,就是

UVA - 1606 Amphiphilic Carbon Molecules 极角扫描法

题目:点击查看题目 思路:这道题的解决思路是极角扫描法.极角扫描法的思想主要是先选择一个点作为基准点,然后求出各点对于该点的相对坐标,同时求出该坐标系下的极角,按照极角对点进行排序.然后选取点与基准点形成的线对点进行扫描,基准线为遍历选取,扫描线扫过的点,减去基准线扫过的点即为所要求的点的数量.同时注意到我们要求的是线两边的两种点的数量,于是一种点比如黑点可以旋转180度,然后之考察这180度内的百点数量即可.本题的基准点选取复杂度为O(n),极角排序复杂度O(nlogn),扫描复杂度O(n),

今晚,终于自己把treap的实现写了一遍,感觉不错。加油。

1 #include<cstdio> 2 #include<iostream> 3 #include<cstdlib> 4 #define rep(i,j,k) for(int i = j; i <= k; i++ ) 5 using namespace std; 6 int n = 0; 7 8 int read() 9 { 10 int s = 0, t = 1; 11 char c = getchar(); 12 while( !isdigit(c) ){

UVa - 1619 - Feel Good

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-negat

UVa 1606 Amphiphilic Carbon Molecules (扫描法+极角排序)

题意:平面上有 n 个点,每个点不是黑的就是白的,现在要放一个隔板,把它们分成两部分,使得一侧的白点数加上另一侧的黑点数最多. 析:这个题很容易想到的就是暴力,不妨假设隔板至少经过两个点,即使不经过也可以通过平移使它经过,然后每次枚举两个点,当作隔板,枚举量是n*n, 然后计算是 n,那么时间复杂度就是 n3 ,一定会超时的,我产可以这样想,先枚举一个点,然后绕这个点旋转,每扫过一个点,就动态修改两侧的点数, 在转一周过程中,每个点至多扫到两次,这个过程复杂是 n,扫描前进行极角,时间复杂度是

UVA - 1619 Feel Good 标记+枚举

题目大意:给出n个正整数的序列,要求你找出符合sum(a1 + a2 + a3 + - + an) * min(a1,a2,-,an)的最大值,如果有多个符合条件的,输出最短序列 解题思路:从输出中观察得到,输出的答案要有三个,最大值,左端点,右端点. 那就设两个数组,纪录已当前数位最小值所能覆盖的最大区间,然后枚举每个数,求出区间和再乘上当前数求得值再进行比较 设置左右区间时,可以递归比较,假设l[i]纪录的时ai所能覆盖的最左端点,如果aj <= ai (j > i),那么l[j] = l