CodeForces 547B (单调栈)

题意

一个有n个元素的序列,没连续l个元素的最小值为这个串的strength值,求所有连续l个元素的strength是的最大值。

分析

a[i]如果是其所在串的strength值,那么必然它是最小值,往前和往后找小于它的第一个位置l、r,显然[l + 1, r + 1]区间的strength等于a[i]。
就可以更新长度为len(len = r - l + 1)的strength。还有就是长度为i的strength值一定不大于长度为(i - 1)的strength值。

const int maxn = 2e5 + 10;
int a[maxn];
int ans[maxn];
int pre[maxn];
int nxt[maxn];
int n;
int main(int argc, const char * argv[])
{    
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    // ios::sync_with_stdio(false);
    // cout.sync_with_stdio(false);
    // cin.sync_with_stdio(false);

    cin >> n;
    Rep(i, 1, n) scanf("%d", &a[i]);
    vector<int> st;
    Rep(i, 1, n) {
        while(!st.empty() && a[st.back()] >= a[i]) st.pop_back();
        st.empty() ? pre[i] = 0 : pre[i] = st.back();
        st.push_back(i);
    }
    st.clear();
    Rrep(i, n, 1) {
        while(!st.empty() && a[st.back()] >= a[i]) st.pop_back();
        st.empty() ? nxt[i] = n + 1 : nxt[i] = st.back();
        st.push_back(i);
    }
    Rep(i, 1, n) {
        int l = pre[i] + 1;
        int r = nxt[i] - 1;
        int len = r - l + 1;
        ans[len] = max(ans[len], a[i]);
    }
    Rrep(i, n, 1) ans[i - 1] = max(ans[i - 1], ans[i]);
    Rep(i, 1, n) printf("%d%c", ans[i], i == n ? '\n' : ' ');
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值