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","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1000100;
int r[maxn],l[maxn];
ll sum[maxn],a[maxn];
int n;

int main(){
    FRE();
    int kase = 0;
    while(scanf("%d",&n)!=EOF){
        sum[0] = 0;
        a[0] = a[n+1] = -1;
        for(int i=1; i<=n; i++){
            scanf("%lld",&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];
            }
        }

        int L=1,R=1;//当不知道具体的边界的时候,就将边界设为开头
        ll ans = a[1]*a[1];
        for(int i=1; i<=n; i++){
            ll tsum = sum[r[i]] - sum[l[i]-1];
            if(ans < tsum * a[i]){
                L = l[i];
                R = r[i];
                ans = tsum*a[i];
            }
        }
        if(kase++){
            printf("\n");
        }
        printf("%lld\n",ans);
        printf("%d %d\n",L,R);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/sykline/p/10352836.html

时间: 2024-10-18 23:36:58

UVA - 1619 Feel Good(扫描法)的相关文章

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 a

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

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 - 1619 Feel Good 标记+枚举

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

UVA 1619/POJ2796 滑窗算法/维护一个单调栈

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12409   Accepted: 3484 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

【习题 8-18 UVA - 1619】Feel Good

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用单调队列求出l[i]和r[i] 分别表示i的左边最近的大于a[i]的数的位置以及i右边最近的大于a[i]的数的位置. 则l[i]+1..r[i]-1就是a[i]这个数作为最小数的最大管辖区间了. 写个前缀和就好. 然后取a[i]*区间l[i]+1..r[i]-1的和 中的最大值. 并不是special judge 多个相同区间. 取区间长度最短的区间. 如果仍有多个答案. 取左端点最小的那个区间. (全都是0的情况要注意,直接

UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)

任意线可以贪心移动到两点上.直接枚举O(n^3),会TLE. 所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了.旋转的时候在O(1)时间推出下一种情况,总复杂度为O(n^2logN)就可以过了. 另外,本题有个很巧妙的技巧,就是一点等效与相反坐标的相反颜色的点. 第一次写,细节还是蛮多的,花了好久才搞清所有细节... 极角排序版,比较容易理解,932ms. #include<bits/stdc++.h> using namespace std; const

UVA - 1606 Amphiphilic Carbon Molecules 极角扫描法

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