牛客月赛86+cf(edu)好题

思路:前缀和+双指针

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int t = 1;
  for (int ti = 0; ti < t; ti += 1) {
    int n, W;
    cin >> n >> W;
    vector<i64> w(n + 1), d(n + 1);
    for (int i = 1; i <= n; i += 1) {
      cin >> w[i];
      w[i] += w[i - 1];
    }
    for (int i = 1; i <= n; i += 1) {
      cin >> d[i];
      d[i] += d[i - 1];
    }
    i64 ans = -1e18, mn = 1e18;
    for (int i = 1, j = 0; i <= n; i += 1) {
      while (w[i] - w[j] >= W) {
        mn = min(mn, d[j]);
        j += 1;
      }
      ans = max(ans, d[i] - mn);
    }
    cout << ans;
  }
}

E. Increasing Subsequences

思路:首先如果有0,1,2......,x-1,那么有2^x种选择方案.考虑二进制拆分,首先构造出最高位的答案2^x种.然后从大到小遍历每一位,如果第i位是1,就在序列最后添加一个i,不难发现每添加一次数会让答案增加2^i,而且因为添加的数是递减的,所以不会相互干扰.

代码:

cpp 复制代码
void solve(){
    int n;
    cin >> n;
    int t = __lg(n);
    vector<int> ans;
    for(int i = 0; i < t; i++){
        ans.push_back(i);
    }
    for(int i = t - 1; i >= 0; i--){
        if (n >> i & 1){
            ans.push_back(i);
        }
    }
    cout << ans.size() << '\n';
    for(auto x : ans) cout << x << ' ';
    cout << '\n';
}
相关推荐
聚客AI21 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
moonlifesudo1 天前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法