zoukankan      html  css  js  c++  java
  • 【Codeforces 276C】Little Girl and Maximum Sum

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

    在这里输入题意

    【题解】

    最后的和。 其实可以看成是 ∑bi*ai 的形式。 这里的bi这个系数表示的是有多少个区间覆盖了ai这个元素。 既然这样的话。 那么我们可以用个技巧li++,ri--的技巧算出来每个bi.(或者用线段树的成段更新应该也可以 然后把bi从大到小排个序 然后把ai也从大到小排个序。 (这样就能满足ai大的,他的系数也比较大了。

    【代码】

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int N = 2e5;
    
    int n,q,a[N+10],b[N+10];
    int in[N+10],out[N+10];
    
    int main()
    {
        #ifdef LOCAL_DEFINE
            freopen("rush.txt","r",stdin);
        #endif // LOCAL_DEFINE
        ios::sync_with_stdio(0),cin.tie(0);
        cin >> n >> q;
        for (int i = 1;i <= n;i++) cin >> a[i];
        while (q--){
            int l,r;
            cin >> l >> r;
            in[l]++;out[r+1]++;
        }
        int cur = 0;
        for (int i = 1;i <= n;i++){
            cur+=in[i];cur-=out[i];
            b[i] = cur;
        }
        sort(a+1,a+1+n);
        sort(b+1,b+1+n);
        long long ans = 0;
        for (int i = 1;i <= n;i++)
            ans = ans + 1LL*a[i]*b[i];
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    蓝桥杯_基础训练_龟兔赛跑预测
    大数加法
    Splay!
    topsort
    各种方法
    有时候dfs可以简化各种组合的操作
    组合数学
    重新认识三观
    手速狗还是不行啊。。。
    set和map和pair 转自ACdreamers
  • 原文地址:https://www.cnblogs.com/AWCXV/p/9733095.html
Copyright © 2011-2022 走看看