B. Pipeline(二分+贪心)

https://codeforces.com/problemset/problem/287/B


思路:贪心拿最大的。

每次取x个管子的式子为1+(2*k-x+1)*x/2-x,二分即可

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
bool check(LL x,LL k,LL n){
    if(1+(2*k-x+1)*x/2-x>=n) return true;
    else return false;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,k;cin>>n>>k;
  if(n==1){
    cout<<"0"<<"\n";
  }
  else{
    LL l=1;LL r=k+1;
    while(l<r){
        LL mid=(l+r)>>1;
        if( check(mid,k,n) ) r=mid;
        else l=mid+1;
    }
    if(l==k+1){
        cout<<"-1"<<"\n";
    }
    else cout<<l<<"\n";
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/115024381