# 【2019.8.15 慈溪模拟赛 T1】插头（plugin）（二分+贪心）

### 代码

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 400000
#define LL long long
#define Gmax(x,y) (x<(y)&&(x=(y)))
using namespace std;
int n,m,a[N+5],b[N+5];
class FastIO
{
private:
#define FS 100000
#define tn (x<<3)+(x<<1)
#define D isdigit(c=tc())
char c,*A,*B,FI[FS];
public:
I FastIO() {A=B=FI;}
Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
}F;
class GreedySolver
{
private:
I bool Check(CI x)
{
RI i,p=1,q=x,d=0;LL t=1,nt;W(q)
{
W(q&&d==b[q]) --t,--q;if(t<0) return false;//插充电器
for(i=t;i&&p<=n;--i) t+=a[p++]-1;++d;//插排插
}return true;
}
public:
I void Solve()
{
W(a[n]<=1) --n;RI l=1,r=m,mid;//删去无用的排插
W(l<r) Check(mid=l+r+1>>1)?l=mid:r=mid-1;printf("%d",l);//二分答案
}
}G;
I bool cmp(CI x,CI y) {return x>y;}
int main()
{
freopen("plugin.in","r",stdin),freopen("plugin.out","w",stdout);