UVA - 1619 Feel Good 标记+枚举

题目大意:给出n个正整数的序列,要求你找出符合sum(a1 + a2 + a3 + … + an) * min(a1,a2,…,an)的最大值,如果有多个符合条件的,输出最短序列

解题思路:从输出中观察得到,输出的答案要有三个,最大值,左端点,右端点。

那就设两个数组,纪录已当前数位最小值所能覆盖的最大区间,然后枚举每个数,求出区间和再乘上当前数求得值再进行比较

设置左右区间时,可以递归比较,假设l[i]纪录的时ai所能覆盖的最左端点,如果aj <= ai (j > i),那么l[j] = l[i]了,然后再不断的更新找寻即可

这里要当心一个坑点,因为他所给的数的范围是0--1000000的,包含0的,当数组初始化的时候也是0的,所以,如果给出数中有0的话,那么比较的时候就会越界了

#include<cstdio>
#include<cstring>
#define maxn 1000010
long long sum[maxn];
int num[maxn], l[maxn], r[maxn], n;
void init() {
    sum[0] = 0;
    memset(num,-1,sizeof(num));
    for(int i = 1; i <= n; i++) {
        scanf("%d", &num[i]);
        sum[i] = sum[i-1] + num[i];
        l[i] = i;
        r[i] = i;
    }

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

void solve() {
    long long Max = 0;
    int ll = 1, rr = 1;
    long long s;
    for(int i = 1; i <= n; i++) {
        s = num[i] * (sum[r[i]] - sum[l[i] - 1]);
        if(s > Max || (s == Max && rr - ll > r[i] - l[i])) {
            Max = s;
            ll = l[i];
            rr = r[i];
        }
    }
    printf("%lld\n%d %d\n", Max, ll, rr);
}

int main() {
    int cas = 0;
    while(scanf("%d", &n) != EOF) {
        if(cas)
            printf("\n");
        cas++;
        init();
        solve();
    }
    return 0;
}
时间: 2024-08-06 09:48:20

UVA - 1619 Feel Good 标记+枚举的相关文章

uva 1560 - Extended Lights Out(枚举 | 高斯消元)

题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5?6的矩阵,每个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,如果按了这个位置的开关,将会导致周围包括自己位置的灯状态变换,求一个按开关位置,保证所有灯都灭掉. 解题思路: 枚举,枚举第一行的状态,然后递推出后面四行的状态. 高斯消元,对于每个位置对定变量,这样列出30个方程求解. C++ 枚举 #include <cstdio> #include <cstring> #include &

uva 565 - Pizza Anyone?(暴力枚举 + 二进制)

题目:uva 565 - Pizza Anyone?(暴力枚举 + 二进制) 题目大意:题目是说有一个人要帮他的朋友们定批萨,然后每个朋友都有自己的口味要求,问能不能定一个批萨然后满足每个朋友的至少一个要求. 能就输出所定批萨里面加的东西,,输出要求按字典序: 不能就输出:No pizza can satisfy these requests. 解题思路:这题里面有16种材料,每种材料只有取与不取的可能,这样就有 2^16 种( 0 - 2^16 - 1),枚举出每种情况然后在分别看是否能满足每

UVA - 10892 LCM Cardinality (枚举因子)

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible pairs. Forexample 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, the number of different integer pairs withLCM is equal to

UVa 11825 - Hackers&#39; Crackdown DP, 枚举子集substa = (substa - 1)&amp;sta 难度: 2

题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2925 题意 n个节点,每个节点都有完全相同的n项服务. 每次可以选择一个节点,破坏该节点和相邻节点的某项服务. 问最多能完全破坏多少服务? 思路 如刘书, 直接枚举状态的子集 注意元素个数为k的集合有C^k_n个子集,那么枚举的时间复杂度为sum{c^k_n * 2^k} = 3^n

uva 11210 Chinese Mahjong(暴力枚举)

uva 11210 Chinese Mahjong Mahjong () is a game of Chinese origin usually played by four persons with tiles resembling dominoes and bearing various designs, which are drawn and discarded until one player wins with a hand of four combinations of three

uva 11520 Fill the Square(枚举)

uva 11520 Fill the Square In this problem, you have to draw a square using uppercase English Alphabets. To be more precise, you will be given a square grid with some empty blocks and others already filled for you with some letters to make your task e

uva 725 Division(暴力枚举)

uva 725  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =

UVa1619 Feel Good (标记,枚举)

链接:http://vjudge.net/problem/UVA-1619 分析:首先计算前缀和,后面会用到.设每个i都属于一个满足下述条件的最大区间(因为题意说是正整数序列,区间当然越大越好),a[i]为区间中的最小值,然后预处理出以i为轴向左和向右延伸的最远位置L[i]和R[i]来标记这个区间,最后就是O(n)的枚举. 1 #include <cstdio> 2 3 const int maxn = 1e5+5; 4 5 int n, a[maxn], L[maxn], R[maxn];

uva 1352 Colored Cubes(枚举)

uva 1352 Colored Cubes There are several colored cubes. All of them are of the same size but they may be colored differently. Each face of these cubes has a single color. Colors of distinct faces of a cube may or may not be the same. Two cubes are sa