zoukankan      html  css  js  c++  java
  • 【习题 8-18 UVA

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    用单调队列求出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的情况要注意,直接取[1,1]就好,不能取[1..n]

    【代码】

    #include <bits/stdc++.h>
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define all(x) x.begin(),x.end()
    #define pb push_back
    #define ls l,mid,rt<<1
    #define rs mid+1,r,rt<<1
    using namespace std;
    
    const double pi = acos(-1);
    const int dx[4] = {0,0,1,-1};
    const int dy[4] = {1,-1,0,0};
    const int N = 1e5;
    
    int n,a[N+10],l[N+10],r[N+10];
    LL pre[N+10];
    stack<int> sta;
    
    void solve(){
        rep1(i,1,n) cin >> a[i];
        pre[0] = 0;
        rep1(i,1,n) pre[i] = pre[i-1]+a[i];
        l[1] = 0;
    
        while (!sta.empty()) sta.pop();
    
        sta.push(1);
        for (int i = 2;i <= n;i++){
            while (!sta.empty() && a[sta.top()]>=a[i]) sta.pop();
            if (sta.empty())
                l[i] = 0;
            else
                l[i] = sta.top();
            sta.push(i);
        }
    
        while (!sta.empty()) sta.pop();
        sta.push(n);
        r[n] = n+1;
        for (int i = n-1;i >= 1;i--){
            while (!sta.empty() && a[sta.top()]>=a[i]) sta.pop();
            if (sta.empty())
                r[i] = n+1;
            else
                r[i] = sta.top();
            sta.push(i);
        }
    
        long long ans = -1;
        int posl,posr;
    
        rep1(i,1,n){
            int ll = l[i]+1;int rr = r[i]-1;
            if (a[ll]==0 && a[rr]==0){
                rr = ll;
            }
            LL temp = 1LL*a[i]*(pre[rr]-pre[ll-1]);
    
            if (temp>ans){
                ans = temp;
                posl = ll,posr = rr;
            }else if (temp==ans){
                if (posr-posl>rr-ll || (posr-posl==rr-ll && ll<posl)){
                    posl = ll,posr = rr;
                }
            }
        }
        cout<<ans<<endl;
        cout<<posl<<' '<<posr<<endl;
    }
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("rush_in.txt", "r", stdin);
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
        int kase = 0;
        while (cin>>n){
            if (kase>0)
                cout<<endl;
            else
                kase++;
            solve();
        }
    	return 0;
    }
    
    
  • 相关阅读:
    3.css3文字与字体
    2.css3表示颜色的几种方式
    测试ssh转发
    硬盘相关合集,以及LVM操作实践
    linux硬件驱动
    启动nfs清除端口占用过程
    postgres10.2时区研究
    git submodule临时分支;以及git reset使用
    sudo环境变量问题;程序库函数寻找
    linuxI/O重定向
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8457099.html
Copyright © 2011-2022 走看看