牛客月赛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';
}
相关推荐
code斗12 小时前
Java数据结构:堆详解
java·开发语言·数据结构
顺顺 尼13 小时前
linux 进程信号(上)
linux·c++·算法
罗西的思考13 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 ---(5)--- GRPO
人工智能·算法·机器学习
richard_yuu14 小时前
动态规划:强化学习的「数学基础」,从 MDP 到值迭代
深度学习·神经网络·算法·yolo·机器学习·动态规划
mmmmath_314 小时前
LeetCode.225.用队列实现栈
算法
Elaine33614 小时前
数据结构与算法-程序
数据结构·python·算法·计算机基础·编程基础
2601_9657422214 小时前
布局GEO AI本地营销,我重点看这几个细节
大数据·人工智能·算法·ai·新媒体运营
EatFan14 小时前
AI Agent 上生产前先加三道闸门:审批、限权、可回放的工程实践
人工智能·python·算法·多智能体·ai agent·mcp·harness
思茂信息14 小时前
CST软件BCI仿真模型及仿真案例
开发语言·单片机·嵌入式硬件·算法·emc
Persistent的粽子!15 小时前
双指针算法:最大盛水容器
c++·算法·leetcode