CF965D Single-use Stones
思路
这题的二分解法明明很简单,但是所有题解都是一样的做法,没有二分题解,于是我来一篇二分的题解。
容易想到若有
接下来就是
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int const N=1e5+10;
int a[N],w,l;
inline bool check(int x){
deque< pair<int,int> >q;
q.push_back({0,x});
for (int i=1;i<w;++i){
if (!a[i]) continue;
if (abs(i-q.front().first)>l) return 0;
int x=a[i];
while (!q.empty() && x){
auto now=q.front();q.pop_front();
if (x>=now.second) x-=now.second;
else{now.second-=x;x=0;q.push_front(now);}
}
q.push_back({i,a[i]-x});
}
return (w-q.front().first)<=l;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>w>>l;
for (int i=1;i<w;++i) cin>>a[i];
int l=0,r=1e9;
while (l<r){
if (l+1==r){
if (check(r)) l=r;
break;
}
int mid=(l+r)>>1;
if (check(mid)) l=mid;
else r=mid-1;
}
cout<<l<<'\n';
return 0;
}
本文作者:Tx_Lcy
本文链接:https://www.cnblogs.com/tx-lcy/p/16856755.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步