POJ 2796 Feel Good(并查集)

74 篇文章 0 订阅
39 篇文章 0 订阅

题目链接:点击打开链接

思路:

该题转化一下, 就是枚举每一个数, 找到以这个数为最小值的最大区间(因为没有负数)。  那么一个办法是预处理出每一个数左边第一个比他大的数的位置, 和右边第一个比他大的数的位置, 这个可以用构造单调栈的线性算法处理出来: 我们构造一个单调上升栈, 标记栈里每个元素在实际中的位置, 加入一个元素a[i]的时候, 如果栈顶元素大于他, 那么将栈顶元素出队列, i就是这个元素右边大于他的第一个元素。  直到栈顶元素<=a[i],这时,栈顶元素就是a[i]左边第一个>=他的, 把a[i]入队列。 复杂度O(n)

还有一个思维复杂度低的方法, 我们从大到小访问每一个元素, 如果他左边或者右边的元素>=他, 他们将他们合并, 这样, 符合条件的集合就是一个连续区间。 并且, 先访问的一定>=后访问的, 每次更新答案即可。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1e5 + 10;
int T,n,m,p[maxn];
int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); }
ll sum[maxn],a[maxn];
struct node {
    int l, r, v;
    node(int l=0, int r=0, int v=0):l(l), r(r), v(v) {}
}res[maxn];
struct edge {
    int pos, v;
    edge(int pos=0, int v=0):pos(pos), v(v) {}
    bool operator < (const edge& rhs) const {
        return v > rhs.v;
    }
}b[maxn];
int main() {
    while(~scanf("%d",&n)) {
        ll ans = 0, l = 1, r = 1;
        for(int i = 1; i <= n; i++) {
            scanf("%I64d", &a[i]);
            b[i] = edge(i, a[i]);
            sum[i] = sum[i-1] + a[i];
            p[i] = i;
            res[i] = node(i, i, a[i]);
            if(ans < a[i] * a[i]) {
                ans = a[i] * a[i];
                l = r = i;
            }
        }
        sort(b+1, b+n+1);
        for(int i = 1; i <= n; i++) {
            int pp = b[i].pos;
            if(pp > 1 && a[pp-1] >= a[pp]) {
                int x = _find(pp);
                int y = _find(pp-1);
                if(x != y) {
                    p[x] = y;
                    res[y] = node(min(res[y].l,res[x].l), max(res[y].r,res[x].r), a[pp]);
                    if(ans < a[pp]*(sum[res[y].r] - sum[res[y].l-1])) {
                        ans = a[pp]*(sum[res[y].r] - sum[res[y].l-1]);
                        l = res[y].l;
                        r = res[y].r;
                    }
                }
            }
            if(pp < n && a[pp+1] >= a[pp]) {
                int x = _find(pp);
                int y = _find(pp+1);
                if(x != y) {
                    p[x] = y;
                    res[y] = node(min(res[y].l,res[x].l), max(res[y].r,res[x].r), a[pp]);
                    if(ans < a[pp]*(sum[res[y].r] - sum[res[y].l-1])) {
                        ans = a[pp]*(sum[res[y].r] - sum[res[y].l-1]);
                        l = res[y].l;
                        r = res[y].r;
                    }
                }
            }
        }
        printf("%I64d\n%I64d %I64d\n", ans, l, r);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值