牛客月赛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';
}
相关推荐
sali-tec5 小时前
C# 基于halcon的视觉工作流-章66 四目匹配
开发语言·人工智能·数码相机·算法·计算机视觉·c#
小明说Java6 小时前
常见排序算法的实现
数据结构·算法·排序算法
行云流水20196 小时前
编程竞赛算法选择:理解时间复杂度提升解题效率
算法
smj2302_796826528 小时前
解决leetcode第3768题.固定长度子数组中的最小逆序对数目
python·算法·leetcode
cynicme8 小时前
力扣3531——统计被覆盖的建筑
算法·leetcode
core5129 小时前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.8249 小时前
计数if|
算法
a伊雪9 小时前
c++ 引用参数
c++·算法
Data_agent10 小时前
1688获得1688店铺列表API,python请求示例
开发语言·python·算法
2301_7644413310 小时前
使用python构建的应急物资代储博弈模型
开发语言·python·算法