zoukankan      html  css  js  c++  java
  • b_mt_比当前数小的最大值之和(--set.lower_bound() -> 比自己小的最大值)

    有一个长度为的序列 A,定义序列中第 i 个数的 prev[i] 的值为前 i-1 个数中比 A[i] 小的最大的值,即满足 1<j<i,且 A[j] < A[i] 中最大的 A[j],若不存在这样的数,则 prev[i] 的值为 0。现在计算对于所有的 prev[i]i 之和是多少, 即(sum(prev[i]i)(1<=i<=n)

    输入描述
    第一行是一个整数n表示序列的长度。
    接下来行n个 数用空格隔开, 第1个数表示间的大小。
    输出描述
    一行个整数,表示答案。
    输入
    5
    1 6 3 3 8
    样例输出
    39
    提示
    100%的数据保证n<=100000, 1<=A[i]<=100000。

    思路:...

    #include<bits/stdc++.h>
    using namespace std;
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
        int n; cin >> n;
        int s = 0;
        set<int> st;
        int x = 0;
        for (int i = 1; i <= n; i++) {
            cin >> x;
            auto prev_iter = st.lower_bound(x);
            if (prev_iter != st.begin()) {
                prev_iter--;
                s += *prev_iter * i;
            }
            st.insert(x);
        }
        cout << s;
        return 0;
    }
    
  • 相关阅读:
    获取配置文件
    windows下多tomcat部署
    tomcat是否有必要配置环境变量(摘)
    js删除map中元素
    HDU-3440 House Man
    HDU-1534 Schedule Problem
    POJ-1364/HDU 1531 King
    POJ-1275/HDU-1529 Cashier Employment
    POJ-1201/HDU-1384 Intervals
    HDU-5780 gcd
  • 原文地址:https://www.cnblogs.com/wdt1/p/15115406.html
Copyright © 2011-2022 走看看