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

思路:前缀和+双指针

代码:

#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,而且因为添加的数是递减的,所以不会相互干扰.

代码:

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';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临江浪怀柔ℳ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值