B3799 [NICA #1] 序列

[B3799 NICA #1] 序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

发现两个操作跟顺序无关,最大子序列和就是所有正数之和。

对数组进行排序,之后求前缀和。用 n o w now now记录操作一加了多少。找到第一个 a i + n o w < 0 a_i + now \lt 0 ai+now<0的位置,求 ∑ k i a [ k ] \sum_k^ia[k] kia[k]即可。

如何快速求 i i i下标,可以用二分。

代码如下:

(自写二分)

#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 21;
using LL = long long;
LL pre[N], a[N];
int main()
{
    int n,q; cin>>n>>q;
    for(int i = 1; i <= n; ++i) cin>>a[i];
    sort(a + 1, a + n + 1, greater<int>());
    for(int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + a[i];
    LL now = 0;
    while(q--) {
        int op,k; cin>>op;
        if(op == 1) cin>>k, now += k;
        else {
            auto check = [&](int mid) -> bool {
                return a[mid] + now > 0;
            };
            int l = 1, r = n;
            while(l < r) {
                int mid = l + r + 1 >> 1;
                if(check(mid)) l = mid;
                else r = mid - 1;
            }
            LL ans = pre[l] + now * l;
            if(a[l] + now < 0) ans = 0;
            cout<<ans<<endl;
        }
    }
}

(用upper_bound

upper_bound函数用法:

如果是stl容器,则找到返回的是迭代器下标,否则返回v.end()

如果是普通数组,找到返回数组下标,否则返回0。

#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 21;
using LL = long long;
LL pre[N], a[N];
int main()
{
    int n,q; cin>>n>>q;
    for(int i = 1; i <= n; ++i) cin>>a[i];
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + a[i];
    LL now = 0;
    while(q--) {
        int op,k; cin>>op;
        if(op == 1) cin>>k, now += k;
        else {
            LL pos = upper_bound(a + 1, a + n + 1, -now) - a - 1;
            LL sum = pre[n] - pre[pos] + now * (n - pos);
            cout<<sum<<endl;
        }
    }
}

相关推荐

  1. B3799 [NICA #1] 序列

    2024-04-06 11:32:03       17 阅读
  2. B3637 最长上升子序列

    2024-04-06 11:32:03       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-06 11:32:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-06 11:32:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-06 11:32:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-06 11:32:03       20 阅读

热门阅读

  1. P8783 [蓝桥杯 2022 省 B] 统计子矩阵

    2024-04-06 11:32:03       16 阅读
  2. 关于人员的管理问题小讨论

    2024-04-06 11:32:03       15 阅读
  3. spring 和spring boot的区别

    2024-04-06 11:32:03       15 阅读
  4. C/C++中const关键字用法总结

    2024-04-06 11:32:03       17 阅读
  5. springcloud第4季 使用resilience4j实现服务流量治理

    2024-04-06 11:32:03       15 阅读
  6. C++入门

    C++入门

    2024-04-06 11:32:03      14 阅读
  7. Linux 指令

    2024-04-06 11:32:03       22 阅读
  8. MySQL Payload

    2024-04-06 11:32:03       13 阅读
  9. 金蝶Apusic应用服务器 未授权目录遍历漏洞复现

    2024-04-06 11:32:03       13 阅读