二分答案dp

给定一个序列,有n+2个数,选择一个数i能获得一个ai,至少获得w的前提下,选的数的最小间隔是多少,考虑用二分答案解决,用二分选择答案,然后进行check即可,然后对于可以的答案取mid和右边,不可以的考虑取左边和mid。
对于check用dp进行,dp【i】代表从i到最后的n+1点通过最小x间隔能选出的最大值,对于第i个,我们知道要么选要么不选,若不选则dp【i】=dp【i+1】,选的话是dp【i+x+1】然后取max即可。
代码如下

/*
 * @Author: tunan
 * @Date: 2021-06-21 20:59:05
 * @LastEditTime: 2021-06-29 21:26:00
 * @Description: In User Settings Edit
 * @FilePath: \c++worksapce\2021.06.21\new.cpp
 */
#include<bits/stdc++.h>
#define ll long long
const ll mod = 1e9+7;
const int maxx=1e5+7;
using namespace std;
ll n,m,x,y,z,k;
int arr[maxx];
int f[maxx];
bool check(int x){
    ll sum=0;
    memset(f,0,sizeof(f));
    ll ans=0;
    for(int i=x;i<=n+1-x;i++){
        f[i]=max(f[i-1],f[i-x]+arr[i]);
        ans=max(f[i],ans);
    }
    return ans>=m;
}
void solve(){
    cin>>n>>m;
    x=0;
    int l=1;int r=n+1;
    for(int i=1;i<=n;i++)cin>>arr[i],x+=arr[i];
    if(x<m){cout<<"-1";return ;}
    while(l<r){
        int mid=l+r+1>>1;
        if(check(mid))l=mid;
        else r=mid-1;
    }
    cout<<r<<endl;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie();
    solve();
    return 0;
}
    
        
    
                                                                                                  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值